Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Date Calculations

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks Alex!
 
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic