In this article, we will learn how to parse ZonedDateTime and java.util.Date in String form using ZonedDateTime.parse() method provided in Java 1.8 version
1. Parse java.time.ZonedDateTime :
- Sometimes, we need to parse date/time passed in String-form to ZonedDateTime, for that we can use ZonedDateTime.parse() method which will return ZonedDateTime in yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm[Region/City] format
- There are 2 variants of ZonedDateTime.parse() method –
- ZonedDateTime.parse(CharSequence) – returns ZonedDateTime in default yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm[Region/City] format
- ZonedDateTime.parse(CharSequence, DateTimeFormatter) – this variant helps to customize ZonedDateTime format using formatter
- Both variants throws java.time.format.DateTimeParseException when,
- For the 1st variant, if the specified date/time in string-form isn’t in default formats as listed below,
- yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm[Region/City]
- yyyy-MM-ddTHH:mm:ss+/-HH:mm[Region/City]
- yyyy-MM-ddTHH:mm+/-HH:mm[Region/City]
- For the 2nd variant, if specified date/time in string-form isn’t matching with custom pattern/format specified in the DateTimeFormatter
- For the 1st variant, if the specified date/time in string-form isn’t in default formats as listed below,
- If the given String is in default format then,
- DateTimeFormatter isn’t required
- Or else default formatter DateTimeFormatter.ISO_ZONED_DATE_TIME can be used
- Finally, print ZonedDateTime to the console for both variants
ParseZonedDateTime.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ParseZonedDateTime {
public static void main(String[] args) {
// 1. Office Joining date/time
String officeJoiningDateTime = "2017-03-06T08:29:59.987654321+05:30[Asia/Calcutta]";
// 1.1 convert/parse to dateInString to ZonedDateTime in default format
ZonedDateTime parsedOfficeJoiningDateTime = ZonedDateTime.parse(officeJoiningDateTime);
// 1.2 print to console
System.out.println("Original String is = " + officeJoiningDateTime);
System.out.println("Parsed Date/time is = " + parsedOfficeJoiningDateTime);
// 2. Office Relieving date/time
String officeRelievingDateTime = "2019-06-28T17:30:01.123456789+05:30[Asia/Calcutta]";
// 2.1 custom format
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ISO_ZONED_DATE_TIME;
// 2.2 convert/parse to dateInString to LocalDate in custom format
ZonedDateTime parsedOfficeRelievingDateTime = ZonedDateTime
.parse(officeRelievingDateTime, dateTimeFormatter2);
// 2.3 print to console
System.out.println("\nOriginal String is = " + officeRelievingDateTime);
System.out.println("Parsed Date/time is = " + parsedOfficeRelievingDateTime);
// 3. Session start date/time
String sessionStartDateTime = "29-06-2017 11:29:45 am Europe/Paris";
// 3.1 custom format
DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter
.ofPattern("dd-MM-yyyy HH:mm:ss a VV");
// 3.2 convert/parse to dateInString to LocalDate in custom format
ZonedDateTime parsedSessionStartDateTime = ZonedDateTime
.parse(sessionStartDateTime, dateTimeFormatter3);
// 3.3 print to console
System.out.println("\nOriginal String is = " + sessionStartDateTime);
System.out.println("Parsed Date/time is = " + parsedSessionStartDateTime);
// 4. Session end date/time
String sessionEndDateTime = "29.06.2017 05:59 pm +02:00 Europe/Paris";
// 4.1 custom format
DateTimeFormatter dateTimeFormatter4 = DateTimeFormatter
.ofPattern("dd.MM.yyyy hh:mm a VV VV");
// 4.2 convert/parse to dateInString to LocalDate in custom format
ZonedDateTime parsedSessionEndDateTime = ZonedDateTime
.parse(sessionEndDateTime, dateTimeFormatter4);
// 4.3 print to console
System.out.println("\nOriginal String is = " + sessionEndDateTime);
System.out.print("Parsed Date/time is = " + parsedSessionEndDateTime);
}
}
Output:
Original String is = 2017-03-06T08:29:59.987654321+05:30[Asia/Calcutta]
Parsed Date/time is = 2017-03-06T08:29:59.987654321+05:30[Asia/Calcutta]
Original String is = 2019-06-28T17:30:01.123456789+05:30[Asia/Calcutta]
Parsed Date/time is = 2019-06-28T17:30:01.123456789+05:30[Asia/Calcutta]
Original String is = 29-06-2017 11:29:45 am Europe/Paris
Parsed Date/time is = 2017-06-29T11:29:45+02:00[Europe/Paris]
Original String is = 29.06.2017 05:59 pm +02:00 Europe/Paris
Parsed Date/time is = 2017-06-29T17:59+02:00[Europe/Paris]
2. Parse java.util.Date :
- Default format for java.util.Date is E MMM dd HH:mm:ss zzz yyyy
- Parsing Date in custom formats is simple using DateFormat & SimpleDateFormat classes as shown in the below example
- Finally, print Date to the console for both default and custom formats
ParseDateTime.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ParseDateTime {
public static void main(String[] args) {
// 1. current date
Date date = new Date();
System.out.println("Date/Time in "
+ "default format :- \n" + date);
// 2. format
DateFormat dateFormat = new SimpleDateFormat("EEEE dd-MM-yyyy hh:mm:ss a z");
String str = dateFormat.format(date);
System.out.print("\nFormatted Date/Time in "
+ "(EEEE dd-MM-yyyy hh:mm:ss a z) format :- \n" + str);
}
}
Output:
Date/Time in default format :-
Fri Aug 12 10:30:20 IST 2022
Formatted Date/Time in (EEEE dd-MM-yyyy hh:mm:ss a z) format :-
Friday 12-08-2022 10:30:20 am IST
Related Articles:
- Java 8 – ZonedDateTime with method details and examples
- Java 8 – How to get Date, Time and Zone fields from ZonedDateTime ?
- Java 8 – How to form ZonedDateTime passing Date, Time and Zone fields ?
- Java 8 – How to form ZonedDateTime passing LocalDate, LocalTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing LocalDateTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing Instant and ZoneId ?
- Java 8 – How to parse ZonedDateTime in String form ?
- Java 8 – How to convert String to ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to String ?
- Java 8 – How to convert ZonedDateTime in different formats ?
- Java 8 – How to convert ZonedDateTime in different Format Style ?
- Java 8 – How to convert ZonedDateTime to LocalDateTime ?
- Java 8 – How to convert ZonedDateTime to an OffsetDateTime ?
- Java 8 – How to convert ZonedDateTime to an Instant ?
- Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert ZonedDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert ZonedDateTime to Calendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime in different ways ?
- Java 8 – How to add Date and Time fields to ZonedDateTime ?
- Java 8 – How to subtract Date and Time fields from ZonedDateTime ?
- Java 8 – How to alter Date, Time and Zone fields of ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is Before another ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is After another ZonedDateTime ?
- Java 8 – How to compare two ZonedDateTime instances ?
- Java 8 – How to find difference between two ZonedDateTime using Period & Duration ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/FormatStyle.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!