문제

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.

도움이 되었습니까?

해결책

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);

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top