Two Laptop Bag*
The moose likes Java in General and the fly likes Calendar class YEAR and DAY_OF_YEAR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calendar class YEAR and DAY_OF_YEAR" Watch "Calendar class YEAR and DAY_OF_YEAR" New topic

Calendar class YEAR and DAY_OF_YEAR

Herbert Kornfeld
Ranch Hand

Joined: Apr 16, 2003
Posts: 32
What I want is to just print out the current year and the day of the year - I am using this code:
Calendar calendar = Calendar.getInstance();
String year = Integer.toString(calendar.YEAR);
System.out.println("year is :" + calendar.YEAR);
System.out.println("year is :" + year);
String dayno = Integer.toString(calendar.DAY_OF_YEAR);
which is conistently printing out a 1 for the year and a 6 for the day of year - no matter when you run the program.
I am sure I am just missing something obvious but I can't figure out what it is.
Many Thanks - Happy Holidays!
fei long
Ranch Hand

Joined: Apr 04, 2002
Posts: 48
Hi Herbert,
the 'YEAR' or 'DAY_OF_YEAR' is used for 'get' and 'set' methods only according to API. So try following:
int year = calendar.get(calendar.YEAR);
int dayno = calendar.get(calendar.DAY_OF_YEAR);
Hope this can help you.
Herbert Kornfeld
Ranch Hand

Joined: Apr 16, 2003
Posts: 32
That's great - thanks a lot!
I agree. Here's the link:
subject: Calendar class YEAR and DAY_OF_YEAR
Similar Threads
Using Calendar.getInstance();
Possble Calendar bug with daylight saving time
add one day to current date
Static variable Question
Substracting days in java