I need to convert a LocalDateTime to UTC date, here is my code
LocalDateTime ldt = LocalDateTime.now();
System.out.println("Local date Time " +ldt);
ZonedDateTime ldtZoned = ldt.atZone(ZoneId.systemDefault());
ZonedDateTime utcZoned = ldtZoned.withZoneSameInstant(ZoneOffset.UTC);
System.out.println("UTC Zone"+utcZoned);
Instant i = utcZoned.toInstant();
System.out.println("Instant "+i);
Date out = Date.from(i);
System.out.println("date from Instant "+ out);
The output comes as
Local date Time 2019-05-08T16:23:53.109
UTC Zone2019-05-08T22:23:53.109Z
Instant 2019-05-08T22:23:53.109Z
date from Instant Wed May 08 16:23:53 MDT 2019
Till 3rd statement all is good. But when we get Date object from Instant it converts the Date object back to Local Time.
How can we get Date object as UTC from LocaldateTime??