Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Day Of Year in GregorianCalendar

Status
Not open for further replies.

tmryan

Programmer
Dec 22, 2000
73
US
Can anyone tell me why the following code always seems to be off one month in the DAY_OF_YEAR?

Thanks

import java.awt.*;
import java.util.*;

public class playdate
{
public static void main(String args[])
{
GregorianCalendar g = new GregorianCalendar(01,10,8);
int year = g.get(Calendar.YEAR);
int month = g.get(Calendar.MONTH);
int date = g.get(Calendar.DATE);
int day = g.get(Calendar.DAY_OF_YEAR);



System.out.println("Year = " + year);
System.out.println("Month = " + month);
System.out.println("Date = " + date);
System.out.println("DOY = " + day);
System.out.println("There are " + (365 - day) + " days left in the year.");

}
} Tim Ryan
PROGRESS Developer
 
In the GregorianCalendar class, the month is 0 based. January is 0, Feburary is 1, etc. That is where your difference is coming from.
 
Geez - how stupid of me. I should have RTFM.

Thanks Tim Ryan
PROGRESS Developer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top