By default, what time zone does method java.util.Date.toString()
display? Since a Java Date
stores a given date in UTC and doesn't contain any explicit time zone, does Date.toString()
simply display the default time zone for the host, or TimeZone.getDefault()
?

- 27,608
- 43
- 124
- 174
2 Answers
It displays using TimeZone.getDefault()
which, in turn, will default to the time zone of the operating system it is running on (i.e. the host). So in practice, they will be the same thing
Note that a Java date is not really a date! It is an instant in time, represented by a millisecond offset since the beginning of the epoch. It still contains methods which reference year, month etc but these are all deprecated. You should on no account be using a Date
object as if it were a date.
Use a Calendar
(although this is arguably even more broken than Date
) or a decent library like JODA.

- 133,303
- 56
- 317
- 449
-
Is there any chance that given a host timezone of Eastern Standard Time (EST) that Date.toString() would sometimes display time zone EST, but other times display Eastern Daylight Time (EDT)? I'm observing this strange behaviour with `ToStringBuilder.reflectionToString(Object)` which, according to http://stackoverflow.com/questions/4198253/in-what-format-does-tostringbuilder-reflectiontostringobject-display-dates/4198295#4198295, uses `Date.toString()` to display `Date` objects. – Derek Mahar Nov 16 '10 at 21:43
-
I would think it likely that your system uses any daylight savings settings appropriate to your locale (your sys admins will know). So during the DST period, I would expect the default timezone to be EDT (assuming that EDT is the daylight saving version of EST). – oxbow_lakes Nov 16 '10 at 22:00
-
My question wasn't clear enough. Given an Object that contains multiple `Date` objects, `ToStringBuilder.reflectionToString(Object)` displays some dates with EST time zone and other dates with EDT. Shouldn't it display them all in the host time zone, in this case EST? – Derek Mahar Nov 17 '10 at 02:44
-
@Derek - you are using one timezone that display a date as either EST or EDT depending on the date ... not on whether "now" is in daylight savings time. *"Shouldn't it display them all in the host time zone"* - that would be really confusing for most people. ("I got to work at 9am on that day, but the stupid computer says it was 10am.") – Stephen C Nov 17 '10 at 03:19
-
Stephen C, I agree with you that this time zone output is strange, but it is not our code that is the culprit. It is either the JAX-WS reference implementation (jaxws-rt) that is confused about to which time zone to translate dates that it deserializes from an input SOAP message, or either Apache Commons Lang 2.4 ToStringBuilder.reflectionToString(Object) or Date.toString() that is displaying several of the "instants in time" in the incorrect time zone. I expect the dates to display in time zone EST, the time zone of the operating system and JVM, and not EDT. – Derek Mahar Nov 17 '10 at 16:46
-
Stephen C, I think I initially misunderstood your comment. When JAX-WS is mapping SOAP XML `xs:dateTime` elements from a client to Java `Date` objects in the server, it translate these input dates to UTC milliseconds. However, when the server outputs the `Date` to the log, or stores the `Date` to the database, according to the answer, `Date.toString()` translates the UTC milliseconds in the `Date` to the host time zone and the JDBC driver also translates these UTC milliseconds, usually to the host time zone, though some drivers may translate it to the database server time zone. – Derek Mahar Nov 17 '10 at 17:21
-
The problem is that the log shows some dates in EST and others in EDT, when I expect them all to be in EST. Why the mix of time zones? – Derek Mahar Nov 17 '10 at 17:47
Does it display the default time zone for the host, or TimeZone.getDefault()?
The latter (which is the same as the former unless you set it explicitly somewhere). However, this is information gleamed from the source code, so it should be considered an implementation detail. If you want specific repeatable behavior, you should implement it yourself.

- 342,105
- 78
- 482
- 720