Java 获取当前天是一年中的第几天
@Test
void dayofweed() throws Exception {
System.out.println("2023-01-01 第 " + getWeekYearISO("2023-01-01"));
System.out.println("2023-08-01 第 " + getWeekYearISO("2023-08-01"));
}
public static int getWeekYearISO(String today) throws ParseException {
SimpleDateFormat df_yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
Date date = df_yyyyMMdd.parse(today);
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY); // 每周一算开始
calendar.setMinimalDaysInFirstWeek(4); // 一周最少要有 4 天,默认:1
calendar.setTime(date);
return calendar.get(Calendar.WEEK_OF_YEAR);
}
2023-01-01 第 52
2023-08-01 第 31