为什么将当年的当前日期另存为3912? [英] Why the current year in the date saved as 3912?

查看:65
本文介绍了为什么将当年的当前日期另存为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)描述:为此日期对象设置自1900年起的公历。因此, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆