0

I've parse a date like: 2016-07-21T09:24:06Z on UTC zone.

I change it to Instant:

Instant.parse(stringLiteral) -> 2016-07-21T09:24:06Z

Then to java.util.Date:

Date utcDateTime = Date.from(Instant.parse(stringLiteral));

However, utcDateTime is Thu Jul 21 11:24:06 CEST 2016.

How can I to keep the UTC zone and create a java.util.Date object on UTC?

I'd like to use an elegant way to do that without specifying the parse format string.

I've also tried with this code:

SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date utcDateTime = isoFormat.parse(stringLiteral);

However, the datetime is changed as well -> Thu Jul 21 11:24:06 CEST 2016

Jordi
  • 20,868
  • 39
  • 149
  • 333
  • `java.util.Date` doesn't have its own timezone. It simply uses your JVM's default timezone. – Andy Turner Jul 21 '16 at 10:34
  • I get it. However, I'm building this `Date` inside a server. – Jordi Jul 21 '16 at 10:35
  • In that case, you will need to set the JVM's timezone to UTC, because `java.util.Date` uses the JVM's default timezone. Or use `DateTime`, which actually *does* have a time zone. `java.util.Date` is basically a Jodatime `Instant`, just with some really confusing (and long-since deprecated) methods to fool you into thinking it's something more. – Andy Turner Jul 21 '16 at 10:36

0 Answers0