views:

57

answers:

2

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.

+3  A: 

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);
Michael Krauklis
A: 

The documentation for this can be found: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html#ERA

It shows Calendar.ERA and how both GregorianCalendar.AD and GregorianCalendar.BC can be used

Jason Aller