Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Current Date

 
Ronnie Phelps
Ranch Hand
Posts: 329
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;
public class PrintDate{
public static void main(String args[]){
GregorianCalendar cal = new GregorianCalendar();
System.out.println(cal.YEAR);
System.out.println(cal.MONTH);
}
}
Why does this print? 1 and 2 and Not the current year and the current month as one would expect?
 
Frank Carver
Sheriff
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cal.YEAR and cal.MONTH are constants representing field indexes, not the actual year and month. Imagine a Calendar instance as an array of useful date information, and the constants as the way you find out what information lives in which "slot" of the array.
To show the year and month you need to read the value of the specified array "slot" using the "get" method:
 
Ronnie Phelps
Ranch Hand
Posts: 329
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it!
Thanks alot!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic