Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String date to xmlgregoriancalendar conversion

Tags:

java

string

date

I have written this function :

public static XMLGregorianCalendar getXMLGregorianCalendar(String date) throws OmniException{     XMLGregorianCalendar xmlCalender=null;     GregorianCalendar calender = new GregorianCalendar();     calender.setTime(Util.stringToJavaDate(date));     xmlCalender = DatatypeFactory.newInstance().newXMLGregorianCalendar(calender);     return xmlCalender; }  public static Date  stringToJavaDate(String sDate)  throws OmniException{     Date date=null;     date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(sDate);             return date;   } 

When i am passing date as "2014-01-07" am getting output date as 2014-01-06T18:30:00:000Z where i am going wrong ? also what to do if I want to get only 2014-01-06T18:30:00 and 2014-01-06T18:30:00Z
Any help is appreciated

like image 302
JavaStudent Avatar asked Jan 07 '14 11:01

JavaStudent


People also ask

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.

How do you convert GregorianCalendar to XMLGregorianCalendar?

DatatypeFactory object can convert from GregorianCalendar to XMLGregorianCalendar by calling its newXMLGregorianCalendar method. XMLGregorianCalendar xmlGregCal = DatatypeFactory . newInstance() .

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.

How do I create an instance of XMLGregorianCalendar?

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.


1 Answers

Found the solution as below.... posting it as it could help somebody else too :)

DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = format.parse("2014-04-24 11:15:00");  GregorianCalendar cal = new GregorianCalendar(); cal.setTime(date);  XMLGregorianCalendar xmlGregCal =  DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);  System.out.println(xmlGregCal); 

Output:

2014-04-24T11:15:00.000+02:00

like image 147
JavaStudent Avatar answered Oct 02 '22 15:10

JavaStudent