How to change the time based on timezone in LocalDateTime, here i have built a date with Time zone as EST, now i need to find the UTC of the corresponding time. please help me how to solve this
String str = "16Jun2015_153556";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMMyyyy_HHmmss");
formatter.withZone(ZoneId.of("EST5EDT"));
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
This answer might be somehow more structured than the correct answer of Jon Skeet. In my comment above I have also pointed out not to overlook the immutable nature of
DateTimeFormatter
so please always assign the result of any method prefixed by "with...()" to a variable of same type.Then you assign your local date-time to the EST-zone. It is safer to use the IANA-notation "America/New_York" than to use the outdated form "EST5EDT" (which is only supporting fixed dst rules without any history of historical raw offsets).
Finally you transform the intermediate global timestamp back to a local date-time at the offset UTC+00 preserving the same physical time: