为什么将日期中的当前年份保存为 3912? [英] Why the current year in the date saved as 3912?
本文介绍了为什么将日期中的当前年份保存为 3912?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
获取当前日期和时间
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
创建当前日期对象
Date toDate;
toDate.setYear(mYear);
toDate.setMonth(mMonth);
toDate.setDate(mDay);
Date endDate = toDate;
打印 endDate 对象时我得到了
when printing endDate object I got
Mon Jan 01 13:11:00 GMT+03:00 3912
为什么?
推荐答案
来自 Date.setYear(int)
描述:为此 Date 对象设置自 1900 年以来的公历年.因此,1900 + 2012 = 3912
.
From Date.setYear(int)
description: Sets the gregorian calendar year since 1900 for this Date object. Thus, 1900 + 2012 = 3912
.
但 calendar.get(Calendar.YEAR)
返回准确的年份编号 2012
.因此,API 的这种不一致会导致您的问题.但无论如何 Date.setYear(int)
已被弃用,因此,最好使用 Calendar
对象进行日期计算.
But calendar.get(Calendar.YEAR)
returns exact year number 2012
. So this inconsistency of API causes your issue. But anyway Date.setYear(int)
is deprecated, thus, it is better to use Calendar
object for date calculations.
这篇关于为什么将日期中的当前年份保存为 3912?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文