Search code examples
javadategregorian-calendar

How to set a BCE year using GregorianCalendar


I have an assignment that converts dates from one calendar system to another.

The documentation for GregorianCalendar seems to suggest that you can use dates with BCE years, but I have no idea how. If I simply make the year negative, i.e.

 GregorianCalendar cal = new GregorianCalendar(-20, 1, 2, 3, 0, 0);
 System.out.println(cal.getTime.toString());

It prints out 'Sun Feb 02 03:00:00 GMT-05:00 21', which is clearly not correct.


Solution

  • You need to set the ERA to BC (BC is a static field on GregorianCalendar).

    The standard (Gregorian) calendar has 2 eras, BC and AD.

    http://java.sun.com/j2se/1.4.2/docs/api/java/util/GregorianCalendar.html

    e.g.

    calendar.set(Calendar.ERA, GregorianCalendar.BC);