如何计算属于基于 ISO 的周数的天数
How to calculate the days belonging to an ISO based week number
我有一个基于 ISO 的周数,它是使用以下公式计算的 Java 8 LocalDate API
int weekNumOfYear = LocalDate#get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)
给定年份和周数(例如,201927),我如何计算本周(201917)的开始日和结束日?
我正在使用Calendar
class来解决这个问题,但不确定它是否正确(尤其是是否遵循了ISO格式)
Update: The following code doesn't work correctly for 201953, there is
no 53th week for 2019
@Test
public void testGetWeekDays() {
Integer year = 2019;
Integer week = 27;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
//Use ISO format
cal.setMinimalDaysInFirstWeek(4);
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.WEEK_OF_YEAR, week);
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
String beginDate = sdf.format(cal.getTime());
cal.add(Calendar.DAY_OF_WEEK, 6);
String endDate = sdf.format(cal.getTime());
System.out.println(beginDate);
System.out.println(endDate);
}
您还可以使用 with
获取一周的开始日期和结束日期
System.out.println(localDate.with(DayOfWeek.MONDAY));
System.out.println(localDate.with(DayOfWeek.SUNDAY));
如果您想从周数中获取开始日期和结束日期,请使用 ISO_WEEK_DATE
LocalDate startDay = LocalDate.parse("2019-W26-1", DateTimeFormatter.ISO_WEEK_DATE);
LocalDate endDay = LocalDate.parse("2019-W26-7", DateTimeFormatter.ISO_WEEK_DATE);
One digit for the day-of-week. The value run from Monday (1) to Sunday (7).
tl;博士
Given the year, and the week number (eg, 201927),I could I calculate the start day and end day for this week(201917)?
YearWeek // Represent a particular week of a week-based year.
.of( // Factory method, rather than calling `new`.
2019 , // The number of the desired week-based year ( *not* calendar year! ).
26 // Week of the year, 1 through 52 or 53.
)
.atDay( // Generate a `LocalDate` object for a day within this week.
DayOfWeek.MONDAY // Specify the day-of-week.
) // Generate a `LocalDate` for the first day of this week.
.plusDays( 6 ) // Generate a `LocalDate` representing the last day of this week.
ThreeTen-Extra — YearWeek
在 org.threeten.extra.YearWeek
class provided in the ThreeTen-Extra 项目中使用标准周要容易得多。该项目为 java.time 类 内置的现代 java.time类 8 及更高版本添加了功能。
如果你有 ISO 8601 compliant string representing a standard week,解析。
String input = "2019-W27";
YearWeek yw = YearWeek.parse( input);
yw.toString(): 2019-W27
标准周从星期一开始。 YearWeek
对象可以生成一个 LocalDate
for any day in the week. Specify a day using the DayOfWeek
枚举。
LocalDate ld = yw.atDay( DayOfWeek.MONDAY);
ld.toString(): 2019-07-01
要获得一周的剩余时间,请一遍又一遍地添加一天。
LocalDate nextDay = ld.plusDays( 1 ) ;
ld.toString(): 2019-07-02
要得到周末,加六。
LocalDate nextDay = ld.plusDays( 6 ) ;
ld.toString(): 2019-07-07
或者使用 Java 流。通过调用 LocalDate::datesUntil
.
生成流
Stream < LocalDate > stream = ld.datesUntil ( ld.plusWeeks ( 1 ) );
List < LocalDate > dates = stream.collect ( Collectors.toList () );
dates.toString(): [2019-07-01, 2019-07-02, 2019-07-03, 2019-07-04, 2019-07-05, 2019-07-06, 2019-07-07]
顺便说一句,在一个标准周内表示 day-of-week 的标准方法是附加一个连字符和数字 1-7。
2019-W26-1 = Monday
2019-W26-2 = Tuesday
…
2019-W26-7 = Sunday
我有一个基于 ISO 的周数,它是使用以下公式计算的 Java 8 LocalDate API
int weekNumOfYear = LocalDate#get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)
给定年份和周数(例如,201927),我如何计算本周(201917)的开始日和结束日?
我正在使用Calendar
class来解决这个问题,但不确定它是否正确(尤其是是否遵循了ISO格式)
Update: The following code doesn't work correctly for 201953, there is no 53th week for 2019
@Test
public void testGetWeekDays() {
Integer year = 2019;
Integer week = 27;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
//Use ISO format
cal.setMinimalDaysInFirstWeek(4);
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.WEEK_OF_YEAR, week);
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
String beginDate = sdf.format(cal.getTime());
cal.add(Calendar.DAY_OF_WEEK, 6);
String endDate = sdf.format(cal.getTime());
System.out.println(beginDate);
System.out.println(endDate);
}
您还可以使用 with
获取一周的开始日期和结束日期
System.out.println(localDate.with(DayOfWeek.MONDAY));
System.out.println(localDate.with(DayOfWeek.SUNDAY));
如果您想从周数中获取开始日期和结束日期,请使用 ISO_WEEK_DATE
LocalDate startDay = LocalDate.parse("2019-W26-1", DateTimeFormatter.ISO_WEEK_DATE);
LocalDate endDay = LocalDate.parse("2019-W26-7", DateTimeFormatter.ISO_WEEK_DATE);
One digit for the day-of-week. The value run from Monday (1) to Sunday (7).
tl;博士
Given the year, and the week number (eg, 201927),I could I calculate the start day and end day for this week(201917)?
YearWeek // Represent a particular week of a week-based year.
.of( // Factory method, rather than calling `new`.
2019 , // The number of the desired week-based year ( *not* calendar year! ).
26 // Week of the year, 1 through 52 or 53.
)
.atDay( // Generate a `LocalDate` object for a day within this week.
DayOfWeek.MONDAY // Specify the day-of-week.
) // Generate a `LocalDate` for the first day of this week.
.plusDays( 6 ) // Generate a `LocalDate` representing the last day of this week.
ThreeTen-Extra — YearWeek
在 org.threeten.extra.YearWeek
class provided in the ThreeTen-Extra 项目中使用标准周要容易得多。该项目为 java.time 类 内置的现代 java.time类 8 及更高版本添加了功能。
如果你有 ISO 8601 compliant string representing a standard week,解析。
String input = "2019-W27";
YearWeek yw = YearWeek.parse( input);
yw.toString(): 2019-W27
标准周从星期一开始。 YearWeek
对象可以生成一个 LocalDate
for any day in the week. Specify a day using the DayOfWeek
枚举。
LocalDate ld = yw.atDay( DayOfWeek.MONDAY);
ld.toString(): 2019-07-01
要获得一周的剩余时间,请一遍又一遍地添加一天。
LocalDate nextDay = ld.plusDays( 1 ) ;
ld.toString(): 2019-07-02
要得到周末,加六。
LocalDate nextDay = ld.plusDays( 6 ) ;
ld.toString(): 2019-07-07
或者使用 Java 流。通过调用 LocalDate::datesUntil
.
Stream < LocalDate > stream = ld.datesUntil ( ld.plusWeeks ( 1 ) );
List < LocalDate > dates = stream.collect ( Collectors.toList () );
dates.toString(): [2019-07-01, 2019-07-02, 2019-07-03, 2019-07-04, 2019-07-05, 2019-07-06, 2019-07-07]
顺便说一句,在一个标准周内表示 day-of-week 的标准方法是附加一个连字符和数字 1-7。
2019-W26-1 = Monday
2019-W26-2 = Tuesday
…
2019-W26-7 = Sunday