Getting the start and the end date of a week using java calendar class

纵饮孤独 提交于 2019-11-29 12:37:41

问题


I want to get the last and the first week of a week for a given date. e.g if the date is 12th October 2011 then I need the dates 10th October 2011 (as the starting date of the week) and 16th october 2011 (as the end date of the week) Does anyone know how to get these 2 dates using the calender class (java.util.Calendar) thanks a lot!


回答1:


Some code how to do it with the Calendar object. I should also mention joda time library as it can help you many of Date/Calendar problems.

Code

public static void main(String[] args) {

    // set the date
    Calendar cal = Calendar.getInstance();
    cal.set(2011, 10 - 1, 12);

    // "calculate" the start date of the week
    Calendar first = (Calendar) cal.clone();
    first.add(Calendar.DAY_OF_WEEK, 
              first.getFirstDayOfWeek() - first.get(Calendar.DAY_OF_WEEK));

    // and add six days to the end date
    Calendar last = (Calendar) first.clone();
    last.add(Calendar.DAY_OF_YEAR, 6);

    // print the result
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println(df.format(first.getTime()) + " -> " + 
                       df.format(last.getTime()));
}



回答2:


This solution works for any locale (first day of week could be Sunday or Monday).

Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - c.getFirstDayOfWeek();
c.add(Calendar.DAY_OF_MONTH, -dayOfWeek);

Date weekStart = c.getTime();
// we do not need the same day a week after, that's why use 6, not 7
c.add(Calendar.DAY_OF_MONTH, 6); 
Date weekEnd = c.getTime();

For example, today is Jan, 29 2014. For the locale with Sunday as a first day of week you will get:

    start: 2014-01-26
    end: 2014-02-01

For the locale with Monday as a first day the dates will be:

    start: 2014-01-27
    end: 2014-02-02



回答3:


If you want all dates then

first.add(Calendar.DAY_OF_WEEK,first.getFirstDayOfWeek() - first.get(Calendar.DAY_OF_WEEK)); 

for (int i = 1; i <= 7; i++) {
    System.out.println( i+" Day Of that Week is",""+first.getTime());
    first.add(Calendar.DAY_OF_WEEK,1);
}



回答4:


Here is the sample code

public static void main(String[] args) {

    Calendar cal = Calendar.getInstance();
    cal.set(2016, 2, 15);

    {
        Calendar startCal = Calendar.getInstance();
        startCal.setTimeInMillis(cal.getTimeInMillis());

        int dayOfWeek = startCal.get(Calendar.DAY_OF_WEEK);
        startCal.set(Calendar.DAY_OF_MONTH,
                (startCal.get(Calendar.DAY_OF_MONTH) - dayOfWeek) + 1);

        System.out.println("end date : " + startCal.getTime());
    }

    {
        Calendar endCal = Calendar.getInstance();
        endCal.setTimeInMillis(cal.getTimeInMillis());

        int dayOfWeek = endCal.get(Calendar.DAY_OF_WEEK);
        endCal.set(Calendar.DAY_OF_MONTH, endCal.get(Calendar.DAY_OF_MONTH)
                + (7 - dayOfWeek));

        System.out.println("start date : " + endCal.getTime());

    }
}

which will print

start date : Sun Mar 13 20:30:30 IST 2016
end date : Sat Mar 19 20:30:30 IST 2016



回答5:


I have found the formula in the accepted answer will only work in some cases. For example your week starts on Saturday and today is Sunday. To arrive at the first day of the week we walk back 1 day, but the formula cal.get(Calendar.DAY_OF_WEEK) - cal.getFirstDayOfWeek() will give the answer -6. The solution is to use a modulus so the formula wraps around so to speak.

int daysToMoveToStartOfWeek =  (
  7 + 
  cal.get(Calendar.DAY_OF_WEEK) - 
  cal.getFirstDayOfWeek()
)%7;

cal.add(Calendar.DAY_OF_WEEK, -1 * daysToMoveToStartOfWeek);


来源:https://stackoverflow.com/questions/7645178/getting-the-start-and-the-end-date-of-a-week-using-java-calendar-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!