Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 997
hollaburoo Avatar asked Feb 02 '10 21:02

hollaburoo


People also ask

How do I change the TimeZone on the Gregorian calendar?

util. GregorianCalendar. setTimeZone() method is an in-built method in Java which modifies the current time zone to a new time zone according to the parameter passed. Parameters: The method takes one parameter tz of TimeZone object which specifies the new time zone value.

How do you use the Gregorian calendar?

The days of the year in the Gregorian calendar are divided into 7-day weeks, and the weeks are numbered 1 to 52 or 53. The international standard is to start the week on Monday. However, several countries, including the US and Canada, count Sunday as the first day of the week.

What is Gregorian date format?

YYYY/MM/DD - Sortable style Gregorian date format.


1 Answers

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);
like image 96
Michael Krauklis Avatar answered Oct 28 '22 11:10

Michael Krauklis