0

I am trying to convert a java.util.Date to XMLGregorianCalendar in Italian format (dd/mm/yyyy) with no time. Whatever I try the output always prints yyyy-mm-dd.

   GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Rome"));
   XMLGregorianCalendar xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendarDate(cal.get(Calendar.YEAR), 
           cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH), 
           DatatypeConstants.FIELD_UNDEFINED);   
   System.out.println(xmlDate);

I am a consumer of SOAP web-service, and the date attribute is defined as XMLGregorianCalendar.

Please advise how can I change the code to get the output with format (dd/mm/yyyy).

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    You are using terrible date-time classes that are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jan 21 '19 at 20:55

1 Answers1

2

You don’t need an XMLGregorianCalender. It will not, cannot give you what you ask for. Instead you need a LocalDate and a DateTimeFormatter:

    DateTimeFormatter italianDateFormatter
            = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
                    .withLocale(Locale.ITALIAN);
    LocalDate date = LocalDate.now(ZoneId.of("Europe/Rome"));
    String formattedDate = date.format(italianDateFormatter);
    System.out.println(formattedDate);

When I ran this code today, the output was:

21/01/19

The difference from what you ask for is the two digit year, 19. If instead of FormatStyle.SHORT we use FormatStyle.MEDIUM, we get four digit year, but the month as a three letter abbreviaiton:

21-gen-2019

The advantage is that the code lends itself very well to internationalization: you just need to change the locale to get proper code for some other language and country. If you do need 21/01/2019 (with four digit year), specify the format explicitly using a pattern:

    DateTimeFormatter italianDateFormatter
            = DateTimeFormatter.ofPattern("dd/MM/uuuu");

21/01/2019

What’s wrong with using XMLGregorianCalendar?

When you call System.out.println(xmlDate), you are implicitly calling toString on your XMLGregorianCalendar. When it hasn’t got time and zone offset, toString always generates yyyy-MM-dd format, there is no way you can change that. On the other hand you can have any format you like in a String. Next obstacle is, there is no formatter that can format an XMLGregorianCalendar directly. You would need to convert to a different type, like ZonedDateTime, for example, first. Since you only want the date, neither the time of day nor the time zone, it’s simpler and easier to start out from LocalDate from the start. Not least for those reading and maintaining your code after you.

Your question mentions java.util.Date and your code uses GregorianCalendar too. Both of those classes are poorly designed and long outdated, fully replaced by java.time, the modern Java date and time API. So I suggest you don’t use them.

Link

Oracle tutorial: Date Time explaining how to use java.time, the modern Java date and time API.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Actually i am am a consumer of SOAP web-service and the date attribute is defined as XMLGegorianCalendar... – user1711472 Jan 21 '19 at 22:31
  • But when the `XMLGregorianCalendar` is passed through SOAP, it doesn’t have any format. @user1711472 Are you sending the `XMLGregorianCalendar`to the web service or receiving it from there? – Ole V.V. Jan 22 '19 at 07:55
  • I am using it to send the data to another application which in turn expecting the format as dd/mm/yyyy – user1711472 Jan 22 '19 at 11:40
  • Either you misunderstood or they have impossible requirements. Nobody in the world can send them an `XMLGregorianCalendar` in that format. As I said, it doesn’t exist. You may want to quote their specs in your question so we can help you decipher. – Ole V.V. Jan 22 '19 at 12:28