0

I'm using the BeanListHandler to convert rows from my database into POJOs. It has been working fine but now I need to do some date comparisons. For this I decided to use JodaTime.

I want all Timestamp/Date type rows from my query's result set to be converted into JodaTime's DateTime class instead of the default Timestamp. I tried using the setter to do this but BeanListHandler does not seem to be calling the setters to set the values (does it use reflection?).

How should I go about doing this?

user3690467
  • 3,049
  • 6
  • 27
  • 54
  • If introducing an external library into your project, why not [ThreeTen Backport](http://www.threeten.org/threetenbp/)? This gives you `java.time`, the modern Java date and time API. Quotes from the Joda-Time home page: “Users are now asked to migrate to `java.time` (JSR-310).” “Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to `java.time` (JSR-310).” Even if using Java 6 or 7, you can take that step today. – Ole V.V. Dec 20 '17 at 14:57

1 Answers1

0

I don't think there is a straight forward solution. One work around is to have both Timestamp and DateTime fields in your POJO and set the DateTime value in your Timestamp attribute's setter method as shown in the example below.

public class Pojo{
    private Timestamp timestamp;
    private DateTime dateTime;

    public Timestamp getTimestamp() {
        if (timestamp == null && dateTime != null) {
            this.timestamp = new Timestamp(dateTime.getMillis());
        }
        return timestamp;
    }

    public void setTimestamp(Timestamp timestamp) {
        if (dateTime == null && timestamp != null) {
            this.dateTime = new DateTime(timestamp.getTime());
        }
        this.timestamp = timestamp;
    }

    public DateTime getDateTime() {
        if (dateTime == null && timestamp != null) {
            this.dateTime = new DateTime(timestamp.getTime());
        }
        return dateTime;
    }

    public void setDateTime(DateTime dateTime) {
        if (timestamp == null && dateTime != null) {
            this.timestamp = new Timestamp(dateTime.getMillis());
        }
        this.dateTime = dateTime;
    }



}
Justin Jose
  • 2,121
  • 1
  • 16
  • 15