我想获取本周和上周的所有日期..现在,我可以获取本周和上周的所有日期
ArrayList<String> week1 = new ArrayList<>(); // this week
ArrayList<String> week2 = new ArrayList<>(); // last week
ArrayList<String> week3 = new ArrayList<>(); // weeks ago
ArrayList<String> week4 = new ArrayList<>(); // weeks ago
Calendar cal = Calendar.getInstance();
for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
cal.set(Calendar.DAY_OF_WEEK, i);
week1.add(sdf.format(cal.getTime()));
}
如何获取week2,week3,week4?
答案 0 :(得分:1)
您可以在Calendar.WEEK_OF_YEAR
上加上-1,以获取上一个WEEK。
calendar.add(Calendar.WEEK_OF_YEAR,-1);
所以最终代码可能是。
ArrayList<String> week1 = new ArrayList<>(); // this week
ArrayList<String> week2 = new ArrayList<>(); // last week
ArrayList<String> week3 = new ArrayList<>(); // weeks ago
ArrayList<String> week4 = new ArrayList<>(); // weeks ago
Calendar calendar=Calendar.getInstance();
for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
calendar.set(Calendar.DAY_OF_WEEK, i);
week1.add(sdf.format(calendar.getTime()));
}
calendar.add(Calendar.WEEK_OF_YEAR,-1);
for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
calendar.set(Calendar.DAY_OF_WEEK, i);
week2.add(sdf.format(calendar.getTime()));
}
calendar.add(Calendar.WEEK_OF_YEAR,-1);
for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
calendar.set(Calendar.DAY_OF_WEEK, i);
week3.add(sdf.format(calendar.getTime()));
}
calendar.add(Calendar.WEEK_OF_YEAR,-1);
for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
calendar.set(Calendar.DAY_OF_WEEK, i);
week4.add(sdf.format(calendar.getTime()));
}
尽管这样做似乎太多了代码。为此可以有一个更短的解决方案。您可以看看Calendar。它有很多简单的方法来操作Dates。
答案 1 :(得分:1)
LocalDate today = LocalDate.now(ZoneId.of("Asia/Harbin"));
LocalDate weekStart = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
List<LocalDate> thisWeek = weekStarting(weekStart);
List<LocalDate> lastWeek = weekStarting(weekStart.minusWeeks(1));
List<LocalDate> twoWeeksAgo = weekStarting(weekStart.minusWeeks(2));
List<LocalDate> threeWeeksAgo = weekStarting(weekStart.minusWeeks(3));
System.out.println("3 weeks ago: " + threeWeeksAgo);
System.out.println("2 weeks ago: " + twoWeeksAgo);
System.out.println("Last week: " + lastWeek);
System.out.println("This week: " + thisWeek);
大多数“魔术”都发生在我的辅助方法中:
private static List<LocalDate> weekStarting(LocalDate date) {
// Might want to validate that date.getDayOfWeek() == DayOfWeek.SUNDAY
List<LocalDate> week = new ArrayList<>(7);
for (int day = 0; day < 7; day++) {
week.add(date);
date = date.plusDays(1);
}
return week;
}
今天运行代码时,它打印出来:
3 weeks ago: [2018-06-10, 2018-06-11, 2018-06-12, 2018-06-13, 2018-06-14, 2018-06-15, 2018-06-16]
2 weeks ago: [2018-06-17, 2018-06-18, 2018-06-19, 2018-06-20, 2018-06-21, 2018-06-22, 2018-06-23]
Last week: [2018-06-24, 2018-06-25, 2018-06-26, 2018-06-27, 2018-06-28, 2018-06-29, 2018-06-30]
This week: [2018-07-01, 2018-07-02, 2018-07-03, 2018-07-04, 2018-07-05, 2018-07-06, 2018-07-07]
我从您的代码中得出,一周的第一天是星期日。在世界上许多地方,一周从另一天开始。如果您需要考虑到这一点,则必须修改代码。
我将LocalDate
对象存储在列表中,而不是字符串中。对于大多数目的,这是一种更好的方法。仅在需要将日期显示给用户时才格式化日期。
即使是Android版,我也正在使用并建议使用java.time
(现代Java日期和时间API)。这样行吗?是的,java.time
在旧的和更新的Android设备上都能很好地工作。它只需要至少Java 6 。
org.threeten.bp
导入日期和时间类。您使用的Calendar
和SimpleDateFormat
类(也是内置在较早的Android版本中)早已过时且设计不良。至少对于像您这样的重要日期操作,我不建议这样做。
java.time
。java.time
。java.time
向Java 6和7(JSR-310的ThreeTen)的反向端口。