日历类的困惑 [英] Calendar class confusion

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

问题描述

我正在玩Calendar类,并得到了一些令人困惑的结果:

I was playing with the Calendar class and got some confusing results:

    Calendar thisCal = Calendar.getInstance();
    thisCal.clear();

    thisCal.set(2012,12,8);

    System.out.println("Year is: " + thisCal.get(Calendar.YEAR));
    System.out.println("Month is: " + thisCal.get(Calendar.MONTH));
    System.out.println("Day of Month is: " + thisCal.get(Calendar.DAY_OF_MONTH));

输出:

年份是:2013

月份为:0

每月的天是:8

我很困惑!

推荐答案

MONTH 字段是从零开始的(我认为是从POSIX API继承的).因此,您将其设置为2012年的第13个月,它被解释为2013年的第一个月(数字0).

The MONTH field is zero based (inherited from some POSIX API, I think). So you're setting it to the 13th month of 2012, which it interprets as the first month (with number 0) of 2013.

如果将 lenient 属性设置为 false ,则会引发Exception.

If you set the lenient property to false, it would throw an Exception instead.

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

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