获取当月日历中的所有日期 [英] Get all dates in calendar in current month

查看:386
本文介绍了获取当月日历中的所有日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取当前/某月日历中的所有日期?例如本月的
,例如图片

How to get all dates in the calendar in current/some month? for example for this month, like the picture

因此,结果是[ 07-31-2016, 08-01-2016, 2016年8月2日 ... ... 2016年8月31日, 2016年9月1日, 2016年9月2日, 2016年3月9日]

So the result is ["07-31-2016", "08-01-2016", "08-02-2016" ... "08-31-2016", "09-01-2016", "09-02-2016", "09-03-2016"]

有什么想法吗?,请先谢谢。

Any ideas?, thanks in advance.

推荐答案

好吧,用 Calendar 及其常量您可以轻松实现:

Well, with Calendar and its constants you can achieve this quite easy:

给出获取每月的第一天并将日历放在星期一:

Given month and year get first day of the month and place calendar on monday:

Calendar start = Calendar.getInstance();
start.set(MONTH, month - 1);  // month is 0 based on calendar
start.set(YEAR, year);
start.set(DAY_OF_MONTH, 1);
start.getTime();   // to avoid problems getTime make set changes apply
start.set(DAY_OF_WEEK, SUNDAY);
if (start.get(MONTH) <= (month - 1))  // check if sunday is in same month!
    start.add(DATE, -7);

给出获取月份的最后一天并将日历移至星期日

Given month and year get last day of month and move calendar to sunday

Calendar end = Calendar.getInstance();
end.set(MONTH, month);  // next month 
end.set(YEAR, year);
end.set(DAY_OF_MONTH, 1);
end.getTime();   // to avoid problems getTime make set changes apply
end.set(DATE, -1);
end.set(DAY_OF_WEEK, SATURDAY);
if (end.get(MONTH) != month)  
    end.add(DATE, + 7);

测试:

public static void main(String[] args) {
    int month = 8, year = 2016; 

    Calendar start = Calendar.getInstance();
    start.set(MONTH, month - 1);  // month is 0 based on calendar
    start.set(YEAR, year);
    start.set(DAY_OF_MONTH, 1);
    start.getTime();
    start.set(DAY_OF_WEEK, SUNDAY);
    if (start.get(MONTH) <= (month - 1))  
        start.add(DATE, -7);

    System.out.println(printCalendar(start));

    Calendar end = Calendar.getInstance();
    end.set(MONTH, month);  // next month 
    end.set(YEAR, year);
    end.set(DAY_OF_MONTH, 1);
    end.getTime();
    end.set(DATE, -1);
    end.set(DAY_OF_WEEK, SATURDAY);
    start.getTime();
    if (end.get(MONTH) != month)  
        end.add(DATE, + 7);

    System.out.println(printCalendar(end));
}

结合:

import static java.util.Calendar.*;

private final static SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
private static String printCalendar(Calendar c) {
    return df.format(c.getTime()); 
}

输出:

2016/07/31
2016/09/03

int month = 5, year = 2015; 

输出:

2015/04/26
2015/06/06

现在,只需迭代开始 Calendar +1 添加到 Calendar。 while 循环中的DATE (在示例中,我分几周进行了更清晰的显示):

Now, just iterate over starting Calendar adding +1 to Calendar.DATE in a while loop (in the example I split by weeks to be more clear):

int i = 1;
while (start.before(end)) {
    System.out.print(printCalendar(start));
    if (i % 7 == 0) {   // last day of the week
        System.out.println();
        i  = 1;
    } else {
        System.out.print(" - ");
        i++;
    }
    start.add(DATE, 1);
}

输出:

2015/04/26 - 2015/04/27 - 2015/04/28 - 2015/04/29 - 2015/04/30 - 2015/05/01 - 2015/05/02
2015/05/03 - 2015/05/04 - 2015/05/05 - 2015/05/06 - 2015/05/07 - 2015/05/08 - 2015/05/09
2015/05/10 - 2015/05/11 - 2015/05/12 - 2015/05/13 - 2015/05/14 - 2015/05/15 - 2015/05/16
2015/05/17 - 2015/05/18 - 2015/05/19 - 2015/05/20 - 2015/05/21 - 2015/05/22 - 2015/05/23
2015/05/24 - 2015/05/25 - 2015/05/26 - 2015/05/27 - 2015/05/28 - 2015/05/29 - 2015/05/30
2015/05/31 - 2015/06/01 - 2015/06/02 - 2015/06/03 - 2015/06/04 - 2015/06/05 - 2015/06/06

这篇关于获取当月日历中的所有日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆