简体   繁体   English

如何获得一年中某一周的年份?

[英]How to get the YEAR for week of year for a date?

I know that Calendar.WEEK_OF_YEAR could been used to obtain the "week of year" for a given date, but how do I get the corresponding YEAR?.我知道Calendar.WEEK_OF_YEAR可用于获取给定日期的“一年中的第几周”,但如何获取相应的 YEAR?。

I know that the definition of Week of Year is dependent on Locale .我知道Week of Year 的定义取决于Locale And I am mostly interested in a DIN 1355-1 / ISO 8601 / German solution (but I do not want to make it a fix implementation) .我最感兴趣的是 DIN 1355-1 / ISO 8601 / 德国解决方案(但我不想将其作为修复实施)

So it could happen that the 1 January belongs to:所以 1 月 1 日可能属于:

  • the first week of year (1) of the new year: for example 2015-01-01 is in week of year 1 of 2015新年 (1) 的第一周:例如 2015-01-01 是 2015 年第一周
  • the last week of year (53) of the year before: for example 2016-01-01 is in week of year 53 of year 2015前一年 (53) 年的最后一周:例如 2016-01-01 在2015年第 53 年的那一周

My question is how to get this year?*我的问题是今年如何获得?*

Date date =
Calender calendar = new GregorianCalendar(Locale.GERMAN)
calendar.setTime(date);

int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
int year       = ????                                             <------------

System.out.println(date + " is in " + weekOfYear + " of " + year);

* the solution is not: date.getYear , date.getFirstDayOfWeek.getYear or date.getLastDayOfWeek.getYear * 解决方案不是: date.getYeardate.getFirstDayOfWeek.getYeardate.getLastDayOfWeek.getYear

java.time java.time

You should be using the java.time framework built into Java 8 and later.您应该使用 Java 8 及更高版本中内置的java.time框架。 Avoid the old and notoriously troublesome java.util.Date/.Calendar classes.避免旧的和臭名昭著的麻烦 java.util.Date/.Calendar 类。

These new java.time classes include LocalDate for date-only value without time-of-day or time zone.这些新的 java.time 类包括LocalDate ,用于没有日期时间或时区的仅日期值。 Note that you must specify a time zone to determine 'today' as the date is not simultaneously the same around the world.请注意,您必须指定一个时区来确定“今天”,因为世界各地的日期不同时。

ZoneId zoneId = ZoneId.of ( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now ( zoneId );

The IsoFields class provides info according to the ISO 8601 standard including the week-of-year for a week-based year. IsoFields类根据 ISO 8601 标准提供信息,包括基于周的一年中的一周。

int calendarYear = now.getYear();
int weekNumber = now.get ( IsoFields.WEEK_OF_WEEK_BASED_YEAR );
int weekYear = now.get ( IsoFields.WEEK_BASED_YEAR );  // Answer to Question

Near the beginning/ending of a year, the week-based-year may be ±1 different than the calendar-year.在一年的开始/结束时,基于周的年份可能与日历年相差 ±1。 For example, notice the difference between the Gregorian and ISO 8601 calendars for the end of 2015: Weeks 52 & 1 become 52 & 53.例如,请注意 2015 年底公历和 ISO 8601 日历之间的差异:第 52 周和第 1 周变为第 52 周和第 53 周。

在此处输入图像描述

在此处输入图像描述

Java 8 or later? Java 8 或更高版本?

Refer to Basil Bourque's answer to use Java's java.time API.请参阅Basil Bourque 的回答使用 Java 的java.time API。

Otherwise (think twice.) see my original answer to use the obsolete Java API as asked in this question.否则(三思而后行)请参阅我在这个问题中提出的使用过时的 Java API 的原始答案。


Pre Java 8? Java 8 之前?

calendar.getWeekYear(); returns 2015 for your given example.为您给出的示例返回2015

See API documentation for GregorianCalendar#getWeekYear() as well as week year .请参阅GregorianCalendar#getWeekYear()week year的 API 文档。

For reference以供参考

Calendar calendar = new GregorianCalendar(Locale.GERMAN);
calendar.set(2016, Calendar.JANUARY, 1, 0, 0, 0);
Date date = calendar.getTime();

int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
int year       = calendar.getWeekYear();

System.out.println(date + " is in " + weekOfYear + " of " + year);

returns回报

Fri Jan 01 00:00:00 CET 2016 is in 53 of 2015

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM