日历设置YEAR问题 [英] Calendar set YEAR issue

查看:184
本文介绍了日历设置YEAR问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下代码:

 日历c1 = Calendar.getInstance(); 
c1.set(Calendar.YEAR,0);
c1.set(Calendar.DAY_OF_YEAR,1);
日期d1 = c1.getTime();

日历c2 = Calendar.getInstance();
c2.setTime(d1);
c2.set(Calendar.YEAR,2001);
c2.set(Calendar.DAY_OF_YEAR,1);
System.out.println(c2.getTime()。toString());

日历c3 = Calendar.getInstance();
c3.set(Calendar.YEAR,2000);
c3.set(Calendar.DAY_OF_YEAR,1);
日期d2 = c3.getTime();

日历c4 = Calendar.getInstance();
c4.setTime(d2);
c4.set(Calendar.YEAR,2001);
c4.set(Calendar.DAY_OF_YEAR,1);
System.out.println(c4.getTime()。toString());

结果是:

  Wed Jan 01 23:47:00 CET 2001 
Mon Jan 01 23:47:00 CET 2001

有什么问题?不应该以这种方式使用日历设置YEAR?

解决方案

这一年是相对于时代的。通过将年份设置为小于或等于0的日历,日历将通过切换时代(从AD到BC或从BC切换到AD)来自动更正。这种行为从其他领域更为人所知。例如。如果将月份设置为负值,则年份相应减少。



这些更正不是单独进行的,而是一次性完成,通常当您打电话时code> getTime()以读出生成的日期。

 日历c1 =日历.getInstance(); // 2012年8月16日AD 
c1.set(Calendar.YEAR,0); // 8月16日,0 AD
c1.set(Calendar.DAY_OF_YEAR,1); // 1月1日,0广告
日期d1 = c1.getTime();公元前1年1月1日(更正)

日历c2 = Calendar.getInstance();
c2.setTime(d1);
c2.set(Calendar.YEAR,2001); // 2001年1月1日BC
c2.set(Calendar.DAY_OF_YEAR,1);
System.out.println(c2.getTime()); //打印Wed Jan 01 05:35:00 CET 2001
//因为01/01/2001 BC是星期三

因此,不要将年份设置为2001年,您必须将其设置为-2000(因为0年不存在)。或者你可以明确地设置这个时代:

  c2.set(Calendar.ERA,GregorianCalendar.AD); 

另一种解决这个错误的方法是通过阅读设置完成日期之前的时间:

 日历c1 = Calendar.getInstance(); // 2012年8月16日AD 
c1.set(Calendar.YEAR,0); // 8月16日,0 AD
c1.set(Calendar.DAY_OF_YEAR,1); // 1月1日,0 AD
c1.set(Calendar.YEAR,2001); // 2001年1月1日AD
System.out.println(c1.getTime()); //打印预期日期

要输出日期的时代,您可以使用G SimpleDateFormat 的模式:

 新SimpleDateFormat(E MMM dd HH :mm:ss z yyyy G)。format(c2.getTime())
//Wed Jan 01 05:35:00 CET 2001 BC
pre>

I tried the following piece of code:

Calendar c1 = Calendar.getInstance();
c1.set(Calendar.YEAR, 0);
c1.set(Calendar.DAY_OF_YEAR, 1);
Date d1 = c1.getTime();

Calendar c2 = Calendar.getInstance();
c2.setTime(d1);
c2.set(Calendar.YEAR, 2001);
c2.set(Calendar.DAY_OF_YEAR, 1);
System.out.println(c2.getTime().toString());

Calendar c3 = Calendar.getInstance();
c3.set(Calendar.YEAR, 2000);
c3.set(Calendar.DAY_OF_YEAR, 1);
Date d2 = c3.getTime();

Calendar c4 = Calendar.getInstance();
c4.setTime(d2);
c4.set(Calendar.YEAR, 2001);
c4.set(Calendar.DAY_OF_YEAR, 1);
System.out.println(c4.getTime().toString());

The result is:

Wed Jan 01 23:47:00 CET 2001
Mon Jan 01 23:47:00 CET 2001

What is wrong? Shouldn't I use Calendar in this way for setting YEAR?

解决方案

The year is relative to the era. By setting the year to something less or equal to 0 the calendar automatically corrects this by switching the era (from AD to BC or from BC to AD). This behaviour is better known from the other fields. E.g. if you set the month to something negative the year gets decremented accordingly.

Those corrections aren't made individually but rather they are made all at once, usually when you call getTime() to read out the resulting date.

Calendar c1 = Calendar.getInstance(); // August  16th, 2012 AD
c1.set(Calendar.YEAR, 0);             // August  16th,    0 AD
c1.set(Calendar.DAY_OF_YEAR, 1);      // January  1st,    0 AD
Date d1 = c1.getTime();               // January  1st,    1 BC (corrected)

Calendar c2 = Calendar.getInstance();
c2.setTime(d1);
c2.set(Calendar.YEAR, 2001);          // January  1st, 2001 BC
c2.set(Calendar.DAY_OF_YEAR, 1);
System.out.println(c2.getTime());     // prints "Wed Jan 01 05:35:00 CET 2001"
                                      // because 01/01/2001 BC was a Wednesday

So instead of setting the year to 2001 you would have to set it to -2000 (because year 0 doesn't exist at all). Or you could explicitly set the era:

c2.set(Calendar.ERA, GregorianCalendar.AD);

Another way to solve this "bug" is by not reading out the time before the complete date is set:

Calendar c1 = Calendar.getInstance(); // August  16th, 2012 AD
c1.set(Calendar.YEAR, 0);             // August  16th,    0 AD
c1.set(Calendar.DAY_OF_YEAR, 1);      // January  1st,    0 AD
c1.set(Calendar.YEAR, 2001);          // January  1st, 2001 AD
System.out.println(c1.getTime());     // prints the expected date

To output the era of a date you can use "G" in the pattern of a SimpleDateFormat:

new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy G").format(c2.getTime())
// "Wed Jan 01 05:35:00 CET 2001 BC"

这篇关于日历设置YEAR问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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