So, a few days ago I encountered an interesting bug in the old and bad java.util.Date
API.
In this code snippet we have 3 different SimpleDateFormat
, and some date to parse:
val sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val sdfPartname = new SimpleDateFormat("YYYY-MM-dd")
val sdfShort = new SimpleDateFormat("yyMMddHHmm")
sdfShort.setTimeZone(TimeZone.getTimeZone("UTC"))
val batchIdList = List(1912311300,1912301130,2001011300,1812311300,1612311300)
batchIdList.foreach( batchId => {
print("\nResult for input:" + batchId + "\n")
val date = sdfShort.parse(batchId.toString).getTime
print("dt: " + sdf.format(date) + "\n")
print("partname: " + sdfPartname.format(date) + "\n"))
})
Output is very interesting:
Result for input:1912311300
dt: 2019-12-31 14:00:00
partname: 2020-12-31
Result for input:1912301130
dt: 2019-12-30 12:30:00
partname: 2020-12-30
Result for input:2001011300
dt: 2020-01-01 14:00:00
partname: 2020-01-01
Result for input:1812311300
dt: 2018-12-31 14:00:00
partname: 2019-12-31
Result for input:1612311300
dt: 2016-12-31 14:00:00
partname: 2016-12-31
I don't know what is behind this wrong year. Do you have any ideas?
PS: Of course changing to ZonedDateTime
solved the problem, but none the less I want to know why java.util.Date
works this way.