Learn to format XMLGregorianCalendar
instance to string in multiple patterns e.g. ‘MM/dd/yyyy hh:mm a z
‘ using DateTimeFormatter and SimpleDateFormat classes in Java.
1. SimpleDateFormat vs DateTimeFormatter
Before Java 8, the standard Java approach for dates and times was via the Date
and Calendar
classes and the standard approach to parsing and formatting dates was via DateFormat
and SimpleDateFormat
.
With Java 8, the preferred date/time classes are in the java.time
package e.g. LocalDate
, LocalDateTime
and ZonedDateTime
.
Similarly, the preferred date/time formatting/parsing classes are no longer in the java.text
package, but instead come from the java.time.format
package.
Apart from the way these classes are used and the methods in them, the most noticeable difference is the behavior in concurrent applications.
SimpleDateFormat
is NOT thread-safe.DateTimeFormatter
is thread-safe.
So there is always a good reason to use DateTimeFormatter over SimpleDateFormat.
2. DateTimeFormatter (Recommended)
The best way to format, since Java 8, is to convert the calendar instance to ZonedDateTime and then use DateTimeFormatter to format it. The excellent support of timezones and offsets in ZonedDateTime will be useful when dealing with multiple locales.
If we do not need the locale information, then we can simply convert the ZonedDateTime to LocalDateTime or LocalDate and print the localized formatted outputs.
Java program to format XMLGregorianCalendar using DateTimeFormatter.
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class Main
{
private final static String TIMESTAMP_PATTERN
= "MM/dd/yyyy hh:mm a z";
private final static DateTimeFormatter DATE_TIME_FORMATTER
= DateTimeFormatter.ofPattern(TIMESTAMP_PATTERN);
public static void main(String[] args)
throws DatatypeConfigurationException
{
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new Date());
XMLGregorianCalendar xgc = DatatypeFactory.newInstance()
.newXMLGregorianCalendar(cal);
//1. Convert XMLGregorianCalendar to ZonedDateTime in default timezone
ZonedDateTime zdt = xgc.toGregorianCalendar().toZonedDateTime();
System.out.println( DATE_TIME_FORMATTER.format(zdt) );
//2. Convert XMLGregorianCalendar to ZonedDateTime in UTC
ZonedDateTime zdtUTC = zdt.withZoneSameInstant(ZoneId.of("UTC"));
System.out.println( DATE_TIME_FORMATTER.format(zdtUTC) );
}
}
Program output.
08/23/2019 01:53 PM GMT+05:30
08/23/2019 08:23 AM UTC
3. Format XMLGregorianCalendar with SimpleDateFormat
In the below code, we are creating an instance of XMLGregorianCalendar
with current Date
object. Then we are formatting it to String using SimpleDateFormat.
XMLGregorianCalendar xCal = ..; //Create instance
Date date = xCal.toGregorianCalendar().getTime();
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a z");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
String formattedString = df.format(date);
System.out.println(formattedString);
Output:
02/18/2022 12:08 am IST
We can use the above code to format XMLGregorianCalendar instance to string in any pattern of our choice.
Happy Learning!!