string to localdate code example

Example 1: convert string to localdatetime

//convert String to LocalDateTime
String str = "2016-03-04 11:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);

Example 2: java 8 string to localdate

package com.mkyong.date;

import java.time.LocalDate;

public class JavaDateExample1 {

    public static void main(String[] args) {

        String date = "2016-08-16";

        //default, ISO_LOCAL_DATE
        LocalDate localDate = LocalDate.parse(date);

        System.out.println(localDate);

    }
}
Copy

Example 3: string to localdate in java

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
  String date = "16/08/2016";

  //convert String to LocalDate
  LocalDate localDate = LocalDate.parse(date, formatter);

Example 4: date to localdate

dateToConvert.toInstant()
      .atZone(ZoneId.systemDefault())
      .toLocalDate();

Example 5: convert localdatetime to localdate

import java.time.LocalDate;
import java.time.LocalDateTime;
 
public class Main {
    public static void main(String[] args) {
 
        LocalDateTime localDateTime = LocalDateTime.now();
         
        LocalDate localDate = localDateTime.toLocalDate();
         
        System.out.println(localDate);
    }
}

Tags:

Java Example