Java 8 – How to parse ZonedDateTime in String form ?

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 :

  1. 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
  2. There are 2 variants of ZonedDateTime.parse() method –
    1. ZonedDateTime.parse(CharSequence) – returns ZonedDateTime in default yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm[Region/City] format
    2. ZonedDateTime.parse(CharSequence, DateTimeFormatter) – this variant helps to customize ZonedDateTime format using formatter
  3. 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
  4. 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
  5. 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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert String to ZonedDateTime ?
Java 8 – How to form ZonedDateTime passing Instant and ZoneId ?