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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help, calculating remaing days in year 1

Status
Not open for further replies.

darwin101

Programmer
Mar 7, 2001
156
US
i need to calculate the days remaining in this year (End of year - today) i am just starting java, any suggestions on how to do this? i know there is a class called "calendar" but i dont know how to use it :(
thanks for your help.
 
The Calendar class is pretty good for this:

Calendar date = Calendar.getInstance();
^^^ this line sets date with the current date and time

int dayofyear = date.get( Calendar.DAY_OF_YEAR );
^^^ this will give you the number of days that have already past in the current year

date.set( Calendar.MONTH, Calendar.DECEMBER );
date.set( Calendar.DAY_OF_MONTH, 31 );
int lastdayofyear = date.get( Calendar.DAY_OF_YEAR );
^^^ these lines will set date to the last day of the current year, and then return the day number of the last day of the year. this is so you dont have to worry about leapyears.

int daysleft = lastdayofyear - dayofyear;
^^^ just subtract the current day of the year from the last day of the year, and there you are :)

Hope this helps
 
thanks Gerald, this works great, and i am happy that you explained it so well! "I.T." needs more people like you.
:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top