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.

Share:
16,309

Related videos on Youtube

turbanoff
Author by

turbanoff

Developer (Java, C#, Oracle)

Updated on June 04, 2022

Comments

  • turbanoff
    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
      Kushan over 12 years
      i think this time zone problem, check with correct time zone?
  • turbanoff
    turbanoff over 12 years
    What if time will not increase, and subtract?
  • BillRobertson42
    BillRobertson42 over 12 years
    Can you be more specific? I'm not sure what you are referring to. toDateMidngith returns a LocalDate 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. If withMillisOfDay() works for you then stick with it.
  • Aram Kocharyan
    Aram Kocharyan about 10 years
    Just a note - I was dealing with java.sql.Timestamp and not java.sql.Date, so using new DateTime(java.sql.Timestamp.valueOf(someSqlDate)); did the trick.
  • Basil Bourque
    Basil Bourque about 10 years
    FYI, the Joda-Time team no longer recommends the "midnight"-related classes and methods. They added the method withTimeAtStartOfDay to the DateTime class instead.