I have UTC date and I need to create Date object with exact same value as UTC ( legacy reasons ).
I have managed to do it:
String date = "2012-05-05 12:13:14";
TemporalAccessor formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.withZone(ZoneId.of("UTC"))
.parse(date);
Instant instant = Instant.from(
formatter
); //
ZonedDateTime zdf = ZonedDateTime.ofInstant(instant,ZoneId.of("UTC"));
Calendar calendar = Calendar.getInstance();
calendar.set(zdf.getYear(),zdf.getMonthValue(),zdf.getDayOfMonth(),zdf.getHour(),zdf.getMinute(),zdf.getSecond());
Date dt = calendar.getTime();
Date d2 = Date.from(instant);
However, what bothers me -
When I create date object, it should display the date in my JVM's default timezone. But here dt
has exact same value as my input UTC date, but dt2
has same date represented in my default timezone, why did this happen? Why is one not converted and another is?
Thanks for explaining!