Skip to content

Instantly share code, notes, and snippets.

@solarmicrobe
Created February 18, 2020 20:47
Show Gist options
  • Save solarmicrobe/518ff1954e2cf86b4a72895c6386481d to your computer and use it in GitHub Desktop.
Save solarmicrobe/518ff1954e2cf86b4a72895c6386481d to your computer and use it in GitHub Desktop.
Examples of date time and timezone fun in the JVM
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