6

I've got a problem with timestamps between java and javascript.

I already found these 2 questions about the timestamps and I know about the timechanges all over the years.

Timestamp deviation Java vs Javascript for old dates (3600secs)

Why is subtracting these two times (in 1927) giving a strange result?

Basically at midnight at the end of 1927, the clocks went back 5 minutes and 52 seconds. So "1927-12-31 23:54:08" actually happened twice, and it looks like Java is parsing it as the later possible instant for that local date/time.

What the problems makes is that when I have javascript and put the timestamp in there then I get an other date than the Java date. I need this to show the correct date on the webpage. I know I can request the date as a string but I prefer using a timestamp.

Java date 0001-01-01 timestamp is -62135773200000

JavaScript date 0001-01-01 timestamp is -62135596800000

The difference is -176400000; 49 hours.

Does anybody know what I can do for this.

Community
  • 1
  • 1

2 Answers2

6

Personally, I would avoid passing numerical timestamps around from a system in one language to a system in another language for the sole reason that the languages may differ in the algorithm they use to generate them.

There is an international standard in place (ISO-8601) to deal with passing timestamps from system to system. In this your date representation becomes 0001-01-01T00:00:00+00:00. I would recommend using this approach, as it's a widely accepted solution for this very problem.

Kyri Elia
  • 997
  • 9
  • 18
  • Yes, ISO 8601 is the correct approach. Usage such as this Question is the very purpose of that standard. The standard formats are much easier to read, log, troubleshoot, and debug than `long` numeric values. – Basil Bourque Nov 28 '15 at 02:57
1

This might be related to TZ and DST settings which diverge from browser to java. In order to nail it down, I recommend to use ISO-8601 formats like 2008-02-01T09:00:22+05, this is ambiguous-less

Axel Amthor
  • 10,980
  • 1
  • 25
  • 44