Search code examples
javasqliteresultset

Get a date from resultset that is already in localdate format?


My SQLlite database uses the format yyyy-mm-dd for dates. When I get a resulset from my database, I want to store the day of birth I got from my database by using a method from a different class. However, i'm not sure how to store it.

public void setBirthDay(LocalDate birthDay) {
    this.birthDay = birthDay;
}


     try (PreparedStatement stmt = conn.prepareStatement(
        "select id, naam, voornaam, birthday, opmerking, debetstand_limiet, actief from klant where id = ?");) {
        stmt.setInt(1, id);
        stmt.execute();
        try (ResultSet r = stmt.getResultSet()) {
           Klant k = new Klant();


           if (r.next()) {
              k.setBirthDay(r.getDate("birthday").toLocalDate());
    }

This is what I'm trying now, but is this even correct? The format is in LocalDate already, so why would i have to change it to LocalDate still? What is correct to do in this scenario?


Solution

  • String theOtherDay = "2011-07-12";
    LocalDate today = LocalDate.now();
    LocalDate tod = LocalDate.parse(theOtherDay);
    
    System.out.println(today.toString());
    System.out.println(tod.toString());
    
    //2016-07-19
    //2011-07-12
    

    you can store the LocalDates objects in an arrayList if need be.