# Date Calculations

Janna Lockhart
Greenhorn
Posts: 24
I'm trying to make a simple application in which the user selects their birthdate from a month JComboBox and a day JComboBox. I then want to take this birthday and calculate how many days from today it is until their birthday. Since you initialize JComboBox with a String, I have just concatenated the input to a single string value that equals mm/dd/yyyy, the yyyy part of which I retrieve by getting the current year. My question is, is their a method that figures out what day of the year a particular date is? As in day 1 = Jan 1 and day 365 = Dec 31? Therefor, I would just need to figure whether or not their birthday has already occured this year. If it hasn't, I say the number of days until their birthday is equal to ((365 - number their bday falls on) - (todays date)). If their birthday has already passed for this year, the value would be ((365 - number of days for todays date) + (number their bday falls on)).
void computeBDay(String input)
{
java.util.Date presentDate = new java.util.Date();
java.util.Date birthDay = new java.util.Date(input);
if (presentDate.before(birthDay))
{
//then calculate easy
}
else
{
//calculate difficult
}

alexleong
Greenhorn
Posts: 1
hi Janna Lockhart,
All you have to do is:
//get today date
Date todaydate = new Date();
//format the birthday date in String to Date Object
DateFormat df =
DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
String birthday="04/10/2001";
Date dbirthday= df.parse(birthday);
//calculate the different
//if negative value => the birthday past
//else => hasn't past yet
long diff = dbirthday.getTime()-todaydate.getTime();
long daysecond = 24*60*60000L;
long numofday =
(dbirthday.getTime()-todaydate.getTime())/daysecond;
Hope this helps
Alex

Janna Lockhart
Greenhorn
Posts: 24
Thanks Alex!