java.sql.Date to joda time conversion
16,309
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.
Related videos on Youtube
Comments
-
turbanoff almost 2 years
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?
-
Kushan over 12 yearsi think this time zone problem, check with correct time zone?
-
-
turbanoff over 12 yearsWhat if time will not increase, and subtract?
-
BillRobertson42 over 12 yearsCan you be more specific? I'm not sure what you are referring to.
toDateMidngith
returns aLocalDate
object, and it can only represent dates, not times. Is that the problem? If so, then it may not be what you're looking for. IfwithMillisOfDay()
works for you then stick with it. -
Aram Kocharyan about 10 yearsJust a note - I was dealing with
java.sql.Timestamp
and notjava.sql.Date
, so usingnew DateTime(java.sql.Timestamp.valueOf(someSqlDate));
did the trick. -
Basil Bourque about 10 yearsFYI, the Joda-Time team no longer recommends the "midnight"-related classes and methods. They added the method
withTimeAtStartOfDay
to the DateTime class instead.