Joda时间周结束日期

时间:2015-08-06 17:46:53

标签: java jodatime

我正在尝试使用Joda时间来获得本周的最后一天。

这是正确的方式(生产准备好)在星期日 - 星期六这样做:

        DateTimeZone timeZone = DateTimeZone.forID( "America/New_York" );
        DateTime now = new DateTime( timeZone );

        DateTime weekStart = now.withDayOfWeek( DateTimeConstants.SATURDAY ).minusDays(6);
        DateTime weekEnd = now.withDayOfWeek(DateTimeConstants.SATURDAY).plusDays( 0 );
        Interval week = new Interval( weekStart, weekEnd );
        //Dump to console…
        String[] week_start = weekStart.toString().split("T"); 
        String[] week_End = weekEnd.toString().split("T"); 
        String[] now_date = now.toString().split("T"); 
        System.out.println( "now: " + now_date[0] );
        System.out.println( "weekStart: " + week_start[0]);
        System.out.println( "weekEnd: " + week_End[0] );
        System.out.println( "week: " + week );

输出:

now: 2015-08-06
weekStart: 2015-08-02
weekEnd: 2015-08-08
week: 2015-08-02T14:15:46.580-04:00/2015-08-08T14:15:46.580-04:00

1 个答案:

答案 0 :(得分:0)

感谢gaganbm的评论,我这样做了:

DateTime weekStart = getUSFirstDayOfWeek(now);
DateTime weekEnd = now.withDayOfWeek(DateTimeConstants.SATURDAY).plusDays( 0 );

public static DateTime getUSFirstDayOfWeek(DateTime dateTime) {
    return dateTime.withDayOfWeek(DateTimeConstants.MONDAY).minusDays(1);
}

似乎能产生正确的结果!