728x90
Convert java.time.LocalDateTime to java.util.Date
The easiest way of getting a java.util.Date from LocalDateTime is to use an extension to the java.sql.Timestamp — available with Java 8:
public Date convertToDateViaSqlTimestamp(LocalDateTime dateToConvert) {
return java.sql.Timestamp.valueOf(dateToConvert);
}Copy
But of course, an alternative solution is using an Instant object, which we obtain from ZonedDateTime:
Date convertToDateViaInstant(LocalDateTime dateToConvert) {
return java.util.Date
.from(dateToConvert.atZone(ZoneId.systemDefault())
.toInstant());
}
https://www.baeldung.com/java-date-to-localdate-and-localdatetime
Convert Date to LocalDate or LocalDateTime and Back | Baeldung
Learn about possible ways of converting the between old java.util.Date classes and new java.time API.
www.baeldung.com
728x90
'Web > tip' 카테고리의 다른 글
[error] module java.base does not "opens java.lang" to unnamed module (0) | 2023.06.14 |
---|---|
[error] Error attempting to get column '컬럼' from result set: Cause: java.sql.SQLException: Invalid value for getInt() (0) | 2023.05.04 |
[Exception] (0) | 2023.04.29 |
[CI/CD와 무중단 배포] (0) | 2023.04.21 |
[Persistence] JDBC, SQLMAPPER, ORM (0) | 2023.04.16 |