Why GregorianCalendar.getInstance contains calsys and cdate of Julian Calendar type

I tried setting the date value in PreparedStatement with a default, but the value is sometimes returned as JulianValue. For example (assume spanBegin and spanEnd are zero)

Calendar cal = new GregorianCalendar();
if (spanBegin == null) {
    cal.set(0000, Calendar.JANUARY, 1);
    spanBegin = cal.getTime();
}

if (spanEnd == null)
{
    cal.set(9999, Calendar.DECEMBER, 31);
    spanEnd = cal.getTime();
}

      

On line number 3, since January 1, 0000 is limited to the Julian calendar, the CD file becomes the Julian calendar. However, the next date, even if it is in the year 9999, its CDate becomes the Julian calendar. I had to create another instance of the Gregorian calendar to fix this issue.

Calendar cal = new GregorianCalendar();
if (spanBegin == null) {
    cal.set(0000, Calendar.JANUARY, 1);
    spanBegin = cal.getTime();
}

Calendar cal = new GregorianCalendar();
if (spanEnd == null)
{
    cal.set(9999, Calendar.DECEMBER, 31);
    spanEnd = cal.getTime();
}

      

The question is, is this expected behavior or a bug in the date object? In fact, using GregorianCalendar.getInstance () shows that JulianCalendar is sometimes set in cdate.

+2


source to share


3 answers


Which version of Java are you using and on which OS? Do you really need to store dates in years 0 and 9999, or are you just using them as negative infinity and positive infinity values? How do you see that the calendar is a Julian calendar?

I've tried this:

Calendar cal = Calendar.getInstance();

cal.set(0, Calendar.JANUARY, 1);
Date d1 = cal.getTime();

cal.set(9999, Calendar.DECEMBER, 31);
Date d2 = cal.getTime();

System.out.println(d1);
System.out.println(d2);

      

Output (on Windows XP using Sun Java 1.6.0_16):

Thu Jan 01 09:53:56 CET 1 java.util.Date
Tue Dec 31 09:53:56 CET 9999 java.util.Date

      



It changes year 0 to year 1. Changing the code to use the second calendar object for the second date:

Calendar cal = Calendar.getInstance();

cal.set(0, Calendar.JANUARY, 1);
Date d1 = cal.getTime();

Calendar cal2 = Calendar.getInstance();
cal2.set(9999, Calendar.DECEMBER, 31);
Date d2 = cal2.getTime();

System.out.println(d1);
System.out.println(d2);

      

This doesn't change anything for the output or content of the two objects Date

.

Note. Remember that integer literals starting with 0, for example 0000

in your code, will be interpreted as octal numbers by the Java compiler. It doesn't matter in this case, because the number is 0, but you shouldn't add integer literals with zeros unless you mean them as octal numbers.

+1


source


There was no Gregorian calendar until 1582. The Julian calendar was used throughout Europe until there were minor problems caused by the fact that the solar year is not exactly 365.25 days, but slightly less than that. To remedy the situation, Pope Gregory XIII ordered to change the calendar to what we know today - every year that divides by 100 is not a leap year unless it divides by 400. In October 1582, a transition took place - a day after October 4 was on October 15th. This means that until October 1582 the Gregorian and Julian calendars are the same. You can read about it here



This is why the dates before October 1582 are converted to use the Julian system. According to the API If you really need to represent a historical event (which doesn't seem to be the case here), you can only do so from March 1st, 4AD

+2


source


There is no year 0 in the Julian calendar. It runs from 1 to 1 AD.

+1


source







All Articles