Java — Convert a String to Date (LocalDate, LocalDateTime, ZonedDateTime, LocalTime) | Code Factory

Donate : Link

WordPress Blog : Link

Find several examples demonstrating how to convert a date represented as a String to a Date, LocalDate, LocalDateTime, ZonedDateTime, or LocalTime instance in Java.

How to convert a String to Date in Java using SimpleDateFormat

package com.example.java.programming.datetime;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author code.factory
*
*/
public class SimpleDateFormatExample {
public static void main(String... args) {
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = "2020-07-01";
try {
Date date = dateFormatter.parse(dateStr);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}

Output :

Wed Jul 01 00:00:00 IST 2020

Here is another example in which we parse a more complex date time representation :

package com.example.java.programming.datetime;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author code.factory
*
*/
public class SimpleDateFormatExample {
public static void main(String... args) {
SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("E, MMM dd yyyy, hh:mm:ss a");
String dateTimeStr = "Fri, Jul 01 2020, 03:30:45 PM";
try {
Date date = dateTimeFormatter.parse(dateTimeStr);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}

Output :

Wed Jul 01 15:30:45 IST 2020

Convert/Parse a String to LocalDate

The DateTime API has a DateTimeFormatter class that can be used to define the date time format and parse the String according to the specified date time format. The DateTimeFormatter class uses the following patterns and symbols :

package com.example.java.programming.datetime;import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/**
* @author code.factory
*
*/
public class LocalDateParseExample {
public static void main(String... args) {
LocalDate date1 = LocalDate.parse("2020-07-01");
System.out.println(date1);
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date2 = LocalDate.parse("01/07/2020", dateFormatter);
System.out.println(date2);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("E, MMM dd yyyy, hh:mm:ss a");
LocalDate date3 = LocalDate.parse("Wed, Jul 01 2020, 10:20:50 PM", dateTimeFormatter);
System.out.println(date3);
}
}

Output :

2020-07-01
2020-07-01
2020-07-01

Convert/Parse a String to LocalDateTime

package com.example.java.programming.datetime;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author code.factory
*
*/
public class LocalDateParseExample {
public static void main(String... args) {
LocalDateTime dateTime1 = LocalDateTime.parse("2020-07-01T10:15:20");
System.out.println(dateTime1);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MMM dd yyyy, HH:mm");
LocalDateTime dateTime2 = LocalDateTime.parse("Jul 01 2020, 10:20", dateTimeFormatter);
System.out.println(dateTime2);
DateTimeFormatter descriptiveDateTimeFormatter = DateTimeFormatter.ofPattern("E, MMM dd yyyy, hh:mm:ss a");
LocalDateTime dateTime3 = LocalDateTime.parse("Wed, Jul 01 2020, 10:30:50 AM", descriptiveDateTimeFormatter);
System.out.println(dateTime3);
}
}

Output :

2020-07-01T10:15:20
2020-07-01T10:20
2020-07-01T10:30:50

Convert/Parse a String to LocalTime

package com.example.java.programming.datetime;import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* @author code.factory
*
*/
public class LocalTimeParseExample {
public static void main(String... args) {
LocalTime time1 = LocalTime.parse("10:30:50");
System.out.println(time1);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
LocalTime time2 = LocalTime.parse("01/07/2020 08:16:32", dateTimeFormatter);
System.out.println(time2);
}
}

Output :

10:30:50
08:16:32

Convert/Parse a String to ZonedDateTime

package com.example.java.programming.datetime;import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author code.factory
*
*/
public class ZonedDateTimeParseExample {
public static void main(String... args) {
ZonedDateTime dateTime1 = ZonedDateTime.parse("2020-07-01T10:20:30+01:00[Europe/Paris]");
System.out.println(dateTime1);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MMM dd yyyy, HH:mm (VV)");
ZonedDateTime dateTime2 = ZonedDateTime.parse("Jul 01 2020, 20:40 (America/Los_Angeles)", dateTimeFormatter);
System.out.println(dateTime2);
}
}

Output :

2020-07-01T10:20:30+02:00[Europe/Paris]
2020-07-01T20:40-07:00[America/Los_Angeles]