Akhilesh Mohare
Akhilesh Mohare

Reputation: 45

Convert XMLGregorianCalendar to oracle timestamp value

I need to convert XMLGregorianCalendar to Oracle Timestampformat

My database data type is of Timestamp format.

Trying to execute the query using Java.

Upvotes: 0

Views: 4398

Answers (1)

cosbor11
cosbor11

Reputation: 16054

By default the XMLGregorianCalendar#toString() method returns the date string in ISO 8601 format "YYYY-MM-DDThh:mm:ssTZD", For example: "2014-12-10T12:49:28-08:00", with 'T' being a delimiter for Time. A shortcut hack would be to replace the 'T' with a space, but that is not the preferable option in my opinion.

Since you need the date string in a format that is compatible with Oracle's TO_TIMESTAMP() function, something like this: "YYYY-MM-DD HH24:MI:SS.FF3", you could convert it to a java.sql.Timestamp by passing in the milliseconds.

XMLGregorianCalendar now = new XMLGregorianCalendar();
GregorianCalendar calenderNow = now.toGregorianCalendar();
Timestamp createOnTimestamp = new Timestamp(calenderNow.getTimeInMillis()));

If you need to pass the timestamp to a prepared statement as a formatted String you could do this:

String createdOnString = createOnTimestamp.toString(); 
//returns yyyy-MM-dd HH:mm:ss.SSS 

or if you have a specific format you want to use:

String createdOnString = new SimpleDateFormat("yourformat").format(createOnTimestamp);

*Note: you can see what Oracle's default timestamp format is by running the following query:

SELECT value FROM v$nls_parameters WHERE parameter = 'NLS_TIMESTAMP_FORMAT'; 

Upvotes: 2

Related Questions