【JAVA】Calendar

时间:2022-04-12 05:32:47
Calendar是JDK 1.1增加的类
最近使用了下Calendar发现几个很让人抓狂的问题

源码:
public final static int SUNDAY = 1;
public final static int MONDAY = 2;
public final static int TUESDAY = 3;
public final static int WEDNESDAY = 4;
public final static int THURSDAY = 5;
public final static int FRIDAY = 6;
public final static int SATURDAY = 7;
你发现了么。是的这是真的,星期一(MONDAY) 对应的值为2,不是一,其他的值也是都向后措了1位数字。
 
同样的,源码:
public final static int JANUARY = 0;  
public final static int FEBRUARY = 1; 
public final static int MARCH = 2;
public final static int APRIL = 3;
public final static int MAY = 4;
public final static int JUNE = 5;
public final static int JULY = 6;
public final static int AUGUST = 7;
public final static int SEPTEMBER = 8;
public final static int OCTOBER = 9;
public final static int NOVEMBER = 10;
public final static int DECEMBER = 11;
没错,月份也有相同的问题。一不小心就会把时间处理错