1

I need to get real world date and time in Java. I use:

Date date = new Date();

But I'm not sure that it is not just system time. I don't need to be dependent on PC local date and time.

If it is so, then is there any way to abstract from it? I mean I need correct time and date. If today is the 1st of May, 2012 and user changed (maybe there was a system error) it to the 1st of December 2000, it shouldn't affect business logic. So is there any alternative to achieve this?

Dragon
  • 2,431
  • 11
  • 42
  • 68
  • See the following identical SO question: http://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java – Seth Flowers Jun 12 '12 at 16:03
  • I'm not sure if this is really a duplicate (at least not one of the specified question). The way I understand the question, the objective is to get the "real world date", if the system clock is set to a wrong date. In this case, the answer would be to query a time server. See e.g.: http://stackoverflow.com/questions/925191/java-ntp-client – martin Oct 15 '12 at 14:06

1 Answers1

6

Date only represents an instant in time, in milliseconds since the Unix epoch of January 1st 1970 UTC (modulo leap seconds). It has no concept of a time zone in its data. However, if you use the toString method it will always convert that UTC instant to a local date/time using the system time zone. That confuses a lot of users, making them think that Date contains a time zone - it's just an illusion.

Likewise Date doesn't have any concept of a calendar system (Gregorian, Julian etc) or a "format". Basically it's just a long :)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Actually, its since about 24 seconds later than that, as Unix time ignores leap seconds. But it is correct to current UTC, its just if you want historic UTC it gets it wrong. – Julian Jun 12 '12 at 16:07
  • Jon, does this slightly contradict your earlier quote, "java.util.Date is always in UTC" as posted in this [answer](http://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java/308689#308689)? – adarshr Jun 12 '12 at 16:08
  • @adarshr: Not really. It's UTC as much as it's anything, in that it's defined in terms of UTC. – Jon Skeet Jun 12 '12 at 16:09
  • @Julian: Edited in a hand-wavy way :) – Jon Skeet Jun 12 '12 at 16:10
  • So, if I call "new Date()" on a machine that has its local date changed, business logic will persist (some part of it depends a lot on real life date)? – Dragon Jun 12 '12 at 21:56
  • @Dragon: What do you mean by "its local date changed"? If you just mean the time zone, that won't make any difference. But non-zone changes will be seen, yes. `new Date()` only uses the system clock, not NTP etc. – Jon Skeet Jun 13 '12 at 05:44
  • @Jon Skeet: Yes, it depends on system clock. But is there any way to abstract from it? I mean I need correct time and date. If today is the 1st of May, 2012 and user changed (maybe there was a system error) it to the 1st of December 2000, it shouldn't affect business logic. So is there any alternative to achieve this? – Dragon Jun 13 '12 at 07:21
  • @Dragon: You *have* to talk to an external source, such as an NTP server. If you can't trust anything local, you'll have to go over the network. – Jon Skeet Jun 13 '12 at 08:16