RSS Forums RSS
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 4352 | Replies: 1
Reply
Join Date: Mar 2006
Posts: 2
Reputation: Fredjava is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Fredjava Fredjava is offline Offline
Newbie Poster

trying to do a date of birth comparison.

  #1  
Mar 14th, 2006
I am trying to write a date of birth compair. here is how i have started out but I am not getting the results I want to see I am wanting to enter the text as

03/14/73 and compaire that to todays date and tell the user how old they are in only years.

This is as far as I can get with it and I will be totaly truthfull I did not write the hole thing this is the beginging of a class assinment and I cant find were on the net to get help. Sugestions. Please.

System.out.print("Enter the year you were born: ");
        	 usrYear = getYear(year);
        	 userYr = Integer.parseInt(usrYear);
        		 
	System.out.print("Enter the month you were born: ");
       		 usrMonth = getMonth();
       		 userMth = Integer.parseInt(usrMonth);

	 System.out.print("Enter the day you were born: ");
       		 usrDay = getDay(userMth);
       		 userDay = Integer.parseInt(usrDay);

	 System.out.println("Your birthday is on " + userMth + "/" + userDay + "/" + userYr);
       		 displayAge(userYr, year, userMth, month, userDay, day);
	 }
        		 
	 public static String getYear(int yr)throws Exception {		 
 		 int outYear=0;
 		 int year;
 		 year = yr;
 		 String usrYear = new String();
 		 String output = new String();
 		 usrYear = getString();

AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Posts: 231
Reputation: sam1 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

Re: trying to do a date of birth comparison.

  #2  
Mar 14th, 2006
hi,

here is a simple program that gives you your age month and day...give the date of birth as an argument though...
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class Age {

  public Age(long birthday) {
    Calendar age = Calendar.getInstance(Locale.getDefault());
    age.setTimeInMillis(Math.abs(birthday-System.currentTimeMillis()));
    System.out.println("Your are "+(age.get(Calendar.YEAR)-1970)+" years, "+age.get(Calendar.MONTH)+" month, "+age.get(Calendar.DAY_OF_MONTH)+" days "+(System.currentTimeMillis()-birthday<0 ?"Not Born Yet" :"Old")+".");
  }

  public static void main(String[] args) {
    SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
    if (args.length==0) {
      System.out.println("Usage:\n java Age dd/mm/yyyy\nWhere:\n dd - date\n mm - month\n yyyy - year");
    } else {
      try {
        final Date birthday = dateFormatter.parse(args[0]);
        System.out.println("Your birthday is " + dateFormatter.format(birthday));
        final Age myAge = new Age(birthday.getTime());
      } catch (ParseException e) {
        System.out.println("Error parsing your birthday.");
        e.printStackTrace();
      }
    }
  }

}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:52 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC