• Post Reply Bookmark Topic Watch Topic
  • New Topic

Gregorian Calendar always returns same values  RSS feed

 
Rami Zahr
Greenhorn
Posts: 2
Android Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble using the GregorianCalendar class. Everytime I set the date when I instantiate the class, I always get the field MONTH = 2, DAY_OF_MONTH = 1 and YEAR = 1. Here is the code that I have:



The output is the following:
1
2 5 1

What am I doing wrong?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are printing out the static final constants which will always be the same values. Use the get method to access the values you want e.g

 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
A lot of people think the standard Java date and time classes are not very good; try googling for Joda time as an alternative.
 
Rami Zahr
Greenhorn
Posts: 2
Android Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for both welcoming me and for help answering my question! Just so I understand this correctly, the static field is inherited from the Calendar class and cannot be modified whereas the set method lets you change the field with the same name (for example YEAR) in the instantiated Gregorian calendar object. Is that correct?
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those fields are intended to find the field to change. The method looks something like thisSo when you pass MONTH, it finds the month field and returns it. And of course, as we all know, JANUARY = 0. A Date object doesn’t store those particular fields, but would have to calculate them from its long field, which is a sort of timestamp in milliseconds since 1st January 1970. I don’t know whether a Calendar object stores its data as separate fields, or calculates those values when they are required. Not that it matters which.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!