To convert LocalDate
and Timestamp
, put LocalDateTime
in between.
--Reference - Timestamp (Java Platform SE 8) - LocalDate (Java Platform SE 8)
/**
*Get the current date and time with TimeStamp.
* @return Current date and time.
*/
public Timestamp getTimestamp() {
//Current date and time with LocalDate.
var localDate = LocalDate.now();
//Create LocalDateTime at the start of the date.
var localDateTime = localDate.atStartOfDay();
//Same date as LocalDateTime/Get Timestamp with time value.
var timestamp = Timestamp.valueOf(localDateTime);
return timestamp;
}
/**
*Get the current date with LocalDate.
* @return current date.
*/
public LocalDate getLocalDate() {
//Current date and time in Timestamp.
var timestamp = new Timestamp(new Date().getTime());
//Convert Timestamp to LocalDateTime
var localDateTime = timestamp.toLocalDateTime();
//This date/Get LocalDate part of time
var localDate = localDateTime.toLocalDate();
return localDate;
}
Recommended Posts