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

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

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

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

示例数据集

  1. Date date = new SimpleDateFormat.parse("04/01/2015")
  2. Date date = new SimpleDateFormat.parse("04/02/2015")
  3. Date date = new SimpleDateFormat.parse("04/03/2015")
  4. Date date = new SimpleDateFormat.parse("04/04/2015")
  5. Date date = new SimpleDateFormat.parse("04/05/2015")
  6. Date date = new SimpleDateFormat.parse("04/06/2015")
  7. Date date = new SimpleDateFormat.parse("04/07/2015")
  8. Date date = new SimpleDateFormat.parse("04/08/2015")
  9. Date date = new SimpleDateFormat.parse("04/09/2015")

期望的输出

  1. HashMap<Date,Date> hashMap = groupByWeek(ArrayList<Date> dates);
  2.  
  3. printWeeklyGroupedDates();
  4.  
  5. Week 1:
  6. 04/01/2015
  7. 04/02/2015
  8. 04/03/2015
  9. 04/04/2015
  10. 04/05/2015
  11. 04/06/2015
  12. 04/07/2015
  13. Week 2:
  14. 04/08/2015
  15. 04/09/2015

我试过的

  1. public Date getWhichYearlyWeek(Date date){
  2.  
  3. Calendar cal = Calendar.getInstance();
  4. cal.setTime(date);
  5. cal.get(Calendear.WEEK_OF_YEAR);
  6.  
  7. //Need to return this as 'Date' but indicate which week this is for the TreeMap functionality below
  8.  
  9. return cal.getTime();
  10. }
  11.  
  12.  
  13. public TreeMap<Date,ArrayList<Date>> getWeeklyMappedDates(ArrayList<Date> dateArray){
  14.  
  15. TreeMap<Date,ArrayList<Date>> treeMap = new TreeMap<Date,ArrayList<Date>>
  16.  
  17. for(Date i : dateArray){
  18.  
  19. Date date = getWhichYearlyWeek(date);
  20.  
  21. if(!treeMap.containsKey(date))
  22. treeMap.get(date).add(date);
  23. else
  24. treeMap.put(date,new ArrayList<Date>());
  25. }
  26.  
  27. return treeMap;
  28. }

解决方法

您只需使用自定义Comparator对日期列表进行排序即可.下面的例子按年份排序日期,然后按日期排序,不知道你是否想要第二部分,但这是一个很好的演示……
  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.ArrayList;
  4. import java.util.Calendar;
  5. import java.util.Collections;
  6. import java.util.Comparator;
  7. import java.util.Date;
  8. import java.util.List;
  9.  
  10. public class Test {
  11.  
  12. public static void main(String[] args) {
  13. try {
  14. SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
  15. List<Date> dates = new ArrayList<Date>(25);
  16. dates.add(sdf.parse("04/01/2015"));
  17. dates.add(sdf.parse("04/02/2015"));
  18. dates.add(sdf.parse("04/03/2015"));
  19. dates.add(sdf.parse("04/04/2015"));
  20. dates.add(sdf.parse("04/05/2015"));
  21. dates.add(sdf.parse("04/06/2015"));
  22. dates.add(sdf.parse("04/07/2015"));
  23. dates.add(sdf.parse("04/08/2015"));
  24. dates.add(sdf.parse("04/09/2015"));
  25.  
  26. int week = 0;
  27. int woy = -1;
  28. Collections.sort(dates,new WeekComparator());
  29. for (Date date : dates) {
  30. if (woy != getWeekOfYear(date)) {
  31. woy = getWeekOfYear(date);
  32. week++;
  33. System.out.println("Week " + week + ":");
  34. }
  35. System.out.println(date);
  36. }
  37.  
  38. } catch (ParseException exp) {
  39. exp.printStackTrace();
  40. }
  41. }
  42.  
  43. public static class WeekComparator implements Comparator<Date> {
  44.  
  45. @Override
  46. public int compare(Date o1,Date o2) {
  47. int result = getWeekOfYear(o1) - getWeekOfYear(o2);
  48. if (result == 0) {
  49. result = o1.compareTo(o2);
  50. }
  51. return result;
  52. }
  53.  
  54. }
  55.  
  56. protected static int getWeekOfYear(Date date) {
  57. Calendar cal = Calendar.getInstance();
  58. cal.setTime(date);
  59. return cal.get(Calendar.WEEK_OF_YEAR);
  60. }
  61.  
  62. }

哪个输出

  1. Week 1:
  2. Wed Apr 01 00:00:00 EST 2015
  3. Thu Apr 02 00:00:00 EST 2015
  4. Fri Apr 03 00:00:00 EST 2015
  5. Sat Apr 04 00:00:00 EST 2015
  6. Week 2:
  7. Sun Apr 05 00:00:00 EST 2015
  8. Mon Apr 06 00:00:00 EST 2015
  9. Tue Apr 07 00:00:00 EST 2015
  10. Wed Apr 08 00:00:00 EST 2015
  11. Thu Apr 09 00:00:00 EST 2015

Java 8 Time API

仅仅因为我需要练习……

  1. import java.text.SimpleDateFormat;
  2. import java.time.LocalDate;
  3. import java.time.format.DateTimeFormatter;
  4. import java.time.temporal.WeekFields;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.Comparator;
  8. import java.util.List;
  9. import java.util.Locale;
  10.  
  11. public class Test {
  12.  
  13. public static void main(String[] args) {
  14. SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
  15. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy");
  16.  
  17. List<LocalDate> dates = new ArrayList<LocalDate>(25);
  18. dates.add(LocalDate.parse("04/01/2015",dtf));
  19. dates.add(LocalDate.parse("04/02/2015",dtf));
  20. dates.add(LocalDate.parse("04/03/2015",dtf));
  21. dates.add(LocalDate.parse("04/04/2015",dtf));
  22. dates.add(LocalDate.parse("04/05/2015",dtf));
  23. dates.add(LocalDate.parse("04/06/2015",dtf));
  24. dates.add(LocalDate.parse("04/07/2015",dtf));
  25. dates.add(LocalDate.parse("04/08/2015",dtf));
  26. dates.add(LocalDate.parse("04/09/2015",dtf));
  27.  
  28. int week = 0;
  29. int woy = -1;
  30. Collections.sort(dates,new WeekComparator());
  31. for (LocalDate date : dates) {
  32. if (woy != getWeekOfYear(date)) {
  33. woy = getWeekOfYear(date);
  34. week++;
  35. System.out.println("Week " + week + ":");
  36. }
  37. System.out.println(date);
  38. }
  39. }
  40.  
  41. public static class WeekComparator implements Comparator<LocalDate> {
  42.  
  43. @Override
  44. public int compare(LocalDate o1,LocalDate o2) {
  45. int result = getWeekOfYear(o1) - getWeekOfYear(o2);
  46. if (result == 0) {
  47. result = o1.compareTo(o2);
  48. }
  49. return result;
  50. }
  51.  
  52. }
  53.  
  54. protected static int getWeekOfYear(LocalDate date) {
  55. WeekFields wf = WeekFields.of(Locale.getDefault());
  56. return date.get(wf.weekOfYear());
  57. }
  58.  
  59. }

哪个输出

  1. Week 1:
  2. 2015-04-01
  3. 2015-04-02
  4. 2015-04-03
  5. 2015-04-04
  6. Week 2:
  7. 2015-04-05
  8. 2015-04-06
  9. 2015-04-07
  10. 2015-04-08
  11. 2015-04-09

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

猜你在找的Java相关文章