Overview

In this tutorial, we show you how to convert String to Date in Java JDK 8. The java.time Package is the base package of new Java 8 Date Time API. All the major base classes are part of this package, such as LocalDate, LocalTime, LocalDateTime.

Java Convert String to Date in Java 8

Using ISO_LOCAL_DATE format

package com.jackrutorial;

import java.time.LocalDate;

public class ConvertStringToDateJava8Example1 {

 public static void main(String[] args) {
  String date = "2018-06-01";
  LocalDate localDate = LocalDate.parse(date);
        System.out.println("Date in ISO_LOCAL_DATE format is: " + localDate);
 }
}

Output

Date in ISO_LOCAL_DATE format is: 2018-06-01


Using dd-MMM-yyyy format. Example: 01-Jun-2018

package com.jackrutorial;

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

public class ConvertStringToDateJava8Example2 {
 
 public static void main(String[] args) {
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
  String date = "01-Jun-2018";
  
  LocalDate localDate = LocalDate.parse(date, formatter);
        System.out.println("Date in dd-MMM-yyyy format is: " + formatter.format(localDate));
 }
}

Output

Date in dd-MMM-yyyy format is: 01-Jun-2018

Using yyyy-MM-dd format. Example: 2018-06-01

package com.jackrutorial;

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

public class ConvertStringToDateJava8Example3 {
 
 public static void main(String[] args) {
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  String date = "2018-06-01";
  
  LocalDate localDate = LocalDate.parse(date, formatter);
        System.out.println("Date in yyyy-MM-dd format is: " + formatter.format(localDate));
 }
}

Output

Date in yyyy-MM-dd format is: 2018-06-01

Using yyyy-MM-dd HH:mm:ss format. Example: 2018-06-01 23:59:59

package com.jackrutorial;

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

public class ConvertStringToDateJava8Example4 {
 
 public static void main(String[] args) {
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  String date = "2018-06-01 23:59:59";
  
  LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);
        System.out.println("Date in yyyy-MM-dd HH:mm:ss format is: " + formatter.format(localDateTime));
 }
}

Output

Date in yyyy-MM-dd HH:mm:ss format is: 2018-06-01 23:59:59

Using dd/MM/yyyy format. Example: 01/06/2018

package com.jackrutorial;

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

public class ConvertStringToDateJava8Example5 {
 
 public static void main(String[] args) {
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
  String date = "01/06/2018";

  LocalDate localDate = LocalDate.parse(date, formatter);
  System.out.println("Date in dd/MM/yyyy format is: " + formatter.format(localDate));
 }
}

Output

Date in dd/MM/yyyy format is: 01/06/2018

Using dd/MM/yyyy HH:mm:ss format. Example: 01/06/2018

package com.jackrutorial;

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

public class ConvertStringToDateJava8Example6 {

 public static void main(String[] args) {
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
  String date = "01/06/2018 23:59:59";

  LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);
  System.out.println("Date in dd/MM/yyyy HH:mm:ss format is: " + formatter.format(localDateTime));
 }
}

Output

Date in dd/MM/yyyy HH:mm:ss format is: 01/06/2018 23:59:59

Using E, MMM dd yyyy format. Example: Fri, Jun 01 2018

package com.jackrutorial;

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

public class ConvertStringToDateJava8Example7 {

 public static void main(String[] args) {
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, MMM dd yyyy");
  String date = "Fri, Jun 01 2018";

  LocalDate localDate = LocalDate.parse(date, formatter);
  System.out.println("Date in 'E, MMM dd yyyy' format is: " + formatter.format(localDate));
 }
}

Output

Date in 'E, MMM dd yyyy' format is: Fri, Jun 01 2018

Using E, MMM dd yyyy HH:mm:ss format. Example: Fri, Jun 01 2018 23:59:59

package com.jackrutorial;

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

public class ConvertStringToDateJava8Example8 {

 public static void main(String[] args) {
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, MMM dd yyyy HH:mm:ss");
  String date = "Fri, Jun 01 2018 23:59:59";

  LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);
  System.out.println("Date in 'E, MMM dd yyyy HH:mm:ss' format is: " + formatter.format(localDateTime));
 }
}

Output

Date in 'E, MMM dd yyyy HH:mm:ss' format is: Fri, Jun 01 2018 23:59:59

Using EEEE, MMM dd, yyyy HH:mm:ss a format. Example: Friday, Jun 01, 2018 12:10:56 PM

package com.jackrutorial;

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

public class ConvertStringToDateJava8Example9 {

 public static void main(String[] args) {
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMM dd, yyyy HH:mm:ss a");
  String date = "Friday, Jun 01, 2018 12:10:56 PM";

  LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);
  System.out.println("Date in 'EEEE, MMM dd, yyyy HH:mm:ss a' format is: " + formatter.format(localDateTime));
 }
}

Output

Date in 'EEEE, MMM dd, yyyy HH:mm:ss a' format is: Friday, Jun 01, 2018 12:10:56 PM

Using java.time.Instant

package com.jackrutorial;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ConvertStringToDateJava8Example10 {

 public static void main(String[] args) {
  Instant instant = Instant.now();
        System.out.println("Current Timestamp = "+ instant);
        
        ZonedDateTime zonedDateTime1 = instant.atZone(ZoneId.of("US/Alaska"));
        System.out.println("Date in US/Alaska Time Zone a format is:" +zonedDateTime1);

        ZonedDateTime zonedDateTime2 = instant.atZone(ZoneId.of("Europe/London"));
        System.out.println("Date in Europe/London Time Zone a format is:" +zonedDateTime2);
 }
}

Output

Current Timestamp = 2018-06-21T09:39:11.582Z
Date in US/Alaska Time Zone a format is:2018-06-21T01:39:11.582-08:00[US/Alaska]
Date in Europe/London Time Zone a format is:2018-06-21T10:39:11.582+01:00[Europe/London]

Using java.time.ZonedDateTime

package com.jackrutorial;

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

public class ConvertStringToDateJava8Example11 {

 public static void main(String[] args) {
  String date = "2018-06-01T23:59:59+12:00";
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(date, DateTimeFormatter.ISO_DATE_TIME);
        System.out.println("ZonedDateTime : " + zonedDateTime);
        System.out.println("TimeZone : " + zonedDateTime.getZone());
        LocalDate localDate = zonedDateTime.toLocalDate();
        System.out.println("LocalDate : " + localDate);
 }
}

Output

ZonedDateTime : 2018-06-01T23:59:59+12:00
TimeZone : +12:00
LocalDate : 2018-06-01
Previous Post
Next Post

post written by: