简体   繁体   中英

Adding millis to a date

Recently I stumbled across a strange behaviour while adding some millis to a Date . Why does the second example produce 11:00 instead of 12:00 ?

def addWeek(s: String) = {
  val df = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm")
  val utcMillisSinceEpoch = df.parse(s).getTime
  val weekMillis = 1000L * 60L * 60L * 24L * 7L
  df.format(new java.util.Date(utcMillisSinceEpoch + weekMillis))
}
addWeek("2013/10/01 12:00") // 2013/10/08 12:00
addWeek("2013/10/21 12:00") // 2013/10/28 11:00
addWeek("2013/11/21 12:00") // 2013/11/28 12:00

The daylight saving time ends end of october. Therefore the second addWeek call shifts your date over the end of DST.

Depending on your locale, daylight savings time may end around that period. When DST ends, one hour is repeated.

It looks like you're in Germany. In that locale, DST ends Sunday, October 27.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM