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.
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.