java.time 中的年-周-日?
Year-Week-Day in java.time?
java.time classes, or its extension ThreeTen-Extra, for a week dates 是否有支持,特别是年-周-日,例如 2009-W53-7
即 2010 年 1 月 3 日星期日。
至于年周没有星期:
- My Answer to the Question, java get week of year for given a date, explains using
IsoFields
处理年-周。
- ThreeTen-Extra project that extends the java.time classes offers the
YearWeek
class。
但是如何表示星期几呢?
参见IsoFields class, which allows the week-based year and week of week-based year to be queried. There is also a dedicated formatter ISO_WEEK_DATE。
DayOfWeek
enum tells you the number of the day-of-week, 1-7 for Monday to Sunday. Call LocalDate::getDayOfWeek
and then DayOfWeek::getValue
.
LocalDate ld = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;
2016-12-07
int weekOfWeekBasedYear = ld.get( IsoFields.WEEK_OF_WEEK_BASED_YEAR ) ;
int yearOfWeekBasedYear = ld.get( IsoFields.WEEK_BASED_YEAR ) ;
int dayOfWeek = ld.getDayOfWeek().getValue();
使用这些部分构建标准字符串 ISO 8601 week date formats。
String yearWeek = yearOfWeekBasedYear + "-W" + String.format( "%02d", weekOfWeekBasedYear ) ;
2016-W49
String yearWeekDay = yearWeek + "-" + dayOfWeek ;
2016-W49-3
或者,让预定义的 DateTimeFormatter.ISO_WEEK_DATE
完成工作。
String ywd = ld.format( DateTimeFormatter.ISO_WEEK_DATE );
2016-W49-3
同一个格式化程序可以解析此类标准字符串。
String input = "2016-W49-3" ;
LocalDate ldParsed = LocalDate.parse( input , DateTimeFormatter.ISO_WEEK_DATE ) ;
2016-12-07
java.time classes, or its extension ThreeTen-Extra, for a week dates 是否有支持,特别是年-周-日,例如 2009-W53-7
即 2010 年 1 月 3 日星期日。
至于年周没有星期:
- My Answer to the Question, java get week of year for given a date, explains using
IsoFields
处理年-周。 - ThreeTen-Extra project that extends the java.time classes offers the
YearWeek
class。
但是如何表示星期几呢?
参见IsoFields class, which allows the week-based year and week of week-based year to be queried. There is also a dedicated formatter ISO_WEEK_DATE。
DayOfWeek
enum tells you the number of the day-of-week, 1-7 for Monday to Sunday. Call LocalDate::getDayOfWeek
and then DayOfWeek::getValue
.
LocalDate ld = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;
2016-12-07
int weekOfWeekBasedYear = ld.get( IsoFields.WEEK_OF_WEEK_BASED_YEAR ) ;
int yearOfWeekBasedYear = ld.get( IsoFields.WEEK_BASED_YEAR ) ;
int dayOfWeek = ld.getDayOfWeek().getValue();
使用这些部分构建标准字符串 ISO 8601 week date formats。
String yearWeek = yearOfWeekBasedYear + "-W" + String.format( "%02d", weekOfWeekBasedYear ) ;
2016-W49
String yearWeekDay = yearWeek + "-" + dayOfWeek ;
2016-W49-3
或者,让预定义的 DateTimeFormatter.ISO_WEEK_DATE
完成工作。
String ywd = ld.format( DateTimeFormatter.ISO_WEEK_DATE );
2016-W49-3
同一个格式化程序可以解析此类标准字符串。
String input = "2016-W49-3" ;
LocalDate ldParsed = LocalDate.parse( input , DateTimeFormatter.ISO_WEEK_DATE ) ;
2016-12-07