java date to localdatetime code example

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

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

Example 3: date to localdate

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

Example 4: how to parse a specific datetime to localdatetime

LocalDateTime date = LocalDateTime.parse("1995-07-25T15:03:45");

Example 5: how to turna date into a LocalDateTime java

public LocalDateTime convertToLocalDateTimeViaMilisecond(Date dateToConvert) {
    return Instant.ofEpochMilli(dateToConvert.getTime())
      .atZone(ZoneId.systemDefault())
      .toLocalDateTime();
}

Tags:

Java Example