试图在本周获得星期一的日期

时间:2013-03-19 15:05:44

标签: java date calendar

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

但是,无论我做什么,它都会在2013年3月24日格林威治标准时间15:03:07返回。我看不出这里的问题。有人能帮忙吗?

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 个答案:

答案 0 :(得分: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));
}

答案 1 :(得分: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());

答案 2 :(得分:1)

只需在一周中添加一个:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, cal.getActualMinimum(Calendar.DAY_OF_WEEK) + 1);
return cal.getTime();

答案 3 :(得分:0)

JodaTime

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

来自Joda Time: First day of week?