Предполагая, что bean.getCustomerCreationDate()
возвращает объект java.util.Date
, попробуйте добавить следующее в цикл while внизу:
public class Application {
public static void main(String[] args) {
HashMap<String, Object> map1 = new HashMap<String, Object>();
map1.put("customerBean",new CustomerBean("2018-12-30") );
HashMap<String, Object> map2 = new HashMap<String, Object>();
map2.put("customerBean",new CustomerBean("2018-03-13") );
HashMap<String, Object> map3 = new HashMap<String, Object>();
map3.put("customerBean",new CustomerBean("2018-12-30") );
HashMap<String, Object> map4 = new HashMap<String, Object>();
map4.put("customerBean",new CustomerBean("2019-03-01") );
HashMap<String, Object> map5 = new HashMap<String, Object>();
map5.put("customerBean",new CustomerBean("2017-05-16") );
HashMap<String, Object> map6 = new HashMap<String, Object>();
map6.put("customerBean",new CustomerBean("2019-01-23") );
ArrayList<Map<String, Object>> listEnquiry = new ArrayList<Map<String,Object>>();
listEnquiry.add(map1);
listEnquiry.add(map2);
listEnquiry.add(map3);
listEnquiry.add(map4);
listEnquiry.add(map5);
listEnquiry.add(map6);
ArrayList<CustomerBean> newValues = new ArrayList<>(); //List will contain all objects with 2019 values
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Iterator<Map<String, Object>> itr = listEnquiry.iterator();
while (itr.hasNext()) {
Map<String, Object> map = itr.next();
CustomerBean bean = (CustomerBean) map.get("customerBean");
//String date = simpleDateFormat.format(bean.getCustomerCreationDate()); // it is giving me date column which contains both year.
String date = "";
Calendar cal = Calendar.getInstance();
cal.setTime(bean.getCustomerCreationDate());
if (cal.get(Calendar.YEAR)==2019) {
date = simpleDateFormat.format(bean.getCustomerCreationDate());
newValues.add(bean);
//System.out.println(date); Simple print the date
}
}
newValues.forEach((v)->System.out.println(simpleDateFormat.format(v.getCustomerCreationDate())));
}
private static class CustomerBean {
Date date = new Date();
public CustomerBean(String date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
this.date = format.parse(date);
} catch (ParseException e) {}
}
public Date getCustomerCreationDate() {
return this.date;
}
}
}
Это дает мне вывод
2019-03-01
2019-01-23