在Joda-Time获得特定一周的第一天? java的

时间:2022-10-21 10:10:45

In Joda-Time, is there a way to get the date of the first day of the week(monday).

在Joda-Time,有没有办法获得一周的第一天(星期一)的日期。

for instance i want to find out what date was this weeks monday based on todays current date 21/01/11

例如,我想根据今天的当前日期21/01/11找出这个星期一星期一的日期

Cheers in advance.

提前干杯。

edit: i also wish to find the date for the end of the week i.e sunday's date. cheers

编辑:我也希望找到一周结束的日期,即星期日的日期。干杯

5 个解决方案

#1


69  

Try LocalDate.withDayOfWeek:

试试LocalDate.withDayOfWeek:

LocalDate now = new LocalDate();
System.out.println(now.withDayOfWeek(DateTimeConstants.MONDAY)); //prints 2011-01-17
System.out.println(now.withDayOfWeek(DateTimeConstants.SUNDAY)); //prints 2011-01-23

#2


25  

LocalDate today = new LocalDate();
LocalDate weekStart = today.dayOfWeek().withMinimumValue();
LocalDate weekEnd = today.dayOfWeek().withMaximumValue();

Will give you the first and last days i.e Monday and sunday

会给你第一天和最后一天,即周一和周日

#3


12  

Another option is to use roundFloorCopy. This looks like the following:

另一种选择是使用roundFloorCopy。这看起来如下:

LocalDate startOfWeek = new LocalDate().weekOfWeekyear().roundFloorCopy();

For the last day of the standard week (Sunday) use roundCeilingCopy and minusDays

对于标准周(星期日)的最后一天,使用roundCeilingCopy和minusDays ...

LocalDate lastDateOfWeek = new LocalDate().weekOfWeekyear().roundCeilingCopy().minusDays( 1 );

Also works for DateTime. And works for end of week (exclusive).

也适用于DateTime。并适用于周末(独家)。

DateTime dateTime = new DateTime();
DateTime startOfWeek = dateTime.weekOfWeekyear().roundFloorCopy();
DateTime endOfWeek = dateTime.weekOfWeekyear().roundCeilingCopy();

Dump to console…

转储到控制台......

System.out.println( "dateTime " + dateTime );
System.out.println( "startOfWeek " + startOfWeek );
System.out.println( "endOfWeek " + endOfWeek );

When run…

跑的时候......

dateTime 2014-01-24T00:00:34.955-08:00
startOfWeek 2014-01-20T00:00:00.000-08:00
endOfWeek 2014-01-27T00:00:00.000-08:00

#4


5  

You can use the getDayOfWeek() method that gives you back 1 for Monday, 2 for Tue, .., 7 for Sunday in order to go back that many days and reach Monday:

你可以使用getDayOfWeek()方法给你回星期一,2给Tue,..,7给星期日,以便返回那么多天并到达星期一:

import org.joda.time.DateTime;

    public class JodaTest {

        public static void main(String[] args) {
            DateTime date = new DateTime();
            System.out.println(date);
                    //2011-01-21T15:06:18.713Z
            System.out.println(date.minusDays(date.getDayOfWeek()-1));
                    //2011-01-17T15:06:18.713Z
        }
    }

#5


4  

See the section "Querying DateTimes" of the Joda-Time user guide.

请参阅Joda-Time用户指南的“查询日期时间”部分。

Here is the general algorithm I would follow:

这是我将遵循的一般算法:

  1. find the day-of-week of the target date (Jan 21 2011 as you mentioned)
  2. 找到目标日期的星期几(如您所述,2011年1月21日)
  3. determine how many days ahead of Monday this is
  4. 确定周一提前几天
  5. Subtract the value of #2 from the target date using dateTime.minusDays(n)
  6. 使用dateTime.minusDays(n)从目标日期减去#2的值

#1


69  

Try LocalDate.withDayOfWeek:

试试LocalDate.withDayOfWeek:

LocalDate now = new LocalDate();
System.out.println(now.withDayOfWeek(DateTimeConstants.MONDAY)); //prints 2011-01-17
System.out.println(now.withDayOfWeek(DateTimeConstants.SUNDAY)); //prints 2011-01-23

#2


25  

LocalDate today = new LocalDate();
LocalDate weekStart = today.dayOfWeek().withMinimumValue();
LocalDate weekEnd = today.dayOfWeek().withMaximumValue();

Will give you the first and last days i.e Monday and sunday

会给你第一天和最后一天,即周一和周日

#3


12  

Another option is to use roundFloorCopy. This looks like the following:

另一种选择是使用roundFloorCopy。这看起来如下:

LocalDate startOfWeek = new LocalDate().weekOfWeekyear().roundFloorCopy();

For the last day of the standard week (Sunday) use roundCeilingCopy and minusDays

对于标准周(星期日)的最后一天,使用roundCeilingCopy和minusDays ...

LocalDate lastDateOfWeek = new LocalDate().weekOfWeekyear().roundCeilingCopy().minusDays( 1 );

Also works for DateTime. And works for end of week (exclusive).

也适用于DateTime。并适用于周末(独家)。

DateTime dateTime = new DateTime();
DateTime startOfWeek = dateTime.weekOfWeekyear().roundFloorCopy();
DateTime endOfWeek = dateTime.weekOfWeekyear().roundCeilingCopy();

Dump to console…

转储到控制台......

System.out.println( "dateTime " + dateTime );
System.out.println( "startOfWeek " + startOfWeek );
System.out.println( "endOfWeek " + endOfWeek );

When run…

跑的时候......

dateTime 2014-01-24T00:00:34.955-08:00
startOfWeek 2014-01-20T00:00:00.000-08:00
endOfWeek 2014-01-27T00:00:00.000-08:00

#4


5  

You can use the getDayOfWeek() method that gives you back 1 for Monday, 2 for Tue, .., 7 for Sunday in order to go back that many days and reach Monday:

你可以使用getDayOfWeek()方法给你回星期一,2给Tue,..,7给星期日,以便返回那么多天并到达星期一:

import org.joda.time.DateTime;

    public class JodaTest {

        public static void main(String[] args) {
            DateTime date = new DateTime();
            System.out.println(date);
                    //2011-01-21T15:06:18.713Z
            System.out.println(date.minusDays(date.getDayOfWeek()-1));
                    //2011-01-17T15:06:18.713Z
        }
    }

#5


4  

See the section "Querying DateTimes" of the Joda-Time user guide.

请参阅Joda-Time用户指南的“查询日期时间”部分。

Here is the general algorithm I would follow:

这是我将遵循的一般算法:

  1. find the day-of-week of the target date (Jan 21 2011 as you mentioned)
  2. 找到目标日期的星期几(如您所述,2011年1月21日)
  3. determine how many days ahead of Monday this is
  4. 确定周一提前几天
  5. Subtract the value of #2 from the target date using dateTime.minusDays(n)
  6. 使用dateTime.minusDays(n)从目标日期减去#2的值