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?
Converting XMLGregorianCalendar to Date getTime() method returns the java. util. Date object. XMLGregorianCalendar xCal = ..; //Create instance Date date = xCal.
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).
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).
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With