• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calendar.getActualMaximum()  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that for Japanese locale, Calendar.getActualMaximum(Calendar.YEAR) for Taisho era is wrong.

1: Locale locale = new Locale("ja", "jp", "JP");
2: Calendar ca = Calendar.getInstance(locale);
3: ca.set(Calendar.ERA, 2); //Taisho
4: int maxyear = ca.getActualMaximum(Calendar.YEAR);

When the above code is executed, maxyear returned 63 (the max year for Showa period). It should be returning 15. Does anyone see anything wrong with the above code?
After line 3 is executed above, the ERA for the calendar is "3" or Showa, not Taisho.
 
Sheriff
Posts: 21136
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably get some overflow. Before setting the ERA, the year is probably higher than 15. When you set the ERA, you get year X of ERA 2 which is actually year Y of ERA 3.

For instance, if I try your code but add some printlns:

If I set the year to 1 before setting the ERA, the numbers become 26 (original year), 4 (original era), 15 (max year), 1 (new year) and 2 (new era).
 
Tet Thach
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right. Not sure why you have to set the date to get the actual maximum. I would assume if you change the era, it should probably init the date to the start date of that era.



Gives me the correct result.

ERA: 明治 <====Meiji
getActualMax = 44
Current date = 09/08/1868
ERA: 大正 <====Taisho
getActualMax = 15
Current date = 07/30/1912
ERA: 昭和 <====Showa
getActualMax = 63
Current date = 12/25/1926
ERA: 平成<====Heisei
getActualMax = 292277006
Current date = 01/08/1989
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tet Thach wrote:You're right. Not sure why you have to set the date to get the actual maximum. I would assume if you change the era, it should probably init the date to the start date of that era.

Just one reason why I detest the Calendar interface.

You may find more intuitive stuff in the new Date/Time classes in version 8; or - if you can't upgrade yet - have a look at Joda time.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!