Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Time Android:calender没有给出全年数字,只给出111而不是2011年_Time_Android Emulator_Emulation_Android - Fatal编程技术网

Time Android:calender没有给出全年数字,只给出111而不是2011年

Time Android:calender没有给出全年数字,只给出111而不是2011年,time,android-emulator,emulation,android,Time,Android Emulator,Emulation,Android,我使用以下代码获取当前设备时间的年份: Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(System.currentTimeMillis()); cal.getTime().getYear(); 但我得到的是111,而不是今年的2011 如果我把2012年设为当前年份,那么它会给我112而不是2012年 为什么会这样? 请帮帮我。 谢谢。The()返回自1900年以来的年份。所以你必须使用 cal.getTime().get

我使用以下代码获取当前设备时间的年份:

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.getTime().getYear();
但我得到的是111,而不是今年的2011

如果我把2012年设为当前年份,那么它会给我112而不是2012年

为什么会这样? 请帮帮我。 谢谢。

The()返回自1900年以来的年份。所以你必须使用

cal.getTime().getYear() + 1900
编辑

不推荐使用
Date.getYear()
。因此,您可以使用
cal.get(Calendar.YEAR)
获取年份字段。请参见和

The()返回自1900年以来的年份。所以你必须使用

cal.getTime().getYear() + 1900
编辑


不推荐使用
Date.getYear()
。因此,您可以使用
cal.get(Calendar.YEAR)
获取年份字段。请参见和

+1。“返回此日期对象自1900年起的公历年。”您确实应该使用
cal.get(calendar.year)-1900
,尽管谢谢@Michell,我编辑了我的答案。事实上,他希望今年和2011年一样。因此,不需要使用1900进行减法运算。好的,感谢这两种方法的回复。此方法已被弃用。use Calendar.get(Calendar.YEAR)-1900返回此日期对象自1900年起的公历年。所以这是真的+1。“返回此日期对象自1900年起的公历年。”您确实应该使用
cal.get(calendar.year)-1900
,尽管谢谢@Michell,我编辑了我的答案。事实上,他希望今年和2011年一样。因此,不需要使用1900进行减法运算。好的,感谢这两种方法的回复。此方法已被弃用。use Calendar.get(Calendar.YEAR)-1900返回此日期对象自1900年起的公历年。所以这是真的