There seems to be a problem in my code. I expect different times in my output - one converted from UTC to german time and one to east brasilian time, but i get always the same UTC time output.
My code:
LocalDateTime localDateTime = resource.getDateOfManufacture();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy_HH-mm-ss", Locale.getDefault());
ZoneId zoneDE = ZoneId.of("Europe/Berlin");
ZoneId zoneBR = ZoneId.of("Brazil/East");
System.out.println("TIME --------------------------------------------------------------------");
System.out.println(localDateTime);
System.out.println(localDateTime.atZone(zoneDE));
System.out.println(localDateTime.atZone(zoneBR));
System.out.println(localDateTime.atZone(zoneDE).format(dateTimeFormatter));
System.out.println(localDateTime.atZone(zoneBR).format(dateTimeFormatter));
My actual Output:
13:06:43,890 INFO [stdout] (EJB default - 2) TIME --------------------------------------------------------------------
13:06:43,904 INFO [stdout] (EJB default - 2) 2016-05-31T22:00
13:06:43,907 INFO [stdout] (EJB default - 2) 2016-05-31T22:00+02:00[Europe/Berlin]
13:06:43,908 INFO [stdout] (EJB default - 2) 2016-05-31T22:00-03:00[Brazil/East]
13:06:43,913 INFO [stdout] (EJB default - 2) 31-05-2016_22-00-00
13:06:43,917 INFO [stdout] (EJB default - 2) 31-05-2016_22-00-00
My Expection (regarding the last two lines):
13:06:43,890 INFO [stdout] (EJB default - 2) TIME --------------------------------------------------------------------
13:06:43,904 INFO [stdout] (EJB default - 2) 2016-05-31T22:00
13:06:43,907 INFO [stdout] (EJB default - 2) 2016-05-31T22:00+02:00[Europe/Berlin]
13:06:43,908 INFO [stdout] (EJB default - 2) 2016-05-31T22:00-03:00[Brazil/East]
13:06:43,913 INFO [stdout] (EJB default - 2) 01-06-2016_00-00-00
13:06:43,917 INFO [stdout] (EJB default - 2) 31-05-2016_19-00-00
Your code is just adding a zone to a local date time, which has no zone. The first 2 lines show exactly what those results are. Your date time formatters aren't going to do anything special either, they are just formatting everything but ignoring the zone.
The misconception that you might have is that you might have thought local date times represents a date-time in UTC. They don't. They are simply a combination of a year, month, day, hour, minute, second, and nano.
The output you expect shows that you want to get the corresponding date-times in another timezone, if localDateTime
were in UTC. You are essentially asking, given that I will have a party at this date in UTC, what strings would I need to produce, so that I can tell my friends in Berlin and Brazil to come to the party?
One way to do this is to get a ZonedDateTime
at the zone UTC
, then set the zone of your date time formatters:
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy_HH-mm-ss", Locale.getDefault());
ZoneId zoneDE = ZoneId.of("Europe/Berlin");
ZoneId zoneBR = ZoneId.of("Brazil/East");
DateTimeFormatter formatterDE = dateTimeFormatter.withZone(zoneDE);
DateTimeFormatter formatterBR = dateTimeFormatter.withZone(zoneBR);
System.out.println(localDateTime.atZone(ZoneOffset.UTC).format(formatterDE));
System.out.println(localDateTime.atZone(ZoneOffset.UTC).format(formatterBR));