Skip to content

Instantly share code, notes, and snippets.

@squito
Created August 24, 2017 16:47
Show Gist options
  • Save squito/c3b6bb5098650a865b4cf151022ea606 to your computer and use it in GitHub Desktop.
Save squito/c3b6bb5098650a865b4cf151022ea606 to your computer and use it in GitHub Desktop.
Java timestamp mechanics
> scala -Duser.timezone=America/Los_Angeles timestamp.scala
Defaul TZ: America/Los_Angeles
hours in UTC: 8
TZ offset in hours: -8
> scala -Duser.timezone=America/New_York timestamp.scala
Defaul TZ: America/New_York
hours in UTC: 5
TZ offset in hours: -5
import java.sql.Timestamp
import java.util.TimeZone
val tz = TimeZone.getDefault();
println(s"Defaul TZ: ${tz.getID()}")
val tsString = "1970-01-01 00:00:00"
val ts = Timestamp.valueOf(tsString);
println(s"timestamp $tsString has millis ${ts.getTime()}")
println(s"hours in UTC: ${ts.getTime() / 1000 / 60 / 60}")
println(s"TZ offset in hours: ${tz.getOffset(ts.getTime()) / 1000 / 60 / 60}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment