'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;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
