简体   繁体   中英

How to retrieve paricular value from map which contains list?

I am retrieving the list and want a particular column say date from that list which I am getting but in that particular column, I want date which is of the year 2019 only as that column contains both year 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.
}

The output which I am getting is-

2018-12-30
2018-12-30
2019-03-13
2018-12-30

But I want the output like that

2019-03-13

So How can I acieve this. I am unable to do.

Assuming that bean.getCustomerCreationDate() returns a java.util.Date object then try adding the following inside your while loop at the bottom:

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;
        }

    }

}

This gives me the output

2019-03-01
2019-01-23

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM