4

我已经实现了一种方法来获取本周星期一的日期,并且我已将星期一指定为一周的第一天。

但是,无论我做什么,它都会返回 Sun Mar 24 15:03:07 GMT 2013。我看不出这里有什么问题。有人能帮忙吗?

public static Date getFirstDayOfWeekDate()
{
    Calendar cal = Calendar.getInstance();
    cal.setFirstDayOfWeek(2);
    cal.set(Calendar.DAY_OF_WEEK,
    cal.getActualMinimum(Calendar.DAY_OF_WEEK));
    Date firstDayOfTheWeek = cal.getTime();
    return firstDayOfTheWeek;
}
4

4 回答 4

3

试试这个 :

public static Date getFirstDayOfWeekDate() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_WEEK,
            cal.getActualMinimum(Calendar.DAY_OF_WEEK));
    Date now = new Date();
    cal.setTime(now);
    int week = cal.get(Calendar.DAY_OF_WEEK);
    return new Date(now.getTime() - 24 * 60 * 60 * 1000 * (week - 1));
}
于 2013-03-19T15:18:27.707 回答
1

这个对我有用:

    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.setTime(new Date());
    int today = c.get(Calendar.DAY_OF_WEEK);
    c.add(Calendar.DAY_OF_WEEK, -today+Calendar.MONDAY);
    System.out.println("Date "+c.getTime());
于 2013-03-19T15:10:46.653 回答
1

只需在星期几中添加一个:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, cal.getActualMinimum(Calendar.DAY_OF_WEEK) + 1);
return cal.getTime();
于 2013-03-19T15:14:55.440 回答
0

或使用JodaTime

LocalDate.now().withDayOfWeek(DateTimeConstants.MONDAY);

来自Joda 时间:一周的第一天?

于 2013-03-19T15:22:13.270 回答