使用java日历类获取一周的开始和结束日期

发布于 2024-12-08 08:06:50 字数 163 浏览 1 评论 0原文

我想获取给定日期的一周的最后一周和第一周。 例如,如果日期是 2011 年 10 月 12 日,那么我需要日期 2011 年 10 月 10 日(作为一周的开始日期)和 2011 年 10 月 16 日(作为一周的结束日期) 有谁知道如何使用日历类(java.util.Calendar)获取这两个日期 多谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦亿 2024-12-15 08:06:50

一些代码如何使用 Calendar 对象执行此操作。我还应该提到 joda 时间库,因为它可以帮助您了解许多日期/日历问题。

代码

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()));
}

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()));
}
疏忽 2024-12-15 08:06:50

该解决方案适用于任何区域设置(一周的第一天可以是星期日或星期一)。

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

例如,今天是 2014 年 1 月 29 日。对于以星期日为一周第一天的区域设置,您将得到:

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

对于以星期一为第一天的区域设置,日期将为:

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

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
清浅ˋ旧时光 2024-12-15 08:06:50

如果你想要所有日期那么

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);
}

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);
}
纵情客 2024-12-15 08:06:50

示例代码

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

    }
}

这是将打印的

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

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
柠檬 2024-12-15 08:06:50

我发现接受的答案中的公式仅在某些情况下有效。例如,您的一周从星期六开始,今天是星期日。为了得到一周的第一天,我们向后走 1 天,但公式 cal.get(Calendar.DAY_OF_WEEK) - cal.getFirstDayOfWeek() 会给出答案 -6。解决方案是使用模数,因此可以说公式可以环绕。

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

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文