[BAEL-8456] - Moved Java Date articles into a new module - 'java-dates'
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.java9.datetime;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Class which shows a way to convert java.util.Date into java.time.LocalDate.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class DateToLocalDateConverter {
|
||||
|
||||
public static LocalDate convertToLocalDateViaInstant(Date dateToConvert) {
|
||||
return dateToConvert.toInstant()
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toLocalDate();
|
||||
}
|
||||
|
||||
public static LocalDate convertToLocalDateViaSqlDate(Date dateToConvert) {
|
||||
return new java.sql.Date(dateToConvert.getTime()).toLocalDate();
|
||||
}
|
||||
|
||||
public static LocalDate convertToLocalDateViaMilisecond(Date dateToConvert) {
|
||||
return Instant.ofEpochMilli(dateToConvert.getTime())
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toLocalDate();
|
||||
}
|
||||
|
||||
public static LocalDate convertToLocalDate(Date dateToConvert) {
|
||||
return LocalDate.ofInstant(dateToConvert.toInstant(), ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user