Java 8 - How to convert String to LocalDate and LocalDate to String in specific format
This Java 8 code tip first shows how to convert
OUTPUT of the above code
Explanation of the code
OUTPUT of the above code
Explanation of the code
String
date to java.time.LocalDate
instance using the parse()
method of java.time.DateTimeFormatter
class. It then shows how to use the format()
method of LocalDate
class to convert LocalDate
instance to a String
in the specified format.Java 8 code to convert String to LocalDate
package com.javabrahman.java8.time;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class StringToLocalDate {
public static void main(String args[]){
//Converting String in format 'dd-MMM-yyyy' to LocalDate
String dateStr_1="28-Sep-2016";
DateTimeFormatter formatter_1=DateTimeFormatter.ofPattern("dd-MMM-yyyy");
LocalDate localDate_1= LocalDate.parse(dateStr_1,formatter_1);
System.out.println("Input String with value: "+dateStr_1);
System.out.println("Converted Date in default ISO format: "+localDate_1+"\n");
//Converting String in format 'EEEE, MMM d yyyy' to LocalDate
String dateStr_2="Wednesday, Sep 28 2016";
DateTimeFormatter formatter_2=DateTimeFormatter.ofPattern("EEEE, MMM d yyyy");
LocalDate localDate_2= LocalDate.parse(dateStr_2,formatter_2);
System.out.println("Input String with value: "+dateStr_2);
System.out.println("Converted Date in default ISO format: "+localDate_2+"\n");
//Converting String in format 'dd/MM/YY' to LocalDate
String dateStr_3="28/09/16";
DateTimeFormatter formatter_3=DateTimeFormatter.ofPattern("dd/MM/yy");
LocalDate localDate_3= LocalDate.parse(dateStr_3,formatter_3);
System.out.println("Input String with value: "+dateStr_3);
System.out.println("Converted Date in default ISO format: "+localDate_3);
}
}
Input String with value: 28-Sep-2016 Converted Date in default ISO format: 2016-09-28 Input String with value: Wednesday, Sep 28 2016 Converted Date in default ISO format: 2016-09-28 Input String with value: 28/09/16 Converted Date in default ISO format: 2016-09-28
- We start by creating a
String
with value28-Sep-2016
, nameddateStr_1
, which we then convert to aLocalDate
. - We need to convey to the Java 8 Date-Time API the format in which the date is being given to it inside
dateStr_1
. This format is specified using theDateTimeFormatter
class. We pass the format of the datedd-MMM-yyyy
toDateTimeFormatter
’s static method namedofPattern()
. ofPattern()
method returns an instance ofDateTimeFormatter
, namedformatter_1
, with the required date format set in it.- We then pass the
dateStr_1
andformatter_1
as inputs to theLocalDate.parse()
method which creates an instance ofLocalDate
, namedlocalDate_1
, with the value as 28th of September 2016. - We then print
localDate_1
which prints the date as2016-09-28
which is in the Standard ISO Format for dates. - Similarly, two other date formats are shown as examples to show how the date formats work resulting in dates
localDate_2
andlocalDate_3
. These are also printed in the default ISO format to show that theString
has been converted properly to its equivalentLocalDate
. - To print dates in formats other than the default ISO format, we will need to use the
LocalDate.format()
method. The next section of this article shows how to format aLocalDate
into formats other than the default ISO format.
Java 8 code to convert LocalDate to String
package com.javabrahman.java8.time;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
public class LocalDateToString {
public static void main(String args[]){
//We will use current date from the system clock
LocalDate today=LocalDate.now();
System.out.println("Current date using default toString(same as ISO Standard Format): "+today);
//Converting Date to a user specific format 1 - dd-MMM-yyyy
DateTimeFormatter formatter_1 = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
String format_1=(today).format(formatter_1);
System.out.println("Current date in format 'dd-MMM-yyyy': "+format_1);
//Converting Date to a user specific format 2 - dd/MM/yyyy
DateTimeFormatter formatter_2 = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String format_2=(today).format(formatter_2);
System.out.println("Current date in format 'dd/MM/yyyy': "+format_2);
//Converting Date to a user specific format 3 - E, dd MMM yyyy
DateTimeFormatter formatter_3 = DateTimeFormatter.ofPattern("E, dd MMM yyyy");
String format_3=(today).format(formatter_3);
System.out.println("Current date in format 'E, dd MMM yyyy': "+format_3);
}
}
Current date using default toString(same as ISO Standard Format): 2016-09-28 Current date in format 'dd-MMM-yyyy': 28-Sep-2016 Current date in format 'dd/MM/yyyy': 28/09/2016 Current date in format 'E, dd MMM yyyy': Wed, 28 Sep 2016
- We start by creating a
LocalDate
instance, namedtoday
, using theLocalDate.now()
method. Objecttoday
holds the system date for today. We then printtoday
and it is in the default ISO format as expected i.e.2016-09-28
. - We then create a
SimpleDateFormatter
, namedformatter_1
, using its staticofPattern()
method to which we pass the required date format as aString
containingdd-MMM-yyyy
. - Next we invoke the
format()
method ontoday
object withformatter_1
as the input containing the format in which we need theString
equivalent oftoday
. - The
format()
method returns aString
, namedformat_1
, containing the date in the required format i.e.28-Sep-2016
, which is then printed as output. - Similarly,
today
is printed in two different formats, as Stringsformat_2
andformat_3
, as shown in the output -28/09/2016
andWed,28 Sep 2016
.
Tutorials on Java 8’s new Date and Time API
Overview of Java 8's new Date and Time APIClick to Read Overview of Java 8's new Date-Time API
Working with time zones in Java 8| ZonedDateTime, ZoneId tutorial with examplesClick to Read tutorial on time zone handling in Java 8
How to convert LocalDate to String and String to LocalDateClick to Read tutorial on String to LocalDate conversions
How to convert java.util.Date to java.time.LocalDateClick to Read tutorial on java.util.Date to LocalDate conversion
Formatting localized dates in Spanish and FrenchClick to Read date formatting in Spanish & French
How to get day-of-week for a given dateHow to get day-of-week using java.time.DayOfWeek enum
Date Modification using TemporalAdjuster Click to Read Tutorial on TemporalAdjusters
Overview of Java 8's new Date and Time APIClick to Read Overview of Java 8's new Date-Time API
Working with time zones in Java 8| ZonedDateTime, ZoneId tutorial with examplesClick to Read tutorial on time zone handling in Java 8
How to convert LocalDate to String and String to LocalDateClick to Read tutorial on String to LocalDate conversions
How to convert java.util.Date to java.time.LocalDateClick to Read tutorial on java.util.Date to LocalDate conversion
Formatting localized dates in Spanish and FrenchClick to Read date formatting in Spanish & French
How to get day-of-week for a given dateHow to get day-of-week using java.time.DayOfWeek enum
Date Modification using TemporalAdjuster Click to Read Tutorial on TemporalAdjusters