Java 8 – How to convert String to LocalDate - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
corejavatutorial

Java 8 – How to convert String to LocalDate

Few Java examples show you how to convert a String to the new Java 8 Date API – java.time.LocalDate
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");

String date = "17/08/2017";
//convert String to LocalDate
LocalDate localDate = LocalDate.parse(date, formatter);
Note
Refer to this official DateTimeFormatter JavaDoc for more date time formatter examples.
1. String = 2017-08-17
If the String is formatted like ISO_LOCAL_DATE, you can parse the String directly, no need conversion.
TestNewDate1.java
package com.mycareerrepublic.java8.date;

import java.time.LocalDate;

public class TestNewDate1 {

public static void main(String[] argv) {

String date = "2017-08-17";
//default, ISO_LOCAL_DATE
LocalDate localDate = LocalDate.parse(date);

System.out.println(localDate);

}

}
Output
2017-08-17

2. String = 17-Aug-2017

TestNewDate2.java
package com.mycareerrepublic.java8.date;

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

public class TestNewDate2 {

public static void main(String[] argv) {

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-MMM-yyyy");

String date = "15-Aug-2017";

LocalDate localDate = LocalDate.parse(date, formatter);

System.out.println(localDate); //default, print ISO_LOCAL_DATE

System.out.println(formatter.format(localDate));

}

}
Output
2017-08-15
15-Aug-
2017

3. String = 15/08/2017

TestNewDate3.java
package com.mycareerrepublic.java8.date;

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

public class TestNewDate3 {

public static void main(String[] argv) {

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");

String date = "17/08/2017";
LocalDate localDate = LocalDate.parse(date, formatter);

System.out.println(localDate);

System.out.println(formatter.format(localDate));

}

}
Output
2017-08-17
17/08/2017

4. String = Tue, Aug 15 2017

TestNewDate4.java
package com.mycareerrepublic.java8.date;

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

public class TestNewDate4 {

public static void main(String[] argv) {

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, MMM d yyyy");

String date = "Tue, Aug 17 2017";
LocalDate localDate = LocalDate.parse(date, formatter);

System.out.println(localDate);

System.out.println(formatter.format(localDate));

}

}
Output
2017-08-17
Tue, Aug 17 2017

5. String = Tuesday, Aug 17, 2017 12:10:56 PM

This example convert a String to java.time.LocalDateTime
TestNewDate5.java
package com.mycareerrepublic.java8.date;

package com.mycareerrepublic.pageview;

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

public class TestNewDate5 {

public static void main(String[] argv) {

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMM d, yyyy HH:mm:ss a");

String date = "Tuesday, Aug 17, 2017 12:10:56 PM";
LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);

System.out.println(localDateTime);

System.out.println(formatter.format(localDateTime));

}

}
Output
2017-08-17T12:10:56
Tuesday, Aug 17, 201712:10:56 PM

6. String = 2017-08-15T15:23:01Z

The ‘Z’ suffix means UTC, you can convert into a java.time.instant directly, then display it with a time zone.
TestNewDate6.java
package com.mycareerrepublic.java8.date;

import java.time.*;

public class TestNewDate6 {

public static void main(String[] argv) {

String dateInString = "2017-08-17T15:23:01Z";
Instant instant = Instant.parse(dateInString);

System.out.println("Instant : " + instant);

//get date time only
LocalDateTime result = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneOffset.UTC.getId()));

//get localdate
System.out.println("LocalDate : " + result.toLocalDate());

//get date time + timezone
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Asia/Tokyo"));
System.out.println(zonedDateTime);

//get date time + timezone
ZonedDateTime zonedDateTime2 = instant.atZone(ZoneId.of("Europe/Athens"));
System.out.println(zonedDateTime2);

}

}
Output
Instant : 2017-08-17T15:23:01Z
LocalDate : 2017-08-17
2017-08-17T00:23:01+09:00[Asia/Tokyo]
2017-08-17T18:23:01+03:00[Europe/Athens]

7. String = 2017-08-15T10:15:30+08:00

String -> ZonedDateTime -> LocalDate
TestNewDate7.java
package com.mycareerrepublic.java8.date;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class TestNewDate7 {

public static void main(String[] argv) {

String date = "2017-08-17T10:15:30+08:00";
ZonedDateTime result = ZonedDateTime.parse(date, DateTimeFormatter.ISO_DATE_TIME);

System.out.println("ZonedDateTime : " + result);

System.out.println("TimeZone : " + result.getZone());

LocalDate localDate = result.toLocalDate();

System.out.println("LocalDate : " + localDate);

}

}
Output
ZonedDateTime : 2017-08-17T10:15:30+08:00
TimeZone : +08:00
LocalDate : 2017-08-17

About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.