aspose file tools*
The moose likes Java in General and the fly likes Current Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Current Date" Watch "Current Date" New topic
Author

Current Date

Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
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

Joined: Jan 07, 1999
Posts: 6920
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:


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
Got it!
Thanks alot!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Current Date
 
Similar Threads
displaying 2 weeks dates
Question about GregorianCalendar class
how to get system date
GregorianCalendar -- Calendar.SECOND
Calculate date