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:
- Getting current date and time with Clock
- Convert LocalDate to Date in Java
- Convert LocalDateTime into Date in Java
- Convert java.util.Date to java.time.LocalDateTime Example
- Convert java.util.Date to java.time.LocalDate Example
- Duration Between Two LocalDate in Java
- Java Convert ZonedDateTime to String Example
- Convert String to ZonedDateTime in Java
- Compare ZonedDateTime Objects in Java
- How to Get Hour, Minute, Second from ZonedDateTime in Java
- Convert ZonedDateTime to LocalTime Example
- Convert ZonedDateTime to LocalDateTime in Java
- Java ZonedDateTime now() and of() Method Example
- Convert LocalDateTime to LocalTime in Java
- Convert LocalDateTime to LocalDate in Java
- Compare LocalDateTime Objects in Java
- Java LocalDateTime - Get Hour, Minute, and Second
- How to Get Day Month and Year from LocalDateTime in Java
- Java LocalDateTime now() Method Example
- How to Get Current Date Time in Java
- Convert LocalDate to String in Java
- Convert String to LocalDate in Java
- Java LocalDate isLeapYear() Method Example
- How to Get Number of Days from Month and Year using LocalDate in Java
- Compare Java LocalDate Objects Example
- How to Add Days Weeks Months and Years to LocalDate in Java
- How to get Day Month and Year from LocalDate in Java
- Java LocalDate now() Method Example - Get Current Date and Specific Date
- Convert LocalTime to String in Java
- Convert String to LocalTime in Java
- Java LocalTime isBefore() and isAfter() Method Example
- How to Compare Time in Java
- Java 8 - LocalTime getHour(), getMinute(), getSecond() and getNano() Example
- Java LocalTime now() Method Example - Get Current Time and Specific Time
- Java 8 Get Local Date Time in All Available Time Zones
- How to get the year, month, day, hours, minutes, seconds, and milliseconds of LocalDateTime in Java 8?
- How to get the year, month, day, hours, minutes, seconds, and milliseconds of Date in Java?
- Difference between Two Dates in Java
- Create LocalDateTime from LocalDate and LocalTime in Java
Comments
Post a Comment