javasqloracle-databasejodatime

java.sql.Date to joda time conversion


oracle sql:

select trunc( sysdate, 'Month') month
from dual

java:

java.sql.Date sqlDate = resultSet.getDate("month");
log.info(sqlDate);
DateTime dateTime = new DateTime(sqlDate.getTime());
log.info(dateTime);
dateTime = dateTime.withMillisOfDay(0);
log.info(dateTime);

output:

2012-01-01

2012-01-01T 01:00:00.000+07:00

2012-01-01T 00:00:00.000+07:00

where did the extra hour?


Solution

  • Use LocalDate.fromDateFields(date) to interpret the SQL date as local (ignoring time-zone). You can then use methods on LocalDate to get a DateTime if necessary (although if your object really is "just a date" then LocalDate is the right object to use.