日期:2014-05-19  浏览次数:20825 次

Calendar用法求解
小弟现在用Calendar循环推算时间,方法为:
Java code

public static void main(String[] args) throws ParseException {
        // TODO Auto-generated method stub
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        Date sDate = new Date();
        Date date = new Date();
        int dayssh = c.get(Calendar.DATE);
        for (int i = 0; i < 50; i++) {
            c.set(Calendar.DECEMBER, dayssh-1);
//                      c.set(Calendar.DAY_OF_MONTH, dayssh-1);
            Date as = c.getTime();
            date.setDate(sDate.getDate() - i);
            System.out.println(date.toLocaleString());
        }    
    }


输出的结果却是:
2012-6-26 15:57:01
2012-6-25 15:57:01
2012-6-24 15:57:01
2012-6-23 15:57:01
2012-6-22 15:57:01
2012-6-21 15:57:01
2012-6-20 15:57:01
2012-6-19 15:57:01
2012-6-18 15:57:01
2012-6-17 15:57:01
2012-6-16 15:57:01
2012-6-15 15:57:01
2012-6-14 15:57:01
2012-6-13 15:57:01
2012-6-12 15:57:01
2012-6-11 15:57:01
2012-6-10 15:57:01
2012-6-9 15:57:01
2012-6-8 15:57:01
2012-6-7 15:57:01
2012-6-6 15:57:01
2012-6-5 15:57:01
2012-6-4 15:57:01
2012-6-3 15:57:01
2012-6-2 15:57:01
2012-6-1 15:57:01
2012-5-31 15:57:01
2012-4-29 15:57:01
2012-3-29 15:57:01
2012-2-26 15:57:01
2012-1-27 15:57:01
2011-12-26 15:57:01
2011-11-24 15:57:01
2011-10-24 15:57:01
2011-9-22 15:57:01
2011-8-22 15:57:01
2011-7-21 15:57:01
2011-6-19 15:57:01
2011-5-19 15:57:01
2011-4-17 15:57:01
2011-3-17 15:57:01
2011-2-13 15:57:01
2011-1-15 15:57:01
2010-12-14 15:57:01
2010-11-12 15:57:01
2010-10-12 15:57:01
2010-9-10 15:57:01
2010-8-10 15:57:01
2010-7-9 15:57:01
2010-6-7 15:57:01
为什么算到5月31号就开始跳错了呢?求解

------解决方案--------------------
你这输出的结果和calendar有关系吗?

------解决方案--------------------
楼主你要干什么呀,我对你无语,java都被你糟蹋成这样了,你打印出dayssh-i看看结果是多少
public static void main(String[] args) throws ParseException {
Calendar c = Calendar.getInstance();
int dayssh = c.get(Calendar.DATE);
for (int i = 0; i < 50; i++) {
c.set(Calendar.DATE, dayssh - i);
System.out.println(dayssh - i + "-----------------");
System.out.println(c.getTime().toLocaleString());
}

}
结果是:26-----------------
2012-6-26 17:01:14
25-----------------
2012-6-25 17:01:14
24-----------------
2012-6-24 17:01:14
23-----------------
2012-6-23 17:01:14
22-----------------
2012-6-22 17:01:14
21-----------------
2012-6-21 17:01:14
20-----------------
2012-6-20 17:01:14
19-----------------
2012-6-19 17:01:14
18-----------------
2012-6-18 17:01:14
17-----------------
2012-6-17 17:01:14
16-----------------
2012-6-16 17:01:14
15-----------------
2012-6-15 17:01:14
14-----------------
2012-6-14 17:01:14
13-----------------
2012-6-13 17:01:14
12-----------------
2012-6-12 17:01:14
11-----------------
2012-6-11 17:01:14
10-----------------
2012-6-10 17:01:14
9-----------------
2012-6-9 17:01:14
8-----------------
2012-6-8 17:01:14
7-----------------
2012-6-7 17:01:14
6-----------------
2012-6-6 17:01:14
5-----------------
2012-6-5 17:01:14
4-----------------
2012-6-4 17:01:14
3-----------------
2012-6-3 17:01:14
2-----------------
2012-6-2 17:01:14
1-----------------
2012-6-1 17:01:14
0------