简体   繁体   English

在Joda Time获取一年中第一周的第一天的日期

[英]Get Date of the first day in the first week of a year in Joda Time

I have a Java date: tempDate = Sun Jan 01 00:00:00 GMT+01:00 2012. 我有一个Java日期:tempDate = Sun Jan 01 00:00:00 GMT + 01:00 2012。

I would like to create a new Date, that is the first day of the first week in the year of tempDate. 我想创建一个新的日期,即tempDate年份第一周的第一天。

That is: 那是:

  Mon Jan 02 00:00:00 GMT+01:00 2012

When I try to use Joda Time: 当我尝试使用Joda时间时:

DateTime cal = new DateTime(tempDate).withWeekOfWeekyear(1).
                     withDayOfWeek(DateTimeConstants.MONDAY);

I get what I want, but in the previous year: 我得到了我想要的,但在前一年:

Mon Jan 03 00:00:00 GMT+01:00 2011

How can I make this work in Joda Time? 我如何在Joda Time中完成这项工作?

You must understand the difference between a DateTime's year and its week year. 您必须了解DateTime的年份与其周年之间的差异。

In Joda-Time , in accordance with the ISO 8601 standard , Monday is the first day of the week. Joda-Time ,根据ISO 8601标准 ,星期一是一周的第一天。 Yet years do not always start on a Monday. 然而,岁月并不总是在星期一开始。 The first week of a year, still according to the ISO standard, is the week that contains the first Thursday of that year. 按照ISO标准,一年的第一周是包含该年第一个星期四的一周。 As a consequence certain days of a year may fall into a week of the previous or the next year. 因此,一年中的某些日子可能会落入上一年或下一年的一周。

In your example 01/01/2012 is in fact such a day. 在你的例子01/01/2012实际上是这样的一天。 It belongs to week 52 of 2011. So its year (2012) is different from its week year (2011). 它属于2011年第52周。所以它的一年(2012年)与其一周(2011年)不同。 Consequently, if you change the date to the first week of its week year, you'll get a DateTime in the first week of 2011. 因此,如果您将日期更改为其周年的第一周,您将在2011年的第一周获得日期时间。

To make this work consistently you simply have to make sure your week year is the same as your year : 为了使这项工作始终如一,您只需确保您的周年与您的年份相同:

DateTime onTheFirstDayOfTheFirstWeek = dateTime.withWeekyear(dateTime.getYear()).withWeekOfWeekyear(1).withDayOfWeek(1);

BTW the workaround in your comment will not work consistently, specifically it will fail when the system time falls in a day that has a year different from it's week year. 顺便说一下,你的评论中的解决方法将不能始终如一地工作,特别是当系统时间与一年中的年份不同时,它将失败。

The answer by bowmore is correct, answering the question. 鲍莫尔回答是正确的,回答了这个问题。

If you did indeed want to follow the ISO 8601 standard definition of a week-of-year and first week , here is some example code using Joda-Time 2.3. 如果你确实想要遵循一年中第一周ISO 8601标准定义,这里有一些使用Joda-Time 2.3的示例代码。

Note the call to withTimeAtStartOfDay to get the first moment of the day. 请注意对withTimeAtStartOfDay的调用以获取当天的第一个时刻。 Days do not always begin at 00:00:00 time. 天不总是在00:00:00开始。

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );

DateTime now = new DateTime( timeZone );
DateTime firstWeekStart = now.withWeekOfWeekyear(1).withDayOfWeek( DateTimeConstants.MONDAY ).withTimeAtStartOfDay();
DateTime firstWeekStop = firstWeekStart.plusWeeks( 1 );
Interval firstWeek = new Interval( firstWeekStart, firstWeekStop );

Dump to console… 转储到控制台......

System.out.println( "now: " + now );
System.out.println( "firstWeekStart: " + firstWeekStart );
System.out.println( "firstWeekStop: " + firstWeekStop );
System.out.println( "firstWeek: " + firstWeek );
firstWeek: 2013-12-30T00:00:00.000+01:00/2014-01-06T00:00:00.000+01:00

When run… 跑的时候......

now: 2014-02-07T12:49:33.623+01:00
firstWeekStart: 2013-12-30T00:00:00.000+01:00
firstWeekStop: 2014-01-06T00:00:00.000+01:00

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

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