1、Calendar根据日期获取年份和周
直接上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; public class CalendarTest { public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setFirstDayOfWeek(Calendar.MONDAY); getYearAndWeek(cal.getTime()); }
public static WeekYear getYearAndWeek(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setFirstDayOfWeek(Calendar.MONDAY); calendar.setTime(date); int week = calendar.get(Calendar.WEEK_OF_YEAR); int year = calendar.get(Calendar.YEAR); calendar.add(Calendar.DAY_OF_MONTH, -7); int yearBefore = calendar.get(Calendar.YEAR); int weekBefore = calendar.get(Calendar.WEEK_OF_YEAR); if (week < weekBefore) { yearBefore += 1; year = yearBefore; } System.out.println(year + "年第" + week + "周"); return new WeekYear(year, week); } } class WeekYear { private Integer year; private Integer week; public WeekYear(Integer year, Integer week) { this.year = year; this.week = week; } public Integer getYear() { return year; } public void setYear(Integer year) { this.year = year; } public Integer getWeek() { return week; } public void setWeek(Integer week) { this.week = week; } @Override public String toString() { return "WeekYear{" + "year=" + year + ", week=" + week + '}'; } }
|
运行结果:
2020年第50周
如果想获取过去2周和未来四周的年份和日期,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setFirstDayOfWeek(Calendar.MONDAY);
List<WeekYear> wys = new ArrayList<>(8); cal.add(Calendar.DATE, -14); wys.add(getYearAndWeek(cal.getTime())); for (int i = 0; i < 6; i++) { cal.add(Calendar.DATE, +7); wys.add(getYearAndWeek(cal.getTime())); } }
|
运行结果如下:
2020年第48周
2020年第49周
2020年第50周
2020年第51周
2020年第52周
2021年第1周
2021年第2周
这里我们设置一周的第一天为周一,上面的结果可以看出第三周时跨年了,测试时会发现2020-12-28 Calendar返回的是周是1,但是2020年,所以这里需要特殊处理下,应该是2021年第一周。
2、当前周的所有日期
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; public class CalendarTest {
public static List<String> getWeekDays(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); List<String> list = new ArrayList<>(); Calendar cal = Calendar.getInstance(); cal.setTime(date); int dayWeek = cal.get(Calendar.DAY_OF_WEEK); if (1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } cal.setFirstDayOfWeek(Calendar.MONDAY); int day = cal.get(Calendar.DAY_OF_WEEK); cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day); list.add(sdf.format(cal.getTime())); for (int i = 1; i < 7; i++) { cal.add(Calendar.DAY_OF_MONTH, 1); list.add(sdf.format(cal.getTime())); } return list; } public static void main(String[] args) throws ParseException { System.out.println(getWeekDays(new Date())); } }
|
输出结果:
[2020-12-07, 2020-12-08, 2020-12-09, 2020-12-10, 2020-12-11, 2020-12-12, 2020-12-13]
再验证获取2020-12-28所在周的所有日期
1 2 3 4 5 6
| public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse("2020-12-28")); System.out.println(getWeekDays(cal.getTime())); }
|
输出结果:
[2020-12-28, 2020-12-29, 2020-12-30, 2020-12-31, 2021-01-01, 2021-01-02, 2021-01-03]
以上在做日历和排班时可以用得到,本实例用的是Calendar类实现的功能,JDK8中LocalDate也可以实现,大家有兴趣可以去尝试!