i have an XMLGregorianCalendar in this form: 2019-06-24T18:18:55.007+02:00
How can i get the right date (with offset taken into account) in String like this : 24/06/2019 16:18 ?
i have an XMLGregorianCalendar in this form: 2019-06-24T18:18:55.007+02:00
How can i get the right date (with offset taken into account) in String like this : 24/06/2019 16:18 ?
You tagged your question java-6. Yet I am presenting the modern answer using java.time, the modern Java date and time API. This is available in Java 6 through ThreeTen Backport (see the link at the bottom).
Depending on from where you got your XMLGregorianCalendar
you may not need to have one. If I understood correctly, this class was used for representing dates and times in XML documents, where they are formatted like for example 2019-06-24T18:18:55.007+02:00
. This format is close enough to ISO 8601 that the classes of java.time can parse it directly, which we prefer:
Locale userLocale = Locale.forLanguageTag("mt-MT"); // Maltese Malta
DateTimeFormatter displayFormatter
= DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
.withLocale(userLocale);
String fromXml = "2019-06-24T18:18:55.007+02:00";
OffsetDateTime dateTime = OffsetDateTime.parse(fromXml);
String formattedUtcDateTime = dateTime.withOffsetSameInstant(ZoneOffset.UTC)
.format(displayFormatter);
System.out.println(formattedUtcDateTime);
Output from this snippet is (tested in Java 1.7.0_67 with ThreeTen Backport version 1.3.6):
24/06/2019 16:18
Assuming that the formatting is for displaying to a user, I recommend using Java’s localized formats. I have chosen Maltese locale because it matches your requested format exactly. If your user is in a different locale, you should still consider the format of that locale.
If you got your XMLGregorianCalendar
from a legacy API that you cannot afford to change just now, there are a number of options for converting it.
One is to get the string back from the XMLGregorianCalendar
:
XMLGregorianCalendar xmlgc = DatatypeFactory.newInstance()
.newXMLGregorianCalendar("2019-06-24T18:18:55.007+02:00");
String fromXml = xmlgc.toString();
Now the rest is as above. Pro: it’s short, and I don’t think there are any surprises in it. Con: To me formatting back into the original string and parsing it once more feels like the detour.
Another option goes through the outdated GregorianCalendar
class:
GregorianCalendar gc = xmlgc.toGregorianCalendar();
ZonedDateTime zdt = DateTimeUtils.toZonedDateTime(gc);
String formattedUtcDateTime = zdt.toOffsetDateTime()
.withOffsetSameInstant(ZoneOffset.UTC)
.format(displayFormatter);
Pro: I think it’s the official conversion. Con: it’s a line longer, and as I said, it uses the poorly designed and long outdated GregorianCalendar
class.
java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).The working solution i have found:
private String getFormattedDate(final XMLGregorianCalendar xmlGregorianCalendar) {
Date time = xmlGregorianCalendar.toGregorianCalendar().getTime();
Date rightDate = DateUtils.addMinutes(time, - xmlGregorianCalendar.getTimezone());
String formatedDate = DateHelper.format(DateHelper.YYYYMMDDHHMMSS_DASH_COLONS, rightDate);
return formatedDate;}