date to localdatetime code example
Example 1: date to localdate
dateToConvert.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
Example 2: 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);
}
}
Example 3: how to parse a specific datetime to localdatetime
LocalDateTime date = LocalDateTime.parse("1995-07-25T15:03:45");
Example 4: how to turna date into a LocalDateTime java
public LocalDateTime convertToLocalDateTimeViaMilisecond(Date dateToConvert) {
return Instant.ofEpochMilli(dateToConvert.getTime())
.atZone(ZoneId.systemDefault())
.toLocalDateTime();
}
Example 5: local date to date java
public LocalDate convertToLocalDateViaInstant(Date dateToConvert) {
return dateToConvert.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
}
Example 6: java date to localdate
LocalDate localDate = LocalDate.parse( new SimpleDateFormat("yyyy-MM-dd").format(date) );