Convert String to ZonedDateTime in Java

The Java 8 ZonedDateTime class provides the below APIs to convert or parse String to ZonedDateTime in java.
  • static ZonedDateTime parse(CharSequence text) - Obtains an instance of ZonedDateTime from a text string such as 2007-12-03T10:15:30+01:00[Europe/Paris].
  • static ZonedDateTime parse(CharSequence text, DateTimeFormatter formatter) - Obtains an instance of ZonedDateTime from a text string using a specific formatter.

Java Convert String to ZonedDateTime Example

The below example demonstrates the how-to convert String to ZonedDateTime class in Java 8:
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Program to demonstrate ZonedDateTime Class APIs.
 * @author javaguides.net
 *
 */
public class ZonedDateTimeExample {
 
    public static void main(String[] args) {
        convertOrParseStringtoZonedDateTime();
    }

    private static void convertOrParseStringtoZonedDateTime() {
         // ISO date time
        ZonedDateTime dt1 = ZonedDateTime.parse("2017-03-28T12:25:38.492+05:30[Asia/Calcutta]",
        DateTimeFormatter.ISO_ZONED_DATE_TIME);
        System.out.println(dt1);

        // 'yyyy-MMM-dd HH:mm:ss' pattern
        ZonedDateTime dt2 = ZonedDateTime.parse("2017-May-02 23:35:05",
        DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss").withZone(ZoneId.of("UTC")));
        System.out.println(dt2);

       // 'yyyy-MM-dd KK:mm:ss a' pattern
        ZonedDateTime dt3 = ZonedDateTime.parse("2017-05-30 10:20:30 AM",
        DateTimeFormatter.ofPattern("yyyy-MM-dd KK:mm:ss a").withZone(ZoneId.systemDefault()));
        System.out.println(dt3);

       // 'cccc, MMMM dd, yyyy KK:mm a' pattern
       ZonedDateTime dt4 = ZonedDateTime.parse("Wednesday, May 31, 2017 10:21 PM",
       DateTimeFormatter.ofPattern("cccc, MMMM dd, yyyy KK:mm a").withZone(ZoneId.of("Europe/Paris")));
       System.out.println(dt4);
    }
}
Output:
2017-03-28T12:25:38.492+05:30[Asia/Calcutta]
2017-05-02T23:35:05Z[UTC]
2017-05-30T10:20:30+05:30[Asia/Calcutta]
2017-05-31T22:21+02:00[Europe/Paris]

Reference

Related Java 8 Date-Time API Source Code Examples:

  1. Getting current date and time with Clock
  2. Convert LocalDate to Date in Java
  3. Convert LocalDateTime into Date in Java
  4. Convert java.util.Date to java.time.LocalDateTime Example
  5. Convert java.util.Date to java.time.LocalDate Example
  6. Duration Between Two LocalDate in Java
  7. Java Convert ZonedDateTime to String Example
  8. Convert String to ZonedDateTime in Java
  9. Compare ZonedDateTime Objects in Java
  10. How to Get Hour, Minute, Second from ZonedDateTime in Java
  11. Convert ZonedDateTime to LocalTime Example
  12. Convert ZonedDateTime to LocalDateTime in Java
  13. Java ZonedDateTime now() and of() Method Example
  14. Convert LocalDateTime to LocalTime in Java
  15. Convert LocalDateTime to LocalDate in Java
  16. Compare LocalDateTime Objects in Java
  17. Java LocalDateTime - Get Hour, Minute, and Second
  18. How to Get Day Month and Year from LocalDateTime in Java
  19. Java LocalDateTime now() Method Example
  20. How to Get Current Date Time in Java
  21. Convert LocalDate to String in Java
  22. Convert String to LocalDate in Java
  23. Java LocalDate isLeapYear() Method Example
  24. How to Get Number of Days from Month and Year using LocalDate in Java
  25. Compare Java LocalDate Objects Example
  26. How to Add Days Weeks Months and Years to LocalDate in Java
  27. How to get Day Month and Year from LocalDate in Java
  28. Java LocalDate now() Method Example - Get Current Date and Specific Date
  29. Convert LocalTime to String in Java
  30. Convert String to LocalTime in Java
  31. Java LocalTime isBefore() and isAfter() Method Example
  32. How to Compare Time in Java
  33. Java 8 - LocalTime getHour(), getMinute(), getSecond() and getNano() Example
  34. Java LocalTime now() Method Example - Get Current Time and Specific Time
  35. Java 8 Get Local Date Time in All Available Time Zones
  36. How to get the year, month, day, hours, minutes, seconds, and milliseconds of LocalDateTime in Java 8?
  37. How to get the year, month, day, hours, minutes, seconds, and milliseconds of Date in Java?
  38. Difference between Two Dates in Java
  39. Create LocalDateTime from LocalDate and LocalTime in Java

Comments