文章目录
- 前言
- 一、java根据某个日期查询出是某年的第几周
前言
做记录用:
一、java根据某个日期查询出是某年的第几周
public class WeekByDate {public static void main(String[] args) throws ParseException {SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
// String time = "2023-12-25";String time = "2022-01-06";Date parse = simpleDateFormat.parse(time);Calendar calendar = Calendar.getInstance();//设置星期一为一周开始的第一天calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置在一年中第一个星期所需最少天数calendar.setMinimalDaysInFirstWeek(0);calendar.setTime(parse);int week = calendar.get(Calendar.WEEK_OF_YEAR);System.out.println(week);if (week == 1 && parse.getMonth() == 11) {calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 7);Date pastDate = calendar.getTime();String result = simpleDateFormat.format(pastDate);System.out.println(result);week = calendar.get(Calendar.WEEK_OF_YEAR) + 1;System.out.println(week);}}}