Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert XMLGregorianCalendar to date i.e "MM/DD/YYYY hh:mm:ss AM"

I have a date in XMLGregorianCalendar format like "2013-05-16T09:54:13" which i have to convert to timestamp "MM/DD/YYYY hh:mm:ss AM" for inserting into oracle database table using java.

how can i do this in Java?

like image 839
Suniel Avatar asked May 23 '13 05:05

Suniel


People also ask

How to convert XMLGregorianCalendar to date?

Converting XMLGregorianCalendar to Date getTime() method returns the java. util. Date object. XMLGregorianCalendar xCal = ..; //Create instance Date date = xCal.

How do I convert a date timestamp to date?

The constructor of the Date class receives a long value as an argument. Since the constructor of the Date class requires a long value, we need to convert the Timestamp object into a long value using the getTime() method of the TimeStamp class(present in SQL package).

Can we convert date to timestamp?

We can convert date to timestamp using the Timestamp class which is present in the SQL package. The constructor of the time-stamp class requires a long value. So data needs to be converted into a long value by using the getTime() method of the date class(which is present in the util package).


1 Answers

You can do this to return a Date:

calendar.toGregorianCalendar().getTime() 

I found that code from this tutorial. From there, you can use a SimpleDateFormat to turn it into a string in the format you want.

But, if you're using JDBC to save the date in the database, you probably can pass in the Date directly with this method:

preparedStatement.setDate(colNum, myDate); 
like image 156
Daniel Kaplan Avatar answered Oct 18 '22 19:10

Daniel Kaplan