-3

I am parsing date field from xml and store it into oracle db for my requirement. my xml date looks like

<BirthDate>1960-10-24T00:00:00</BirthDate>

during unmarhsalling the xml, i am getting XMLGregorianCalendar data type for this date field.So, am converting this object into java.util.Date and then save it to oracle DB. But it is updated with one day minus. For example, it is updated like 23-10-1960.. How to fix this issue and get it updated with the actual date on the xml.

This is my code:

myEntity.setBirthDate(xmlDomain.getBirthDate().toGregorianCalendar().getTime());
khelwood
  • 55,782
  • 14
  • 81
  • 108
Saravanan
  • 11,372
  • 43
  • 143
  • 213

1 Answers1

2

Avoid legacy date-time classes

You should not be using java.util.Date. That terrible class was supplanted years ago with the adoption of JSR 310.

java.time

Convert from your legacy XMLGregorianCalendar to the modern ZonedDateTime class by way of another terrible legacy class, GregorianCalendar.

ZonedDateTime zdt = myXmlGregCal.toGregorianCalendar().toZonedDateTime() ;

TIMESTAMP WITH TIME ZONE column

To write this moment to the database in a column of a type akin to the SQL-standard type TIMESTAMP WITH TIME ZONE, using JDBC 4.2, convert the ZonedDateTime to an OffsetDateTime. Oddly, the JDBC spec requires support for OffsetDateTime but not Instant or ZonedDateTime.

OffsetDateTime odt = zdt.toOffsetDateTime() ;

Pass to your prepared statement.

myPreparedStatement.setObject( … , odt ) ;

Retrieval.

OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;

DATE column

If writing to a date-only type column, akin to the SQL-standard type DATE, then use LocalDate class.

LocalDate ld = zdt.toLocalDate() ;  // If the zone of that ZonedDateTime object is the zone by which you want to perceive the date.
myPreparedStatement.setObject( … , ld ) ;

Retrieval.

LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154