Calendar.Month gives wrong output

后端 未结 4 1064
滥情空心
滥情空心 2020-11-28 16:09

I have been using java.util for all date and calendar representations. But I am facing a strange problem here. Calendar.MONTH, Calendar.DAY_O

4条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 16:50

    Calendar.MONTH doesn't return the current month. It is a constant whose value is 2.

    From the source:

    public final static int MONTH = 2;
    

    It is for use as a parameter in Calendar's get method

    public int get(int field) {...}
    

    The design of Calendar is from the early days of Java, when many of today's conventions were different, non-existant, or developing. Oracle (and earlier, Sun), would never let an API like the java.util.Calendar API become part of the standard API today.

    And, for completeness, use Joda Time instead of Java's Date and Calendar API's.

提交回复
热议问题