Converting java.sql.Date & java.util.Date to org.joda.time.LocalDate Converting java.sql.Date & java.util.Date to org.joda.time.LocalDate sql sql

Converting java.sql.Date & java.util.Date to org.joda.time.LocalDate


Your code using the ternary operator cannot be that terse precisely because you make two trips to the database. Consider writing a dateutil library with a method like this:

    LocalDate convertToLocalDate(Date date) {        if(date == null) return null;        return new LocalDate(date);    }

IMO making this code clean up at the expense of an often used lightweight static method is a good trade.

Also consider not using Java. In Javascript you could just use || and not have this problem, for instance. I also hear Scala is a good language that solves this with more explicit support for Nullable types. As long as you're cleaning up old code, may as well do it right.


To Convert java.util.Date to org.joda.time.LocalDate

    public static LocalDate convertUtilDateToLocalDate(Date date) {            if(date==null) return null;            DateTime dt = new DateTime(date);            return dt.toLocalDate();        }