java localdatetime to date code example
Example 1: localdate to date java
Date.from(localdate.atStartOfDay(ZoneId.systemDefault()).toInstant())
Example 2: string to localdate in java
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
String date = "16/08/2016";
LocalDate localDate = LocalDate.parse(date, formatter);
Example 3: how to turna date into a LocalDateTime java
public LocalDateTime convertToLocalDateTimeViaInstant(Date dateToConvert) {
return dateToConvert.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDateTime();
}
Example 4: java convert java.util.Date to LocalDate
Date yourDate = new Date();
LocalDate date = yourDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
Example 5: how to turna date into a LocalDateTime java
public LocalDateTime convertToLocalDateTimeViaMilisecond(Date dateToConvert) {
return Instant.ofEpochMilli(dateToConvert.getTime())
.atZone(ZoneId.systemDefault())
.toLocalDateTime();
}
Example 6: local date to date java
public LocalDate convertToLocalDateViaInstant(Date dateToConvert) {
return dateToConvert.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
}