localdate to date java code example

Example 1: convert date to calendar java

public static Calendar toCalendar(Date date){ 
  Calendar cal = Calendar.getInstance();
  cal.setTime(date);
  return cal;
}

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: localdate to date java

// Localdate to Date
Date.from(localdate.atStartOfDay(ZoneId.systemDefault()).toInstant())

Example 4: 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 5: date to localdate

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

Example 6: how to turna date into a LocalDateTime java

public LocalDateTime convertToLocalDateTimeViaInstant(Date dateToConvert) {
    return dateToConvert.toInstant()
      .atZone(ZoneId.systemDefault())
      .toLocalDateTime();
}

Tags:

Misc Example