TL;博士
给定年份和周数(例如,201927),我可以计算本周(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.
额外310
-”
YearWeek
使用标准周更容易
org.threeten.extra.YearWeek
中提供的类
ThreeTen-Extra
项目。该项目为现代
java.time公司
Java 8及更高版本中内置的类。
如果你有一个
ISO 8601
表示
standard week
,分析。
String input = "2019-W27";
YearWeek yw = YearWeek.parse( input);
yw.tostring():2019-w27年
标准的一周从星期一开始。这个
一周
对象可以生成
LocalDate
一周中的任何一天。使用指定日期
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 () );
日期.tostring():[2019-07-01、2019-07-02、2019-07-03、2019-07-04、2019-07-05、2019-07-06、2019-07-07]
顺便说一下,在标准周内表示一周中的一天的标准方法是附加一个连字符和数字1-7。
2019-W26-1=周一
2019-W26-2=星期二
豸
2019-W26-7=周日