28

I have 2 different computers, each with different TimeZone.

In one computer im printing System.currentTimeMillis(), and then prints the following command in both computers: System.out.println(new Date(123456)); --> 123456 stands for the number came in the currentTimeMillis in computer #1.

The second print (though typed hardcoded) result in different prints, in both computers. why is that?

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
Udi
  • 1,080
  • 1
  • 12
  • 25

5 Answers5

54

How about some pedantic detail.

java.util.Date is timezone-independent. Says so right in the javadoc.

You want something with respect to a particular timezone? That's java.util.Calendar.

The tricky part? When you print this stuff (with java.text.DateFormat or a subclass), that involves a Calendar (which involves a timezone). See DateFormat.setTimeZone().

It sure looks (haven't checked the implementation) like java.util.Date.toString() goes through a DateFormat. So even our (mostly) timezone-independent class gets messed up w/ timezones.

Want to get that timezone stuff out of our pure zoneless Date objects? There's Date.toGMTString(). Or you can create your own SimpleDateFormatter and use setTimeZone() to control which zone is used yourself.

John M
  • 13,053
  • 3
  • 27
  • 26
  • 1
    java.util.Date is only somewhat timezone-independent. Copy&Paste from Java SDK7 java.util.Date.toString() `TimeZone zi = date.getZone(); if (zi != null) { sb.append(zi.getDisplayName(date.isDaylightTime(), zi.SHORT, Locale.US)); // zzz } else { sb.append("GMT"); }` `date` is a `BaseCalendar` which time zone is set to `TimeZone.getDefaultRef()` – Lucas Hoepner Apr 11 '13 at 10:41
  • 2
    The key point is that you're talking about the toString() method. I said I hadn't looked at the toString() implementation, but suspected it involved a Calendar. You just confirmed that. No disagreement between us. – John M Apr 11 '13 at 19:22
  • 4
    The `Date` object works with a `cdate` member (which is a `BaseCalendar`) with a time zone. And then stuff happens which nobody really cares about and `Date` behaves timezone-independent as long as you do not print it. Didn't mean to contradict your answer. – Lucas Hoepner Apr 12 '13 at 11:36
  • Note that the Java-doc of the `toString()` Method says _If time zone information is not available, then zzz is empty - that is, it consists of no characters at all._ **BUT** afaik the code will apply `GMT` when there is no timezone information availalbe (`zi != null` resolves to a false condition). – JBA Nov 20 '14 at 11:54
7

why is that?

Because something like "Oct 4th 2009, 14:20" is meaningless without knowing the timezone it refers to - which you can most likely see right now, because that's my time as I write this, and it probably differs by several hours from your time even though it's the same moment in time.

Computer timestamps are usually measured in UTC (basically the timezone of Greenwich, England), and the time zone has to be taken into account when formatting them into something human readable.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 2
    I know this is an old thread, but had to vote-up for your explanation/statement of why date without timezone is meaningless. – Helter Scelter Mar 21 '13 at 20:47
  • 1
    @HelterScelter I have to downvote the same thing. It's like saying mass is meaningless without gravity constant. They're both meaningful in their own way. – Dax Fohl Jan 01 '15 at 01:59
5

Because that milliseconds number is the number of milliseconds past 1/1/1970 UTC. If you then translate to a different timezone, the rendered time will be different.

e.g. 123456 may correspond to midday at Greenwich (UTC). But that will be a different time in New York.

To confirm this, use SimpleDateFormat with a time zone output, and/or change the timezone on the second computer to match the first.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
2

javadoc explains this well, System.currentTimeMillis() Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

Jackie
  • 25,199
  • 6
  • 33
  • 24
2

See https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#toString().

Yes, it's using timezones. It should also print them out (the three characters before the year).

indivisible
  • 4,892
  • 4
  • 31
  • 50
svens
  • 11,438
  • 6
  • 36
  • 55