File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Date Calculations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date Calculations" Watch "Date Calculations" New topic

Date Calculations

Janna Lockhart

Joined: Dec 21, 2000
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
//calculate difficult
Please help! Any ideas at all would be appreciated!

- Janna

Joined: Apr 10, 2001
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 =
Hope this helps

Janna Lockhart

Joined: Dec 21, 2000
Posts: 24
Thanks Alex!
I agree. Here's the link:
subject: Date Calculations
It's not a secret anymore!