本文介绍了日历类时间月不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 日历c = Calendar.getInstance(); 

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

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

虽然我以毫秒格式获得正确的时间,但月份显示为2


解决方案

你想要的是以下成语: 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

解决方案

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

这篇关于日历类时间月不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:24