In this article, we will learn how to convert String to ZonedDateTime using different date/time formats in Java 1.8 version
Q) What is the need of converting String to ZonedDateTime ?
- Generally, whenever we receive any data from web application then it is passed in the form of String
- But for further processing we need to convert String to ZonedDateTime
- This article explains about String to ZonedDateTime conversion in different formats
Read also,
- String to Date conversion in different formats
- Date to String conversion is also required for reverse process
- For ZonedDateTime to String conversion, read Java 8 – How to convert ZonedDateTime to String ?
1. String to ZonedDateTime conversion:
We will use below formats to convert String to ZonedDateTime,
- yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm[Region/City] (default)
- dd-MM-yyyy HH:mm:ss.nnn VV
- dd-MM-yyyy hh:mm:ss.nnn a VV
- dd.MM.yyyy HH:mm:ss VV
- dd-MMM-yyyy hh:mm:ss a VV
- E dd/MMMM/yyyy HH:mm VV
- EEEE MMMM dd, yyyy hh:mm a VV
Note: for creating above formats we need DateTimeFormatter
2. Convert String to ZonedDateTime examples:
2.1 Convert String in default (yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm[Region/City]) format to ZonedDateTime :
- This example converts String in default ISO_ZONED_DATE_TIME format to ZonedDateTime
ConvertStringToZonedDateTime1.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
public class ConvertStringToZonedDateTime1 {
public static void main(String[] args) {
// 1. string
String strZonedDateTime = "2017-05-29T19:07:34.190912345+05:30[Asia/Calcutta]";
// 2. parse date/time in String to default ZonedDateTime format
ZonedDateTime zonedDateTime = ZonedDateTime.parse(strZonedDateTime);
// 3. print to console
System.out.println("Original String :- \n" + strZonedDateTime);
System.out.print("\nString in default "
+ "(yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm[Region/City]) format to ZonedDateTime :- \n"
+ zonedDateTime);
}
}
Output:
Original String :-
2017-05-29T19:07:34.190912345+05:30[Asia/Calcutta]
String in default (yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm[Region/City]) format to ZonedDateTime :-
2017-05-29T19:07:34.190912345+05:30[Asia/Calcutta]
2.2 Convert String in (dd-MM-yyyy HH:mm:ss.nnn VV) format to ZonedDateTime :
- This example converts String in (dd-MM-yyyy HH:mm:ss.nnn VV) format to ZonedDateTime
ConvertStringToZonedDateTime2.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ConvertStringToZonedDateTime2 {
public static void main(String[] args) {
// 1. string
String strZonedDateTime = "29-06-2017 19:07:34.190912345 Asia/Calcutta";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("dd-MM-yyyy HH:mm:ss.nnn VV");
// 2. parse date/time in String to default ZonedDateTime format
ZonedDateTime zonedDateTime = ZonedDateTime.parse(strZonedDateTime, dateTimeFormatter);
// 3. print to console
System.out.println("Original String :- \n" + strZonedDateTime);
System.out.print("\nString in "
+ "(dd-MM-yyyy HH:mm:ss.nnn VV) format to ZonedDateTime :- \n"
+ zonedDateTime);
}
}
Output:
Original String :-
29-06-2017 19:07:34.190912345 Asia/Calcutta
String in (dd-MM-yyyy HH:mm:ss.nnn VV) format to ZonedDateTime :-
2017-06-29T19:07:34.190912345+05:30[Asia/Calcutta]
2.3 Convert String in (dd-MM-yyyy hh:mm:ss.nnn a VV) format to ZonedDateTime :
- This example converts String in (dd-MM-yyyy hh:mm:ss.nnn a VV) format to ZonedDateTime
ConvertStringToZonedDateTime3.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ConvertStringToZonedDateTime3 {
public static void main(String[] args) {
// 1. string
String strZonedDateTime = "29-06-2017 07:07:34.190912345 pm Asia/Calcutta";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("dd-MM-yyyy hh:mm:ss.nnn a VV");
// 2. parse date/time in String to default ZonedDateTime format
ZonedDateTime zonedDateTime = ZonedDateTime.parse(strZonedDateTime, dateTimeFormatter);
// 3. print to console
System.out.println("Original String :- \n" + strZonedDateTime);
System.out.print("\nString in "
+ "(dd-MM-yyyy hh:mm:ss.nnn a VV) format to ZonedDateTime :- \n"
+ zonedDateTime);
}
}
Output:
Original String :-
29-06-2017 07:07:34.190912345 pm Asia/Calcutta
String in (dd-MM-yyyy hh:mm:ss.nnn a VV) format to ZonedDateTime :-
2017-06-29T19:07:34.190912345+05:30[Asia/Calcutta]
2.4 Convert String in (dd.MM.yyyy HH:mm:ss VV) format to ZonedDateTime:
- This example converts String in (dd.MM.yyyy HH:mm:ss VV) format to ZonedDateTime
ConvertStringToZonedDateTime4.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ConvertStringToZonedDateTime4 {
public static void main(String[] args) {
// 1. string
String strZonedDateTime = "29.06.2017 19:07:34 Asia/Dubai";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("dd.MM.yyyy HH:mm:ss VV");
// 2. parse date/time in String to default ZonedDateTime format
ZonedDateTime zonedDateTime = ZonedDateTime.parse(strZonedDateTime, dateTimeFormatter);
// 3. print to console
System.out.println("Original String :- \n" + strZonedDateTime);
System.out.print("\nString in "
+ "(dd.MM.yyyy HH:mm:ss VV) format to ZonedDateTime :- \n"
+ zonedDateTime);
}
}
Output:
Original String :-
29.06.2017 19:07:34 Asia/Dubai
String in (dd.MM.yyyy HH:mm:ss VV) format to ZonedDateTime :-
2017-06-29T19:07:34+04:00[Asia/Dubai]
2.5 Convert String in (dd-MMM-yyyy hh:mm:ss a VV) format to ZonedDateTime :
- This example converts String in (dd-MMM-yyyy hh:mm:ss a VV) format to ZonedDateTime
ConvertStringToZonedDateTime5.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ConvertStringToZonedDateTime5 {
public static void main(String[] args) {
// 1. string
String strZonedDateTime = "29-Jun-2017 09:07:34 pm Asia/Dubai";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("dd-MMM-yyyy hh:mm:ss a VV");
// 2. parse date/time in String to default ZonedDateTime format
ZonedDateTime zonedDateTime = ZonedDateTime.parse(strZonedDateTime, dateTimeFormatter);
// 3. print to console
System.out.println("Original String :- \n" + strZonedDateTime);
System.out.print("\nString in "
+ "(dd-MMM-yyyy hh:mm:ss a VV) format to ZonedDateTime :- \n"
+ zonedDateTime);
}
}
Output:
Original String :-
29-Jun-2017 09:07:34 pm Asia/Dubai
String in (dd-MMM-yyyy hh:mm:ss a VV) format to ZonedDateTime :-
2017-06-29T21:07:34+04:00[Asia/Dubai]
2.6 Convert String in (E dd/MMMM/yyyy HH:mm VV) format to ZonedDateTime :
- This example converts String in (E dd/MMMM/yyyy HH:mm VV) format to ZonedDateTime
ConvertStringToZonedDateTime6.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ConvertStringToZonedDateTime6 {
public static void main(String[] args) {
// 1. string
String strZonedDateTime = "Fri 28/June/2019 15:07 Europe/Paris";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("E dd/MMMM/yyyy HH:mm VV");
// 2. parse date/time in String to default ZonedDateTime format
ZonedDateTime zonedDateTime = ZonedDateTime.parse(strZonedDateTime, dateTimeFormatter);
// 3. print to console
System.out.println("Original String :- \n" + strZonedDateTime);
System.out.print("\nString in "
+ "(E dd/MMMM/yyyy HH:mm VV) format to ZonedDateTime :- \n"
+ zonedDateTime);
}
}
Output:
Original String :-
Fri 28/June/2019 15:07 Europe/Paris
String in (E dd/MMMM/yyyy HH:mm VV) format to ZonedDateTime :-
2019-06-28T15:07+02:00[Europe/Paris]
2.7 Convert String in (EEEE MMMM dd, yyyy hh:mm a VV) format to ZonedDateTime :
- This example converts String in (EEEE MMMM dd, yyyy hh:mm a VV) format to ZonedDateTime
ConvertStringToZonedDateTime7.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ConvertStringToZonedDateTime7 {
public static void main(String[] args) {
// 1. string
String strZonedDateTime = "Sunday June 23, 2019 11:07 pm Europe/Paris";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("EEEE MMMM dd, yyyy hh:mm a VV");
// 2. parse date/time in String to default ZonedDateTime format
ZonedDateTime zonedDateTime = ZonedDateTime.parse(strZonedDateTime, dateTimeFormatter);
// 3. print to console
System.out.println("Original String :- \n" + strZonedDateTime);
System.out.print("\nString in "
+ "(EEEE MMMM dd, yyyy hh:mm a VV) format to ZonedDateTime :- \n"
+ zonedDateTime);
}
}
Output:
Original String :-
Sunday June 23, 2019 11:07 pm Europe/Paris
String in (EEEE MMMM dd, yyyy hh:mm a VV) format to ZonedDateTime :-
2019-06-23T23:07+02:00[Europe/Paris]
3. DateTimeParseException:
- If the specified String doesn’t match with the specified pattern/format then DateTimeParseException is thrown
- Use below date/time notation,
- a – for am/pm marker
- z – regional time-zone like IST or PST or CET
- XXX – for offset from GMT/UTC
- ss – seconds
- mm – minutes
- hh – 12-hour format
- HH – 24 hour format
- y/yyyy– year with all 4 digits
- yy – year with last 2 digits
- MM – month in numbers
- MMM – month in words in short form
- MMMM – month in words in full form
- dd – date
- E/EEE – day of week in short form
- EEEE – day of week in full form
StringToZonedDateTimeConversion.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class StringToZonedDateTimeConversion {
public static void main(String[] args) {
// 1. string
String strZonedDateTime = "29-06-2017 19:07:34.190912345 Asia/Calcutta";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("dd-MMM-yyyy HH:mm:ss.nnn VV");
// 2. parse date/time in String to default ZonedDateTime format
ZonedDateTime zonedDateTime = ZonedDateTime.parse(strZonedDateTime, dateTimeFormatter);
// 3. print to console
System.out.println("Original String :- \n" + strZonedDateTime);
System.out.print("\nString in "
+ "(dd-MM-yyyy HH:mm:ss.nnn VV) format to ZonedDateTime :- \n"
+ zonedDateTime);
}
}
Output:
Exception in thread "main" java.time.format.DateTimeParseException:
Text '29-06-2017 19:07:34.190912345 Asia/Calcutta' could not be parsed at index 3
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:600)
at in.bench.resources.java8.zoneddatetime.examples.StringToZonedDateTimeConversion
.main(StringToZonedDateTimeConversion.java:20)
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/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.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 !!