我正在检索列表,并希望从该列表中获得一个要说date
的特定列,但是在该特定列中,我只希望日期为2019年,因为该列同时包含2018年和2019。
Iterator<Map<String, Object>> itr = listEnquiry.iterator();
while (itr.hasNext()) {
map = itr.next();
CustomerBean bean = (CustomerBean) map.get("customerBean");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String date = simpleDateFormat.format(bean.getCustomerCreationDate()); // it is giving me date column which contains both year.
}
我得到的输出是-
2018-12-30
2018-12-30
2019-03-13
2018-12-30
但是我想要这样的输出
2019-03-13
那么我该如何接受。我做不到。
答案 0 :(得分:0)
假设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