在Joda-Time中,有没有办法获取一周第一天(星期一)的日期。
例如,我想根据今天的当前日期 21/01/11 找出本周星期一是什么日期
提前干杯。
编辑:我还希望找到本周末的日期,即周日的日期。干杯
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
LocalDate today = new LocalDate();
LocalDate weekStart = today.dayOfWeek().withMinimumValue();
LocalDate weekEnd = today.dayOfWeek().withMaximumValue();
将为您提供第一天和最后一天,即星期一和星期日
另一种选择是使用roundFloorCopy。这看起来像下面这样:
LocalDate startOfWeek = new LocalDate().weekOfWeekyear().roundFloorCopy();
对于标准周的最后一天(星期日),使用roundCeilingCopy和minusDays…
LocalDate lastDateOfWeek = new LocalDate().weekOfWeekyear().roundCeilingCopy().minusDays( 1 );
也适用于日期时间。并在周末工作(独家)。
DateTime dateTime = new DateTime();
DateTime startOfWeek = dateTime.weekOfWeekyear().roundFloorCopy();
DateTime endOfWeek = dateTime.weekOfWeekyear().roundCeilingCopy();
转储到控制台...
System.out.println( "dateTime " + dateTime );
System.out.println( "startOfWeek " + startOfWeek );
System.out.println( "endOfWeek " + endOfWeek );
运行时...
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
您可以使用 getDayOfWeek() 方法返回 1 表示星期一,2 表示星期二,..,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
}
}
请参阅 Joda-Time 用户指南的“查询日期时间”部分。
这是我遵循的一般算法:
dateTime.minusDays(n)
给定日期的周一和下周日的日期与给定日期的一周第一天和最后一天的日期不同,因为一周的第一天是
Locale
敏感的,例如美国是星期日,英国是星期一。
java.time
以下为Joda-Time主页上的通知:
请注意,从 Java SE 8 开始,用户被要求迁移到
(JSR-310) - JDK 的核心部分,它取代了这个 项目。java.time
java.time
API 的解决方案TemporalAdjusters#previousOrSame
和 TemporalAdjusters#nextOrSame
以及给定的 LocalDate
查找一周中所需的日子。
但是,正如本答案顶部提到的,一周的第一天是
Locale
敏感;因此,如果您需要找到一周第一天的LocalDate
,首先您需要找到一周的第一天。然后,加上 6 天即可得到本周的最后一个日期。
演示:
public class Main {
public static void main(String[] args) {
// A sample LocalDate
LocalDate date = LocalDate.of(2011, Month.JANUARY, 21);
LocalDate dateOnSameOrPreviousMon = date.with(
TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
LocalDate dateOnSameOrNextSun = date.with(
TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
System.out.println(dateOnSameOrPreviousMon);
System.out.println(dateOnSameOrNextSun);
// ###### The first day of the week is Locale-sensitive ######
LocalDate now = LocalDate.now();
// First day of a week in the US is Sunday
DayOfWeek firstDayOfWeekUs = now
.with(WeekFields.of(Locale.US).dayOfWeek(), 1)
.getDayOfWeek();
System.out.println(firstDayOfWeekUs);
// First day of a week in the UK is Monday
DayOfWeek firstDayOfWeekUk = now
.with(WeekFields.of(Locale.UK).dayOfWeek(), 1)
.getDayOfWeek();
System.out.println(firstDayOfWeekUk);
// ###### Thus, the first and last days of the week of the given date:
// In the US
LocalDate dateOnFirstDayOfWeekUs = date.with(
TemporalAdjusters.previousOrSame(firstDayOfWeekUs));
LocalDate dateOnLaststDayOfWeekUs = dateOnFirstDayOfWeekUs.plusDays(6);
System.out.println(dateOnFirstDayOfWeekUs);
System.out.println(dateOnLaststDayOfWeekUs);
// In the UK
LocalDate dateOnFirstDayOfWeekUk = date.with(
TemporalAdjusters.previousOrSame(firstDayOfWeekUk));
LocalDate dateOnLaststDayOfWeekUk = dateOnFirstDayOfWeekUk.plusDays(6);
System.out.println(dateOnFirstDayOfWeekUk);
System.out.println(dateOnLaststDayOfWeekUk);
}
}
示例运行的输出:
2011-01-17
2011-01-23
SUNDAY
MONDAY
2011-01-16
2011-01-22
2011-01-17
2011-01-23
从 Trail:日期时间了解有关现代日期时间 API 的更多信息。