I have an array of objects that come from the database, I have to keep the object whose date is the highest, that is, the last one that was inserted.
2018-10-09 10:11:50
2018-11-01 13:11:22
2018-11-01 14:20:10 --> esta
private ThirdValidation maxRegister(List<ThirdValidation> process) {
ThirdValidation tv = null, maxTv = null;
String strDateFormat = "aaaa-MM-dd hh:mm:ss"; // Formato de la fecha
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
try {
// se comprueba el obj con la fecha más alta.
for (int i = 0; i < process.size(); i++) {
tv = process.get(i);
if(maxTv == null) {
maxTv = tv;
} else {
Date max = sdf.parse(maxTv.getRegistroModif());
Date current = sdf.parse(tv.getRegistroModif());
// El max es menor que el actual
if(max.compareTo(current) == 0) {
maxTv = tv;
}
}
}
} catch (Exception e) {
LOGGER.warn("******************");
LOGGER.warn("ERROR maxRegister()");
e.printStackTrace();
LOGGER.warn("******************");
}
return maxTv;
}
It gives an error when it tries to do:
maxTv.getRegistroModif()
, I have verified that it has data and they are 2018-10-09 10:11:50
, the error is: java.text.ParseException: Unparseable date but previously I already specified the format: String strDateFormat = "aaaa-MM-dd hh:mm:ss";
// Date format . Thank you!
Simply changing:
For the following:
It should work. Also add that if what you want is to know if it is greater, in the conditional change the
==
by<
.