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 8.
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, 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)
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 current zone 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 below code sample, we are creating instance of XMLGregorianCalendar
with current Date
object. Then I am formatting it to String format in given pattern.
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import java.util.TimeZone; 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 DateFormat TIMESTAMP_FORMATTER = new SimpleDateFormat(TIMESTAMP_PATTERN); private final static TimeZone IST_TIMEZONE = TimeZone.getTimeZone("IST"); public static String formatTimeStamp(XMLGregorianCalendar cal) { if (cal == null) return ""; else { return TIMESTAMP_FORMATTER.format(cal.toGregorianCalendar(IST_TIMEZONE, Locale.US, null).getTime()); } } public static void main(String[] args) throws DatatypeConfigurationException { GregorianCalendar cal = new GregorianCalendar(); cal.setTime(new Date()); XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal); String formmatedDateTimestamp = formatTimeStamp(calendar); System.out.println(formmatedDateTimestamp); } }
Output:
01/27/2017 01:32 PM IST
We can use above code to formar XMLGregorianCalendar instance to string in any pattern of our choice.
Happy Learning!!
Reference:
XMLGregorianCalendar Java Doc