2013-03-19 74 views
4

我已經實施了一種方法來獲取當週的星期一的日期,並且我已將星期一指定爲本週的第一天。試圖獲取當週的星期一的日期

但是,無論我做什麼,它都會返回Sun 3月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; 
} 

回答

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

它爲我的作品:

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()); 
+1

這讓我下週一。 – 2013-09-15 14:07:49

0

只需添加一到星期幾:

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