且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

日历类时间月不匹配

更新时间:2022-10-14 19:32:22

你想要的是以下成语: c.get(Calendar.MONTH)



Calendar.MONTH 本身只是一个内部常量,并且希望总是返回 2



> //我们在六月的时候写
//月份是0,因此June == 5
Calendar c = Calendar.getInstance();
System.out.println(Calendar.MONTH);
System.out.println(c.get(Calendar.MONTH));

输出

  2 
5

a href =https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#get%28int%29 =nofollow> API


Calendar c = Calendar.getInstance();

System.out.println("Mili->>" + c.getTimeInMillis());

System.out.println("Month  ->>" + Calendar.MONTH);

Though I am getting correct time in millisec format, Month is showing as 2 (March??) Why so?

Below is output

Mili->>1434029840778

Month ->>2

What you want is the following idiom: c.get(Calendar.MONTH).

Calendar.MONTH per se is just an internal constant and will (hopefully) always return 2.

Example

// We're in June at the time of writing
// Months are 0-based so June == 5
Calendar c = Calendar.getInstance();
System.out.println(Calendar.MONTH);
System.out.println(c.get(Calendar.MONTH));

Output

2
5

See also: API