• Post Reply Bookmark Topic Watch Topic
  • New Topic

Gregorian Calendar, extract ERA - incorrect result  RSS feed

 
Nigel Shrin
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I've written this routine to create calendar that is AD or BC.



The output from target_gCal.get(Calendar.ERA) and also from tostring shows both are AD??? ie ERA=1 for both.
Can someone please explain what I am doing wrong here?



many thanks!

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change your ifs to:


In GregorianCalendar year 0 is really 1BC, -1 is 2BC and so on.
If you want -1 to represent 1BC in your code use the code I posted. Why would you want to change behavior of GregorianCalendar?
What if an argument passed to your method is 0?
 
Nigel Shrin
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks Pawel!

I just wanted to output AD or BC in some output and the era was returned the same, so not trying to change the behaviour as such, and thanks for pointing out the 0 option, I had that covered further on in the method.

Kind regards, Nigel
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your question that you want to print the string "AD" instead of the code value 0? Why aren't you just using a DateFormat object instead of writing code to format the Calendar object yourself?
 
Nigel Shrin
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

I use simpledateformat frequently but had forgotten (never used) the ERA flag, so thanks for the heads up!




Works a treat, thanks!
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to hear it!

(To tell the truth, I hadn't ever used the "era" code either but I just took a leap in the dark and assumed that it existed.)
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!