0

Converting a string date in Java like so:

val sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.getDefault)
sdf.setTimeZone(TimeZone.getDefault)
sdf.setLenient(false)
sdf.parse("1581-01-01T05:51:00Z").getTime

which produces: -12274711740000

In javascript taking that timestamp and converting back to a string date like so:

var now = moment.unix(-12274711740).utc().format('YYYY-MM-DD hh:mm:ss.SSS');

produces: "1581-01-11 01:51:00.000"

The date expected is "1581-01-01 01:51:00.000", 10 days off.

What is odd is if it is any year after 1581 the date will come out correct. And any year before 1582 will be some number of days off (it can vary).

What is causing this discrepancy?

Logan L.
  • 85
  • 7
  • 4
    https://www.britannica.com/story/ten-days-that-vanished-the-switch-to-the-gregorian-calendar – tgdavies Oct 18 '22 at 00:50
  • Ah, thanks for the answer. Unfortunately, I didn't have something like that pop up in my searches. – Logan L. Oct 18 '22 at 01:39
  • 1
    I strongly recommend you don’t use `SimpleDateFormat` and `TimeZone`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `Instant` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Also never hardcode `Z` as a literal in your format pattern string. It’s an offset and needs to be parsed as an offset. – Ole V.V. Oct 20 '22 at 20:10
  • Agreed, I would love to I'm working within an outdated system talking to Spark 2.4.4 though and it doesn't like the newer date formats. Hopefully upgrading soon and can move to the newer formats. – Logan L. Oct 21 '22 at 20:31

0 Answers0