如何根据日期格式化数据

时间:2019-07-26 11:03:16

标签: java

如何根据日期格式化数据

我正在使用TreeMap,并希望根据日期进行排序

我有一个人员列表,我想根据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 + "]";
        }
    }

2 个答案:

答案 0 :(得分:1)

新的java time classes是一项巨大的改进。对于生日,MonthDay是理想的选择。 它是Comparable<MonthDay>,因此您可以使用它进行排序。

可以通过创建Comparator进行排序。在这里,我首先通过一个人到出生日期的映射方法,然后再通过其名称的人获取器来构成比较器。

Map<String, List<Person>> personByMap = new TreeMap<>(
        Comparator.comparing(this::birthDay)
                  .thenComparing(Person::getName));

MonthDay birthDay(String date) {
    LocalDate d = LocaleDate.parse(date, DateTimeFormatter.ofPattern("MM/dd/yyyy"));
    return MonthDay.from(d);
}

答案 1 :(得分:0)

您需要的是为只比较出生日期的人定制的比较器。

Collections.sort(personList, new Comparator<Person>() {
        @Override
        public int compare(Person o1, Person o2) {
            //access your formatter simpleDateFormatInput here.
            return simpleDateFormatInput.format(o1.dateOfBirth).compareTo(simpleDateFormatInput.format(o2.dateOfBirth));
        }
    });
相关问题