• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Gregorian Calendar always returns same values

 
Greenhorn
Posts: 2
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic