Last active
June 22, 2018 07:59
-
-
Save lunaspeed/3dd41f607dc9eab50c035ddf2e7a8d69 to your computer and use it in GitHub Desktop.
Java: Convert Date to LocalDate/LocalDateTime and back (Scala syntax)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//reference http://www.baeldung.com/java-date-to-localdate-and-localdatetime | |
//LocalDate to Date | |
val date: Date = Date.from(LocalDate.now.atStartOfDay(ZoneId.systemDefault).toInstant) | |
//Date to LocalDate | |
val input = new Date | |
val ldate = input.toInstant.atZone(ZoneId.systemDefault).toLocalDate | |
//Java 9 | |
val ldate9 = LocalDate.ofInstant(input.toInstant, ZoneId.systemDefault) | |
//LocalDateTime to Date | |
val date = Date.from(LocalDateTime.now.atZone(ZoneId.systemDefault).toInstant) | |
//Date to LocalDateTime | |
val datetime = input.toInstant.atZone(ZoneId.systemDefault).toLocalDateTime |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment