• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Date Calculations

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}
Please help! Any ideas at all would be appreciated!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alex!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic