I have been working on task where I need to fetch Date from XMLGregorianCalendar.I have converted it to GregorianCalendar and tried to get date from it.But I am seeing difference between original date stored in XMLGregorianCalendar and Date obtained from toGregorianCalendar, I want date to be in UTC format and source time zone is also in UTC.Please find code snippet for reference.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ";
sdf.setTimeZone(TimeZone.getTimeZone("UTC"))
String date = sdf.format(xmlGregorianCalendar.toGregorianCalendar().getTime());
I am wondering if there is any way to get Date from XMLGregorianCalendar with UTC time zone and without any time difference.Below is the sample source date stored in XMLGregorianCalendar
2018-07-06T05:11:00.050Z
I am expecting exact same date as string after I format it with SimpleDateFormat.Your help is much appreciated, thanks in advance!
With your
XMLGregorianCalendar
this prints:Z
means UTC or offset zero or Zulu time zone. So this gives you date and time in UTC with explicit offset.If what you really need is just the correct point in time, one would usually use an
Instant
for that:Output:
You notice that the date and time is printed exactly as before including the
Z
for UTC. The difference is: anOffsetDateTime
may hold any offset (and will hold a different offset if yourXMLGregorianCalendar
does). AnInstant
is thought of as a point in time independent of any zone or offset. It is only itstoString
method that uses UTC for printing a date and time, and the suffix will always beZ
for UTC.What went wrong on your code?
First, the
SimpleDateFormat
class is not only long outdated. It is also notoriously troublesome, particularly when it comes to parsing, but still.TimeZone
is outdated too. You will prefer to usejava.time
, the modern Java date and time API, as I am doing in the above snippets.I ran your code and got
2018-07-06T05:11:00.050+0000
. Offset zero from UTC is printed differently, but the string agrees with what you say you want.You cannot have a
java.util.Date
(another outdated class) with a time zone or offset. It represents a point in time, nothing more.Links
java.time
.