This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calendar fields

 
Paid O'Cuana
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble with the Calendar class.
I create my Calendar like so:
Calendar rightNow = new GregorianCalendar();
When I check what year it is
System.out.println("The year is "+rightNow.YEAR);
it prints out '1' for some reason
I even tried
rightNow.set(rightNow.YEAR,2003);
and the did the System.out and it still said 1.
If I print out the string version of the Calendar instance
System.out.println(rightNow.toString());
it shows the YEAR field as 2003, but any operation I use YEAR in has YEAR set to 1.
Any idea what stupid mistake I'm making?
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try
rightNow.get(Calendar.YEAR)
You are printing the value of the YEAR constant not getting the year from the Calendar.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic