Created
February 18, 2020 20:47
-
-
Save solarmicrobe/518ff1954e2cf86b4a72895c6386481d to your computer and use it in GitHub Desktop.
Examples of date time and timezone fun in the JVM
This file contains 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
import java.sql.Timestamp | |
import java.time.{Instant, OffsetDateTime} | |
import java.util.{Calendar, TimeZone} | |
val tz = TimeZone.getTimeZone("UTC") | |
val tzid = tz.toZoneId | |
val ts = Timestamp.from(Instant.now) | |
val inst = ts.toInstant | |
val odt = OffsetDateTime.ofInstant(ts.toInstant, tzid) | |
odt.getYear | |
odt.getMonthValue | |
odt.getDayOfMonth | |
val ldt = ts.toLocalDateTime | |
ldt.getYear | |
ldt.getMonthValue-1 | |
ldt.getDayOfMonth | |
val calendar = new Calendar.Builder().setInstant(ts.toInstant.getEpochSecond * 1000).setTimeZone(tz).build | |
calendar.getTime | |
calendar.get(Calendar.YEAR) | |
calendar.get(Calendar.MONTH) | |
calendar.get(Calendar.DAY_OF_MONTH) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment