Skip to content

Instantly share code, notes, and snippets.

@buildlackey
Last active September 5, 2019 22:28
Show Gist options
  • Save buildlackey/1ce4228f8223bab321da2e8a3316c18b to your computer and use it in GitHub Desktop.
Save buildlackey/1ce4228f8223bab321da2e8a3316c18b to your computer and use it in GitHub Desktop.
badtimesInPst
import java.util.TimeZone
def updateTimeZone(zone: String) = {
System.setProperty("user.timezone", zone);
TimeZone.setDefault(TimeZone.getTimeZone(zone))
}
// Set timezone to Pacific Standard, where -- at least for now --
// daylight savings is in effect
updateTimeZone("PST")
java.sql.Timestamp.valueOf("2019-03-10 01:00:00")
// should result in: java.sql.Timestamp = 2019-03-10 01:00:00.0
java.sql.Timestamp.valueOf("2019-03-09 02:00:00") // 2 AM
// should result in: java.sql.Timestamp = 2019-03-09 02:00:00.0
java.sql.Timestamp.valueOf("2019-03-10 02:00:00")
// should result in: java.sql.Timestamp = 2019-03-10 03:00:00.0 - 3 AM, not 2 AM as per input !
// Let's now move to Japan time, where day light savings rules are not followed
updateTimeZone("Asia/Tokyo")
java.sql.Timestamp.valueOf("2019-03-10 01:00:00")
// should result in: java.sql.Timestamp = 2019-03-10 01:00:00.0
java.sql.Timestamp.valueOf("2019-03-09 02:00:00") // 2 AM
// should result in: java.sql.Timestamp = 2019-03-09 02:00:00.0
java.sql.Timestamp.valueOf("2019-03-10 02:00:00")
// should result in: java.sql.Timestamp = 2019-03-10 03:00:00.0 - Doesn't skip 1 hour to 3 AM !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment