-2

Example: 2021-07-26T12:06:00.000+0400 -> 2021-07-26T08:06:00.000UTC

I know I can do it manually but I was wondering if there any libraries that already offer this feature

  • 1
    Just use the ZonedDatetime Api, you don't need external libraries for that, you will have to write the same code and without using external libraries you have one source less of errors and maintainance – Javier Toja Jul 26 '21 at 11:04
  • 1
    Should be closed as duplicate. Please refer this thread for more eloborate answer https://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date – Jp Vinjamoori Jul 26 '21 at 11:17
  • 1
    Does this answer your question? [Converting ISO 8601-compliant String to java.util.Date](https://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date) – Jp Vinjamoori Jul 26 '21 at 11:19

1 Answers1

1

No need to introduce a new third-party dependency here.

Simply use Java 8's ZonedDateTime to perform exactly what you are looking for:-

// Define your date & date format.
String dateFormat = "dd-M-yyyy hh:mm:ss a";
String dateString = "26-7-2021 12:20:00 PM";
LocalDateTime localTime = LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern(dateFormat));

// Convert between time zones
ZonedDateTime newYorkTime = localTime.atZone(ZoneId.of("America/New_York"));
ZonedDateTime londonTime = localTime.atZone(ZoneId.of("Europe/London"));

You can find the Java API documentation & some more detailed examples here.

Daniel Scarfe
  • 256
  • 1
  • 7