$show=/label

Java Convert String to Date (Java 8 LocalDate.parse() Examples)

SHARE:

A quick guide to converting String to Date in java And also example programs using Java 8 new Date API.

How To Convert String to Date In Java

1. Overview


In this article, you'll be learning how to convert String to Date in Java 8 as well as with Java 8 new DateTime API.

String date conversion can be done by calling the parse() method of LocalDate, DateFormat and SimpleDateFormat classes.

LocalDate is introduced in java 8 enhancements.

DateFormat is an abstract class and direct subclass is SimpleDateFormat.

To convert a String to Date, first need to create the SimpleDateFormat or LocalDate object with the data format and next call parse() method to produce the date object with the contents of string date.

Java Convert String to Date (Java 8 LocalDate.parse() Examples)


First, let us write examples on string to date conversations with SimpleDateFormat classs and then next with DateFormat class.

At last, Finally with LocalDate class parse() method.

All of the following classes are used to define the date format of input string. Simply to create the date formats.

LocalDate
DateFormat
SimpleDateFormat

Note: In date format Captial M indicates month whereas lowercase m indicate minutes.

This will be asked in the interview or written test on How to Format Date to String in Java 8?

2. How To Convert String To Date yyyy-MM-dd - SimpleDateFormat


The input string can be any format but all should be valid date formats.

So, First need to pass the date format of string to SimpleDateFormat class and then call parse() method of it. This method returns the String date in Date object format.

See the below example.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleFormatStringToDateExample1 {

	public static void main(String[] args) throws ParseException {
		
		
		String string = "2020-01-01";
		
		System.out.println("Original string (that holds date value) : "+string);
		
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
		
		Date dateObject = dateFormatter.parse(string);
		
		System.out.println("Converted Date value : "+dateObject);

	}

}

Output:

Original string (that holds date value) : 2020-01-01
Converted Date value : Wed Jan 01 00:00:00 IST 2020

If the date format is not matched to the string value then it throws ParseException.

Original string (that holds date value) : 2020-01-01
Exception in thread "main" java.text.ParseException: Unparseable date: "2020-01-01"
	at java.text.DateFormat.parse(DateFormat.java:366)
	at SimpleFormatStringToDateExample.main(SimpleFormatStringToDateExample.java:16)

3. How to convert string to date in java in dd-MM-yyyy format


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleFormatStringToDateExample2 {

	public static void main(String[] args) throws ParseException {
		
		
		String string = "01-01-2020";
		
		System.out.println("Original string (that holds date value) dd-MM-yyyy : "+string);
		
		SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy");
		
		Date dateObject = dateFormatter.parse(string);
		
		System.out.println("Converted Date value : "+dateObject);

	}

}
Output:
Original string (that holds date value) dd-MM-yyyy : 01-01-2020
Converted Date value : Wed Jan 01 00:00:00 IST 2020

4. How to format the String to Date? Examples on multiple date formats


Below program shows on string date in various formats that converts to the Date object with SimpleDateFormat class parse() method.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MultipleFormatsStringToDateExample {

	public static void main(String[] args) throws ParseException {

		// string dates
		String stringDate1 = "31/01/2020";
		String stringDate2 = "31-Jan-2020";
		String stringDate3 = "12 31, 2020";
		String stringDate4 = "Thu, Jan 31 2020";
		String stringDate5 = "Thu, Jan 31 2020 23:37:50";
		String stringDate6 = "31-Jan-2020 23:37:50";
		
		// formatters
		SimpleDateFormat formatter1 = new SimpleDateFormat("dd/MM/yyyy");
		SimpleDateFormat formatter2 = new SimpleDateFormat("dd-MMM-yyyy");
		SimpleDateFormat formatter3 = new SimpleDateFormat("MM dd, yyyy");
		SimpleDateFormat formatter4 = new SimpleDateFormat("E, MMM dd yyyy");
		SimpleDateFormat formatter5 = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
		SimpleDateFormat formatter6 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
		
		// conversions
		Date date1 = formatter1.parse(stringDate1);
		Date date2 = formatter2.parse(stringDate2);
		Date date3 = formatter3.parse(stringDate3);
		Date date4 = formatter4.parse(stringDate4);
		Date date5 = formatter5.parse(stringDate5);
		Date date6 = formatter6.parse(stringDate6);
		
		System.out.println("String   -   Date values");
		System.out.println(stringDate1 + " - " + date1);
		System.out.println(stringDate2 + " - " + date2);
		System.out.println(stringDate3 + " - " + date3);
		System.out.println(stringDate4 + " - " + date4);
		System.out.println(stringDate5 + " - " + date5);
		System.out.println(stringDate6 + " - " + date6);

	}

}
Output:

String   -   Date values
31/01/2020 - Fri Jan 31 00:00:00 IST 2020
31-Jan-2020 - Fri Jan 31 00:00:00 IST 2020
12 31, 2020 - Thu Dec 31 00:00:00 IST 2020
Thu, Jan 31 2020 - Fri Jan 31 00:00:00 IST 2020
Thu, Jan 31 2020 23:37:50 - Fri Jan 31 23:37:50 IST 2020
31-Jan-2020 23:37:50 - Fri Jan 31 23:37:50 IST 2020

6. How to convert string to date in Java 8 with LocalDate Predefined Formatters


Java 8 api DateTimeFormatter class is added with a set of predefiend formatters which are frequently used by the developers and those are the industry standards.

This class helps to get the right formatters. Next pass the string date, foramter to LocalDate.parse() method which returns the date object.

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class StringToDateExampleWithLocalDateWithPredefinedFormatters {

	public static void main(String[] args) throws ParseException {

		// string dates
		String isoDateInString = "2020-07-20";
		
		DateTimeFormatter iso_date = DateTimeFormatter.ISO_DATE;
		
		LocalDate date = LocalDate.parse(isoDateInString, iso_date);
		
		System.out.println("Locale Date : "+date);
		
	}

}
Output:

Locale Date : 2020-07-20
LocalDate.parse() method does the conversion the given string with the given formatter. Here, used ISO_DATE date formater which produces the date as "2020-07-20".

If the formatter is not matching to the string value then it throws the runtime exception. Change the formatter different as below.
DateTimeFormatter iso_date = DateTimeFormatter.BASIC_ISO_DATE;
Error:

Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-07-20' could not be parsed at index 4
	at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
	at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
	at java.time.LocalDate.parse(LocalDate.java:400)
	at StringToDateExampleWithLocalDateWithPredefinedFormatters.main(StringToDateExampleWithLocalDateWithPredefinedFormatters.java:17)

7. How to convert string to date in Java 8 with LocalDate Custom Formatters


Now we are getting the date in the different format as "May 30, 2020" but for this, there is no predefined formatter. So now, How to convert the string to date object.

Let us use the DateTimeFormatter.ofPattern() method and pass the custom pattern as "MMM dd, yyyy".

See the simple program to string date conversion:


import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class StringToDateExampleWithLocalDateWithCustomFormatters {

	public static void main(String[] args) throws ParseException {

		// string dates
		String isoDateInString = "May 30, 2020";
		
		// custom formatter
		DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("MMM d, yyyy");
		
		// string to date conversion in java 8
		LocalDate date = LocalDate.parse(isoDateInString, customFormatter);
		
		// printing the date object
		System.out.println("Locale Date : "+date);
		
	}

}
Output:
Locale Date : 2020-05-30

8. Java 8 DateTimeFormatter VS SimpleDateFormat


A simple program that shows the differences between DateTimeFormatter VS SimpleDateFormat.

Java 8 api returns the LocalDate object as exact same as string content but SimpleDateFormat produces the Date objet which shows the full date with seconds by default even though time information is not provided.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class StringToDateExampleWithLocalDateWithCustomFormatters {

	public static void main(String[] args) throws ParseException {

		// string dates
		String isoDateInString = "May 30, 2020";
		
		// Java 8 DateTimeFormatter 
		// custom formatter
		DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("MMM d, yyyy");
		
		// string to date conversion in java 8
		LocalDate date = LocalDate.parse(isoDateInString, customFormatter);
		
		// printing the date object
		System.out.println("Java 8 DateTimeFormatter "+date);
		
		
		// Before java 8 - SimpleDateFormat
		
		SimpleDateFormat simpleCustomFormatter = new SimpleDateFormat("MMM d, yyyy");
		
		Date simpleDate = simpleCustomFormatter.parse(isoDateInString);
		
		System.out.println("SimpleDateFomat : "+simpleDate);
		
	}

}
Output:

Java 8 DateTimeFormatter 2020-05-30
SimpleDateFomat : Sat May 30 00:00:00 IST 2020

8. Conclusion


In this article, you've seen how to convert the String into Date using SimpleDateFormat.parse() and LocalDate.parse() method in Java 8.

Examples on various date formats to convert string to date.

And also Example programs on Java 8 predefined formats and how to use the custom date formats in java 8.

Finally, Differences between Java 8 DateTimeFormatter VS SimpleDateFormat.

As usual, all programs are over GitHub.

COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,16,Arrays,14,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,5,Collections,22,Collector,1,Command Line,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,81,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,4,Dictionary,1,Difference,1,Download,1,Eclipse,2,Efficiently,1,Error,1,Errors,1,Exception,1,Exceptions,3,Fast,1,Files,9,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,4,Grant,1,Grep,1,HashMap,1,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,5,Iterate,2,Jackson API,3,Java,30,Java 10,1,Java 11,5,Java 12,5,Java 13,2,Java 14,2,Java 8,66,Java 8 Difference,2,Java 8 Stream Conversions,2,java 8 Stream Examples,3,Java 9,1,Java Conversions,11,Java Design Patterns,1,Java Files,1,Java Program,2,Java Programs,72,java.lang,5,java.util. function,1,jQuery,1,Kotlin,10,Kotlin Conversions,3,Kotlin Programs,6,Lambda,1,lang,29,Leap Year,1,live updates,1,Logging,1,Mac OS,2,Math,1,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,1,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,21,String,58,String Programs,10,String Revese,1,Swing,1,System,1,Tags,1,Threads,10,Tomcat,1,Tomcat 8,1,Troubleshoot,16,Unix,2,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java Convert String to Date (Java 8 LocalDate.parse() Examples)
Java Convert String to Date (Java 8 LocalDate.parse() Examples)
A quick guide to converting String to Date in java And also example programs using Java 8 new Date API.
https://1.bp.blogspot.com/-_7MV_l1YFe0/Xx09JsjiXPI/AAAAAAAAC3A/M8lHT8wLa6sQ3yYscnfvrJRvA0_Ze1A-gCLcBGAsYHQ/w400-h193/Java%2BConvert%2BString%2Bto%2BDate%2B%2528Java%2B8%2BLocalDate.parse%2528%2529%2BExamples%2529.png
https://1.bp.blogspot.com/-_7MV_l1YFe0/Xx09JsjiXPI/AAAAAAAAC3A/M8lHT8wLa6sQ3yYscnfvrJRvA0_Ze1A-gCLcBGAsYHQ/s72-w400-c-h193/Java%2BConvert%2BString%2Bto%2BDate%2B%2528Java%2B8%2BLocalDate.parse%2528%2529%2BExamples%2529.png
JavaProgramTo.com
https://www.javaprogramto.com/2020/07/java-convert-string-to-date-java-8.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/07/java-convert-string-to-date-java-8.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content