Java:如何在相应的一周内对所有Java日期进行分组?

前端之家收集整理的这篇文章主要介绍了Java:如何在相应的一周内对所有Java日期进行分组?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
问题:

我有很多日期,我需要按周安排.

题:
如何按照他们所在的一周中的日期对日期进行分组?

示例数据集

Date date = new SimpleDateFormat.parse("04/01/2015")
Date date = new SimpleDateFormat.parse("04/02/2015")
Date date = new SimpleDateFormat.parse("04/03/2015")
Date date = new SimpleDateFormat.parse("04/04/2015")
Date date = new SimpleDateFormat.parse("04/05/2015")
Date date = new SimpleDateFormat.parse("04/06/2015")
Date date = new SimpleDateFormat.parse("04/07/2015")
Date date = new SimpleDateFormat.parse("04/08/2015")
Date date = new SimpleDateFormat.parse("04/09/2015")

期望的输出

HashMap<Date,Date> hashMap = groupByWeek(ArrayList<Date> dates);

printWeeklyGroupedDates();

Week 1:
04/01/2015
04/02/2015
04/03/2015
04/04/2015
04/05/2015
04/06/2015
04/07/2015
Week 2:
04/08/2015
04/09/2015

我试过的

public Date getWhichYearlyWeek(Date date){

  Calendar cal = Calendar.getInstance();
  cal.setTime(date);
  cal.get(Calendear.WEEK_OF_YEAR);

  //Need to return this as 'Date' but indicate which week this is for the TreeMap functionality below          

  return cal.getTime();
}


public TreeMap<Date,ArrayList<Date>> getWeeklyMappedDates(ArrayList<Date> dateArray){

  TreeMap<Date,ArrayList<Date>> treeMap = new TreeMap<Date,ArrayList<Date>>

  for(Date i : dateArray){

    Date date = getWhichYearlyWeek(date);

    if(!treeMap.containsKey(date))
      treeMap.get(date).add(date);
    else
      treeMap.put(date,new ArrayList<Date>());
  }

  return treeMap;
}

解决方法

您只需使用自定义Comparator对日期列表进行排序即可.下面的例子按年份排序日期,然后按日期排序,不知道你是否想要第二部分,但这是一个很好的演示……
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
            List<Date> dates = new ArrayList<Date>(25);
            dates.add(sdf.parse("04/01/2015"));
            dates.add(sdf.parse("04/02/2015"));
            dates.add(sdf.parse("04/03/2015"));
            dates.add(sdf.parse("04/04/2015"));
            dates.add(sdf.parse("04/05/2015"));
            dates.add(sdf.parse("04/06/2015"));
            dates.add(sdf.parse("04/07/2015"));
            dates.add(sdf.parse("04/08/2015"));
            dates.add(sdf.parse("04/09/2015"));

            int week = 0;
            int woy = -1;
            Collections.sort(dates,new WeekComparator());
            for (Date date : dates) {
                if (woy != getWeekOfYear(date)) {
                    woy = getWeekOfYear(date);
                    week++;
                    System.out.println("Week " + week + ":");
                }
                System.out.println(date);
            }

        } catch (ParseException exp) {
            exp.printStackTrace();
        }
    }

    public static class WeekComparator implements Comparator<Date> {

        @Override
        public int compare(Date o1,Date o2) {
            int result = getWeekOfYear(o1) - getWeekOfYear(o2);
            if (result == 0) {
                result = o1.compareTo(o2);
            }
            return result;
        }

    }

    protected static int getWeekOfYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.WEEK_OF_YEAR);
    }

}

哪个输出

Week 1:
Wed Apr 01 00:00:00 EST 2015
Thu Apr 02 00:00:00 EST 2015
Fri Apr 03 00:00:00 EST 2015
Sat Apr 04 00:00:00 EST 2015
Week 2:
Sun Apr 05 00:00:00 EST 2015
Mon Apr 06 00:00:00 EST 2015
Tue Apr 07 00:00:00 EST 2015
Wed Apr 08 00:00:00 EST 2015
Thu Apr 09 00:00:00 EST 2015

Java 8 Time API

仅仅因为我需要练习……

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.WeekFields;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

public class Test {

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy");

        List<LocalDate> dates = new ArrayList<LocalDate>(25);
        dates.add(LocalDate.parse("04/01/2015",dtf));
        dates.add(LocalDate.parse("04/02/2015",dtf));
        dates.add(LocalDate.parse("04/03/2015",dtf));
        dates.add(LocalDate.parse("04/04/2015",dtf));
        dates.add(LocalDate.parse("04/05/2015",dtf));
        dates.add(LocalDate.parse("04/06/2015",dtf));
        dates.add(LocalDate.parse("04/07/2015",dtf));
        dates.add(LocalDate.parse("04/08/2015",dtf));
        dates.add(LocalDate.parse("04/09/2015",dtf));

        int week = 0;
        int woy = -1;
        Collections.sort(dates,new WeekComparator());
        for (LocalDate date : dates) {
            if (woy != getWeekOfYear(date)) {
                woy = getWeekOfYear(date);
                week++;
                System.out.println("Week " + week + ":");
            }
            System.out.println(date);
        }
    }

    public static class WeekComparator implements Comparator<LocalDate> {

        @Override
        public int compare(LocalDate o1,LocalDate o2) {
            int result = getWeekOfYear(o1) - getWeekOfYear(o2);
            if (result == 0) {
                result = o1.compareTo(o2);
            }
            return result;
        }

    }

    protected static int getWeekOfYear(LocalDate date) {
        WeekFields wf = WeekFields.of(Locale.getDefault());
        return date.get(wf.weekOfYear());
    }

}

哪个输出

Week 1:
2015-04-01
2015-04-02
2015-04-03
2015-04-04
Week 2:
2015-04-05
2015-04-06
2015-04-07
2015-04-08
2015-04-09

在任何一种情况下,您都可以将每个日期映射到按周键入的地图,但这取决于您

原文链接:https://www.f2er.com/java/127159.html

猜你在找的Java相关文章