0

I am not able to convert XMLGregorianCalendar dateTime into format dd/mm/yyyy HH:mm.

public class HelloWorld{

 public static void main(String []args){

     XMLGregorianCalendar Datetime ="2019-04-11T05:00:54.000+01:00";

    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");


      String requiredDate = formatter.format(Datetime);

     System.out.println("requiredDate" + requiredDate);

 }
}

I am getting an error as----

incompatible types: String cannot be converted to XMLGregorianCalendar
     XMLGregorianCalendar Datetime ="2019-04-11T05:00:54.000+01:00";
Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41
ASM
  • 27
  • 1
  • 1
  • 9
  • 1
    Refer [this](https://stackoverflow.com/a/835963/9472892). It might be helpful for you – Anchit Apr 12 '19 at 06:09
  • 1
    Possible duplicate of [java.util.Date to XMLGregorianCalendar](https://stackoverflow.com/questions/835889/java-util-date-to-xmlgregoriancalendar) – vezunchik Apr 12 '19 at 06:11
  • 1
    Do you just want to convert a date String from one format to the other? If so, you don't have to use `XMLGregorianCalendar`. – TiiJ7 Apr 12 '19 at 07:03
  • 1
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 12 '19 at 07:12
  • Isn’t this just a duplicate of [Changing String date format](https://stackoverflow.com/questions/34846358/changing-string-date-format) (and many other similar questions)? – Ole V.V. Apr 12 '19 at 07:13
  • how the above thing can be done using Javascript ?? – ASM Sep 18 '19 at 06:09

2 Answers2

2

java.time

    DateTimeFormatter requiredFormatter
            = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
                    .withLocale(Locale.FRENCH);

    String originalDateTimeString = "2019-04-11T05:00:54.000+01:00";
    OffsetDateTime dateTime = OffsetDateTime.parse(originalDateTimeString);
    String requiredDate = dateTime.format(requiredFormatter);
    System.out.println("requiredDate: " + requiredDate);

Output from this snippet is (on Java 9.0.4):

requiredDate: 11/04/2019 05:00

Use Java’s built-in localized date and time formats. Don’t bother with rolling your own formatter through a format pattern string. In most cases Java knows better what your audience expects, writing a format pattern string is error-prone, and code using a built-in format lends itself better to porting to a different locale. I don’t know whether your desired locale is French, of course, since many other locales fit the format you asked for, dd/mm/yyyy HH:mm, too. Just pick the locale that is right for your situation.

The date/time classes you tried to use, XMLGregorianCalendar and SimpleDateFormat, are old now, and the latter notoriously troublesome, so you shouldn’t use those if you can avoid it (which you can). Instead I am using java.time, the modern Java date and time API.

What went wrong in your code?

  • Minor point, variable names in Java begin with a small letter, so your variable should be called dateTime or datetime.
  • You cannot assign a string (like "2019-04-11T05:00:54.000+01:00") to a variable of type XMLGregorianCalendar. This is what your error message is trying to tell you. The correct way to convert would have been the one already shown in zmr’s answer:

       XMLGregorianCalendar dateTime = DatatypeFactory.newInstance()
               .newXMLGregorianCalendar("2019-04-11T05:00:54.000+01:00");
    
  • A SimpleDateFormat cannot format an XMLGregorianCalendar. The code compiles, but at runtime you get a java.lang.IllegalArgumentException: Cannot format given Object as a Date.

Link

Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

You are trying to assign dateTimeStamp to XMLGregorianCalendar which is abstract class.

You can achieve the result by using below code snippet :-

public static void main(String[] args) throws DatatypeConfigurationException {
    XMLGregorianCalendar datetime = DatatypeFactory.newInstance()
            .newXMLGregorianCalendar("2019-04-11T05:00:54.000+01:00");

    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");

    String requiredDate = formatter.format(datetime.toGregorianCalendar().getTime());

    System.out.println("requiredDate" + requiredDate);
}
zmr
  • 29
  • 6