I have parsed a java.util.Date
from a String
but it is setting the local time zone as the time zone of the date
object.
The time zone is not specified in the String
from which Date
is parsed. I want to set a specific time zone of the date
object.
How can I do that?
Use DateFormat. For example,
If you must work with only standard JDK classes you can use this:
Credit goes to this post.
java.util.Calendar
is the usual way to handle time zones using just JDK classes. Apache Commons has some further alternatives/utilities that may be helpful. Edit Spong's note reminded me that I've heard really good things about Joda-Time (though I haven't used it myself).Be aware that
java.util.Date
objects do not contain any timezone information by themselves - you cannot set the timezone on aDate
object. The only thing that aDate
object contains is a number of milliseconds since the "epoch" - 1 January 1970, 00:00:00 UTC.As ZZ Coder shows, you set the timezone on the
DateFormat
object, to tell it in which timezone you want to display the date and time.If anyone ever needs this, if you need to convert an
XMLGregorianCalendar
timezone to your current timezone from UTC, then all you need to do is set the timezone to0
, then calltoGregorianCalendar()
- it will stay the same timezone, but theDate
knows how to convert it to yours, so you can get the data from there.Result:
tl;dr
No Time Zone in j.u.Date
As the other correct answers stated, a java.util.Date has no time zone†. It represents UTC/GMT (no time zone offset). Very confusing because its
toString
method applies the JVM's default time zone when generating a String representation.Avoid j.u.Date
For this and many other reasons, you should avoid using the built-in java.util.Date & .Calendar & java.text.SimpleDateFormat. They are notoriously troublesome.
Instead use the java.time package bundled with Java 8.
java.time
The java.time classes can represent a moment on the timeline in three ways:
Instant
)OffsetDateTime
withZoneOffset
)ZonedDateTime
withZoneId
)Instant
In java.time, the basic building block is
Instant
, a moment on the time line in UTC. UseInstant
objects for much of your business logic.OffsetDateTime
Apply an offset-from-UTC to adjust into some locality’s wall-clock time.
Apply a
ZoneOffset
to get anOffsetDateTime
.ZonedDateTime
Better is to apply a time zone, an offset plus the rules for handling anomalies such as Daylight Saving Time (DST).
Apply a
ZoneId
to anInstant
to get aZonedDateTime
. Always specify a proper time zone name. Never use 3-4 abbreviations such asEST
orIST
that are neither unique nor standardized.LocalDateTime
If the input string lacked any indicator of offset or zone, parse as a
LocalDateTime
.If you are certain of the intended time zone, assign a
ZoneId
to produce aZonedDateTime
. See code example above in tl;dr section at top.Formatted Strings
Call the
toString
method on any of these three classes to generate a String representing the date-time value in standard ISO 8601 format. TheZonedDateTime
class extends standard format by appending the name of the time zone in brackets.For other formats use the
DateTimeFormatter
class. Generally best to let that class generate localized formats using the user’s expected human language and cultural norms. Or you can specify a particular format.About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*
classes.Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.Joda-Time
While Joda-Time is still actively maintained, its makers have told us to migrate to java.time as soon as is convenient. I leave this section intact as a reference, but I suggest using the
java.time
section above instead.In Joda-Time, a date-time object (
DateTime
) truly does know its assigned time zone. That means an offset from UTC and the rules and history of that time zone’s Daylight Saving Time (DST) and other such anomalies.Call the
toString
method to generate a String in ISO 8601 format.Joda-Time also offers rich capabilities for generating all kinds of other String formats.
If required, you can convert from Joda-Time DateTime to a java.util.Date.
Search StackOverflow for "joda date" to find many more examples, some quite detailed.
†Actually there is a time zone embedded in a java.util.Date, used for some internal functions (see comments on this Answer). But this internal time zone is not exposed as a property, and cannot be set. This internal time zone is not the one used by the
toString
method in generating a string representation of the date-time value; instead the JVM’s current default time zone is applied on-the-fly. So, as shorthand, we often say “j.u.Date has no time zone”. Confusing? Yes. Yet another reason to avoid these tired old classes.