Java 8 – How to convert String to LocalDateTime ?

In this article, we will learn how to convert String to LocalDateTime in Java 1.8 version using different date formats

Q) What is the need of converting String to LocalDateTime ?

  • 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 LocalDateTime
  • This article explains about String to LocalDateTime conversion in different formats

Read also,

1. String to LocalDateTime conversion:

We will use below formats to convert String to LocalDateTime,

  1. yyyy-MM-ddTHH:mm:ss.nnn (default)
  2. dd-MM-yyyy hh:mm:ss.nnn a
  3. dd.MM.yyyy HH:mm:ss
  4. dd/MMM/yyyy hh:mm:ss a
  5. E, MMM dd yyyy HH:mm
  6. MMM dd, yyyy hh:mm a
  7. MM/dd, yyyy hh:mm a

Note: for creating above formats we need DateTimeFormatter

2. Convert String to LocalDateTime examples:

2.1 Convert String in default (yyyy-MM-ddTHH:mm:ss.nnn) format to LocalDateTime :

ConvertStringToLocalDateTime1.java

package in.bench.resources.java8.localdatetime.examples;

import java.time.LocalDateTime;

public class ConvertStringToLocalDateTime1 {

	public static void main(String[] args) {

		// 1. string
		String strDateTime = "2017-05-29T19:07:34.190912345";


		// 2. parse date/time in String to default LocalDateTime format
		LocalDateTime localDate = LocalDateTime.parse(strDateTime);


		// 3. print to console
		System.out.println("Original String :- \n" + strDateTime);
		System.out.print("\nString in default (yyyy-MM-ddTHH:mm:ss.nnn) format "
				+ "to LocalDateTime :- \n" 
				+ localDate);
	}
}

Output:

Original String :- 
2017-05-29T19:07:34.190912345

String in default (yyyy-MM-ddTHH:mm:ss.nnn) format to LocalDateTime :- 
2017-05-29T19:07:34.190912345

2.2 Convert String in (dd-MM-yyyy hh:mm:ss.nnn a) format to LocalDateTime :

ConvertStringToLocalDateTime2.java

package in.bench.resources.java8.localdatetime.examples;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertStringToLocalDateTime2 {

	public static void main(String[] args) {

		// 1. string
		String strDateTime = "29-05-2017 05:07:34.190912345 pm";


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter
				.ofPattern("dd-MM-yyyy hh:mm:ss.nnn a");


		// 3. parse date in String format using above dateTimeFormatter
		LocalDateTime localDate = LocalDateTime
				.parse(strDateTime, dateTimeFormatter);


		// 4. print to console
		System.out.println("Original String :- \n" + strDateTime);
		System.out.print("\nString in (dd-MM-yyyy hh:mm:ss.nnn a) format "
				+ "to LocalDateTime  :- \n" 
				+ localDate);
	}
}

Output:

Original String :- 
29-05-2017 05:07:34.190912345 pm

String in (dd-MM-yyyy hh:mm:ss.nnn a) format to LocalDateTime  :- 
2017-05-29T17:07:34.190912345

2.3 Convert String in (dd.MM.yyyy HH:mm:ss) format to LocalDateTime :

ConvertStringToLocalDateTime3.java

package in.bench.resources.java8.localdatetime.examples;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertStringToLocalDateTime3 {

	public static void main(String[] args) {

		// 1. string
		String strDateTime = "29.05.2017 10:07:34";


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter
				.ofPattern("dd.MM.yyyy HH:mm:ss");


		// 3. parse date in String format using above dateTimeFormatter
		LocalDateTime localDate = LocalDateTime
				.parse(strDateTime, dateTimeFormatter);


		// 4. print to console
		System.out.println("Original String :- \n" + strDateTime);
		System.out.print("\nString in (dd.MM.yyyy HH:mm:ss) format "
				+ "to LocalDateTime  :- \n" 
				+ localDate);
	}
}

Output:

Original String :- 
29.05.2017 10:07:34

String in (dd.MM.yyyy HH:mm:ss) format to LocalDateTime  :- 
2017-05-29T10:07:34

2.4 Convert String in (dd/MMM/yyyy hh:mm:ss a) format to LocalDateTime :

ConvertStringToLocalDateTime4.java

package in.bench.resources.java8.localdatetime.examples;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertStringToLocalDateTime4 {

	public static void main(String[] args) {

		// 1. string
		String strDateTime = "29/May/2017 10:07:34 pm";


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter
				.ofPattern("dd/MMM/yyyy hh:mm:ss a");


		// 3. parse date in String format using above dateTimeFormatter
		LocalDateTime localDate = LocalDateTime
				.parse(strDateTime, dateTimeFormatter);


		// 4. print to console
		System.out.println("Original String :- \n" + strDateTime);
		System.out.print("\nString in (dd/MMM/yyyy hh:mm:ss a) format "
				+ "to LocalDateTime  :- \n" 
				+ localDate);
	}
}

Output:

Original String :- 
29/May/2017 10:07:34 pm

String in (dd/MMM/yyyy hh:mm:ss a) format to LocalDateTime  :- 
2017-05-29T22:07:34

2.5 Convert String in (E, MMM dd yyyy HH:mm) format to LocalDateTime :

ConvertStringToLocalDateTime5.java

package in.bench.resources.java8.localdatetime.examples;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertStringToLocalDateTime5 {

	public static void main(String[] args) {

		// 1. string
		String strDateTime = "Tue, Aug 09 2022 11:17";


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter
				.ofPattern("E, MMM dd yyyy HH:mm");


		// 3. parse date in String format using above dateTimeFormatter
		LocalDateTime localDate = LocalDateTime
				.parse(strDateTime, dateTimeFormatter);


		// 4. print to console
		System.out.println("Original String :- \n" + strDateTime);
		System.out.print("\nString in (E, MMM dd yyyy HH:mm) format "
				+ "to LocalDateTime  :- \n" 
				+ localDate);
	}
}

Output:

Original String :- 
Tue, Aug 09 2022 11:17

String in (E, MMM dd yyyy HH:mm) format to LocalDateTime  :- 
2022-08-09T11:17

2.6 Convert String in (MMM dd, yyyy hh:mm a) format to LocalDateTime :

ConvertStringToLocalDateTime6.java

package in.bench.resources.java8.localdatetime.examples;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertStringToLocalDateTime6 {

	public static void main(String[] args) {

		// 1. string
		String strDateTime = "Aug 09, 2022 11:17 pm";


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter
				.ofPattern("MMM dd, yyyy hh:mm a");


		// 3. parse date in String format using above dateTimeFormatter
		LocalDateTime localDate = LocalDateTime
				.parse(strDateTime, dateTimeFormatter);


		// 4. print to console
		System.out.println("Original String :- \n" + strDateTime);
		System.out.print("\nString in (MMM dd, yyyy hh:mm a) format "
				+ "to LocalDateTime  :- \n" 
				+ localDate);
	}
}

Output:

Original String :- 
Aug 09, 2022 11:17 pm

String in (MMM dd, yyyy hh:mm a) format to LocalDateTime  :- 
2022-08-09T23:17

2.7 Convert String in (MM/dd, yyyy hh:mm a) format to LocalDateTime :

ConvertStringToLocalDateTime7.java

package in.bench.resources.java8.localdatetime.examples;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertStringToLocalDateTime7 {

	public static void main(String[] args) {

		// 1. string
		String strDateTime = "08/09, 2022 11:17 pm";


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter
				.ofPattern("MM/dd, yyyy hh:mm a");


		// 3. parse date in String format using above dateTimeFormatter
		LocalDateTime localDate = LocalDateTime
				.parse(strDateTime, dateTimeFormatter);


		// 4. print to console
		System.out.println("Original String :- \n" + strDateTime);
		System.out.print("\nString in (MM/dd, yyyy hh:mm a) format "
				+ "to LocalDateTime  :- \n" 
				+ localDate);
	}
}

Output:

Original String :- 
08/09, 2022 11:17 pm

String in (MM/dd, yyyy hh:mm a) format to LocalDateTime  :- 
2022-08-09T23:17

3. DateTimeParseException:

  • If the specified String doesn’t match with the specified pattern/format then DateTimeParseException is thrown
  • Use below date/time notation,
    1. a – for am/pm marker
    2. z – regional time-zone like IST or PST or CET
    3. XXX – for offset from GMT/UTC
    4. ss – seconds
    5. mm – minutes
    6. hh – 12-hour format
    7. HH – 24 hour format
    8. y/yyyy– year with all 4 digits
    9. yy – year with last 2 digits
    10. MM – month in numbers
    11. MMM – month in words in short form
    12. MMMM – month in words in full form
    13. dd – date
    14. E/EEE – day of week in short form
    15. EEEE – day of week in full form

StringToLocalDateTimeConversion.java

package in.bench.resources.java8.localdatetime.examples;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class StringToLocalDateTimeConversion {

	public static void main(String[] args) {

		// 1. string
		String strDateTime = "16-02-1987 10:07:34";


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter
				.ofPattern("dd/MM/yyyy HH:mm:ss");


		// 3. parse date in String format using above dateTimeFormatter
		LocalDateTime localDate = LocalDateTime
				.parse(strDateTime, dateTimeFormatter);


		// 4. print to console
		System.out.println("Original String :- \n" + strDateTime);
		System.out.println("\nString in (dd/MM/yyyy) format to LocalDate  :- \n" 
				+ localDate);
	}
}

Output:

Exception in thread "main" java.time.format.DateTimeParseException: 
Text '16-02-1987 10:07:34' could not be parsed at index 2
	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.LocalDateTime.parse(LocalDateTime.java:494)
	at in.bench.resources.java8.localdatetime.examples.StringToLocalDateTimeConversion
.main(StringToLocalDateTimeConversion.java:21)

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalDateTime in different formats ?
Java 8 – How to parse LocalDateTime in String form ?