-1

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 ?

NashBird99
  • 193
  • 2
  • 12
  • Might this be a duplicate of [How to get XMLGregorianCalendar to date](https://stackoverflow.com/questions/36203089/how-to-get-xmlgregoriancalendar-to-date)? Or other previous questions? How thoroughly have you searched? – Ole V.V. Jun 25 '19 at 02:41
  • Would you want the time in UTC always, or in the user’s time zone? – Ole V.V. Jun 25 '19 at 06:16

2 Answers2

1

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.

Converting from XMLGregorianCalendar

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.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Tanks for your answer sir, but it isn't corresponding to a Java 6 Solution, the solution i implemented was getting the Date object from xmlGregorianCalendar : `Date date = xmlGregorianCalendar.toGregorianCalendar().getTime();` then using the getTimeZone method : `int minutesOffset = xmlGregorianCalendar.getTimezone()` (resulting in 120 minutes) then `Date wantedDate = date.addMinutes(xmlGregorianCalendar.getTimeZone())` finaly formating to get `24/06/2019 16:18`. – NashBird99 Jun 25 '19 at 19:39
  • I haven’t understood which `addMinutes` method you are using? My answer and code will work on Java 6 if you download ThreeTen Backport from [here](https://search.maven.org/search?q=g:org.threeten%20AND%20a:threetenbp&core=gav) and add the JAR file to your project. – Ole V.V. Jun 25 '19 at 19:40
  • Is that [`org.joda.time.MutableDateTime.addMinutes`](https://www.joda.org/joda-time/apidocs/org/joda/time/MutableDateTime.html#addMinutes-int-)? If so: (1) That’s incorrect/misuse and bound to confuse. (2) The Joda-Time project is in maintenance mode and not recommended for new code. I’d prefer ThreeTen Backport any time. (3) If you insist on Joda-Time, I am sure that a better Joda-Time solution can be found. – Ole V.V. Jun 25 '19 at 19:58
0

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;}
NashBird99
  • 193
  • 2
  • 12