Samarland
Samarland

Reputation: 581

Set XMLGregorianCalendar value from a Calendar date in Java

what is the best way to set a XMLGregorianCalendar value from a Calendar date?

    posting.setXMLGregorianCalendar(message.getCreateDate());

here my class:

Message.java

private Calendar createDate;
public Calendar getCreateDate() {
    return createDate;
}

Upvotes: 1

Views: 5761

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 338181

tl;dr

DatatypeFactory.newInstance().newXMLGregorianCalendar( 
    GregorianCalendar.from( 
        ZonedDateTime.now( 
            ZoneId.of( "Pacific/Auckland" ) 
        ) 
    )
)

A moment requires a time zone

The XMLGregorianCalendar, GregorianCalendar, and Calendar classes all represent a moment, that is, a date, a time-of-day, and an assigned time zone. Stay aware of the issue of time zone. If you fail to address zone explicitly, the JVM’s current default time zone will be assigned implicitly.

Specify a proper time zone name in the format of Continent/Region, such as America/Edmonton, Africa/Tunis, or Europe/Moscow. Never use the 2-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;  

If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the code becomes ambiguous to read in that we do not know for certain if you intended to use the default or if you, like so many programmers, were unaware of the issue.

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

java.time

These …Calendar classes are also terrible. Sun, Oracle, and the JCP community gave up on them years ago with the adoption of JSR 310… and so should you. Use only the java.time classes for your business logic.

ZoneId z = ZoneId.of( "Africa/Casablanca" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;   // Capture the current moment as seen through the wall-clock time used by the people of a particular region (a time zone).

Convert

If interacting with old code not yet updated to java.time, convert between legacy classes and modern classes. Look to new methods added to the old classes.

A ZonedDateTime object can be converted to a GregorianCalendar object (a subclass of Calendar) by calling GregorianCalendar.from( ZonedDateTime ).

GregorianCalendar gregCal = GregorianCalendar.from( zdt ) ;

A GregorianCalendar can be converted to a XMLGregorianCalendar, but we have to go to one extra step. The XMLGregorianCalendar class itself lacks a converter method from GregorianCalendar. A helper class can get the job done, DatatypeFactory, as shown in this Answer by Stephen Colebourne, “JodaStephen”, the inventor of java.time.

XMLGregorianCalendar xmlGregCal = DatatypeFactory.newInstance().newXMLGregorianCalendar( gregCal );

Not that I recommend it, but you can combine this into a one-liner as seen in the tl;dr section up top.

enter image description here

Upvotes: 1

Jagadesh
Jagadesh

Reputation: 2116

You can try out this:

Calendar createDate = Calendar.getInstance();
Date cDate = createDate.getTime();
GregorianCalendar c = new GregorianCalendar();
c.setTime(cDate);
XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);

Upvotes: 1

Related Questions