Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to convert XMLGregorianCalendar to MM/dd/yyyy hh:mm String?

Tags:

java

xml

What is the best way to convert XMLGregorianCalendar objects to 'MM/dd/yyyy hh:mm' String?

like image 597
kostepanych Avatar asked Feb 04 '13 14:02

kostepanych


People also ask

How to convert XMLGregorianCalendar to Date format?

The simplest way to format XMLGregorianCalendar is to first convert it to the Date object, and format the Date to String. XMLGregorianCalendar xCal = ..; //Create instance Date date = xCal. toGregorianCalendar(). getTime(); DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a z"); String formattedString = df.

How to convert XMLGregorianCalendar into Date in Java?

Since java. util. Date is the most common way to deal with Java date and time, we always need to convert the instance of XMLGregorianCalendar to the Java Date instance. Using the Java API, we can easily convert XMLGregorianCalendar to XMLGregorianCalendar Date and Date in Java.

What is XMLGregorianCalendar?

The XML Schema standard defines clear rules for specifying dates in XML format. In order to use this format, the Java class XMLGregorianCalendar, introduced in Java 1.5, is a representation of the W3C XML Schema 1.0 date/time datatypes.


1 Answers

First use XMLGregorianCalendar#toGregorianCalendar() to get a java.util.Calendar instance out of it.

Calendar calendar = xmlGregorianCalendar.toGregorianCalendar(); 

From that step on, it's all obvious with a little help of SimpleDateFormat the usual way.

SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm"); formatter.setTimeZone(calendar.getTimeZone()); String dateString = formatter.format(calendar.getTime()); 

I only wonder if you don't actually want to use HH instead of hh as you aren't formatting the am/pm marker anywhere.

like image 167
BalusC Avatar answered Sep 20 '22 07:09

BalusC