Search code examples
hibernatemethodsstreamresultsetdate-range

How to extract dates from an Object in Java


I should extract dates from an Object, but the compiler rightly tells me that getDate() is not defined on Object. How can I define it? If I can't define it, what did I do wrong? Should I use the Hibernate library or streams? Thank you. To transform an Object into DateRange [] I did two steps: from Object -> MyObject then from MyObject-> DateRange []

class MyObject extends Object {

Date Lower_Date;
Date Upper_Date;

private MyObject(Date Lower_Date, Date Upper_Date) {
    this.Lower_Date = Lower_Date;
    this.Upper_Date = Upper_Date;
}

public Date getLowerDate() {
    return this.Lower_Date;
}

public Date getUpperDate() {
    return this.Upper_Date;
}

}

 ...

public class Tabella_Parlamentari {

public static DateRange getDateRange(Date a, Date b) {
    return new DateRange(a,b);
}   

...

try {   
         
                 if(periodo_carica == null)
                      periodo_carica = null;
                  else {
                      Array pgArray = rs.getArray("periodo_carica"); 
                      
                    Object obj = pgArray.getArray();
                    
                    List<Object> objlist = new ArrayList<Object>();
                    objlist.add(obj);               
                    
                    List<MyObject> myobjlist = new ArrayList<MyObject>();
                    myobjlist = objlist.stream()
                            .filter(e -> e != null)
                            .map(o -> (MyObject) obj.getDate()) //prima estrai tutte le date
                            .collect(Collectors.toList());
                    
                        List<DateRange> periodo_carica2 = new ArrayList<DateRange>();
                            periodo_carica2 = 
                                     myobjlist.stream()
                                     .filter(e -> e != null)
                                     .map(o -> getDateRange(o.getLowerDate(),o.getUpperDate()))
                                     .collect(Collectors.toList());
                            
                        periodo_carica = periodo_carica2.toArray(DateRange[]::new);         
                   
                     }
                                
              /*  Stream.Builder<DateRange> dateStreamBuilder = Stream.builder();

              Date[] date_intervallo = periodo_carica2.stream().toArray(Date[]::new);
             for(int i=0; i< periodo_carica2.size(); i++) {

                 periodo_carica2.stream().forEach(d -> DateRange(d.getLowerDate(), 
         d.getUpperDate()));
                 dateStreamBuilder.accept((DateRange)periodo_carica[i]);
           */
                // Stream<DateRange> dateStream = dateStreamBuilder.build();
             
            }
            catch(NullPointerException obj) {
                periodo_carica = null;
          }     

Solution

  • I don’t know your DateRange class, but it would seem to me that you may write your method in this way.

    public DateRange getDate() {
        return new DateRange(Lower_Date, Upper_Date);
    }
    

    The same will still work if both in MyObject and in DateRange you use LocalDate from java.time, the modern Java date and time API, instead of the old-fashioned java.util.Date.