How to format the data based on Date
I am using TreeMap and want to sort based on Date
I have a List of Persons and i want to sort based on dateOfBirth
package com;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.Map;
    import java.util.TreeMap;
    public class GroupByDemoInJava8 {
        public static void main(String args[]) throws Exception {
            try {
                List<Person> personList = new ArrayList<>(); // Date Format is MM/DD/YYYY
                personList.add(new Person("Mike", "London", 21, "01/01/1981"));
                personList.add(new Person("John", "London", 21, "01/02/1981"));
                personList.add(new Person("Prasanna", "London", 23, "04/28/1990"));
                personList.add(new Person("Monobo", "Tokyo", 23, "04/28/1990"));
                personList.add(new Person("Sam", "Paris", 23, "07/12/1992"));
                personList.add(new Person("Nadal", "Paris", 31, "04/02/1992"));
                String patternInput = "MM/dd/yyyy";
                SimpleDateFormat simpleDateFormatInput = new SimpleDateFormat(patternInput);
                String outputPattern = "MMM-yy";
                SimpleDateFormat simpleDateFormatOutput = new SimpleDateFormat(outputPattern);
                Map<String, List<Person>> personByMap = new TreeMap<String, List<Person>>();
                for (Person p : personList) {
                    Date inputDate = simpleDateFormatInput.parse(p.getDateOfBirth());
                    String outPutDate = simpleDateFormatOutput.format(inputDate);
                    if (!personByMap.containsKey(outPutDate)) {
                        personByMap.put(outPutDate, new ArrayList<>());
                    }
                    personByMap.get(outPutDate).add(p);
                }
                System.out.println(personByMap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    class Person {
        private String name;
        private String city;
        private int age;
        private String dateOfBirth;
        public String getDateOfBirth() {
            return dateOfBirth;
        }
        public void setDateOfBirth(String dateOfBirth) {
            this.dateOfBirth = dateOfBirth;
        }
        public Person(String name, String city, int age, String dateOfBirth) {
            this.name = name;
            this.city = city;
            this.age = age;
            this.dateOfBirth = dateOfBirth;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getCity() {
            return city;
        }
        public void setCity(String city) {
            this.city = city;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        @Override
        public String toString() {
            return "Person [name=" + name + ", city=" + city + ", age=" + age + "]";
        }
    }
 
     
    