Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 73
Registered: ‎09-03-2009
My Device: Not Specified
Accepted Solution

Finding Age using two dates...

Hello,

 

I need to find the age of a person based the DOB entered.  the age can be in days or in years depending on when the person is born. 

 

If the person is born is less than a year then I should be able to calculate the no of days else it should be in years.

 

I cannot find the correct way of doing it.  I tried like this but its not working as it should:

 

 

long todaysDate = (new Date()).getTime();
long dob = dobDateField.getDate();

long dateDiff = todaysDate - dob;

//multiply one day with 365 days (ignore the leap year for now)
long year = DateTimeUtilities.ONEDAY * 365;

if (dateDiff < year) {
   int NoOfDays = (int) (dateDiff / DateTimeUtilities.ONEDAY);
   ageEditField.setText(String.valueOf(NoOfDays) + "D");
 
} else {
   int NoOfYears = (int) (dateDiff /year);
   ageEditField.setText(String.valueOf(NoOfYears));
}

 

 

Please help.... :smileysad:

Developer
Posts: 1,055
Registered: ‎01-25-2009
My Device: BlackBerry Bold 9650/BlackBerry Torch 9800

Re: Finding Age using two dates...

 

int age = (diff / (1000 * 60 * 60 * 24));
int years = 0;

if( age >= 365 ) {
   years = age / 365;
}

if( years > 0 ) {
 // use years 
} else {
 // use days
}

 

Something like that.

 

Jerome Carty - Follow jcarty on Twitter@jcarty | #webworks-dev / #BlackberryDev on irc.freenode.net | My Apps
Click "Accept as Solution" if post solved your original issue.. Give like/thumbs up if you feel post is helpful
Developer
Posts: 73
Registered: ‎09-03-2009
My Device: Not Specified

Re: Finding Age using two dates...

Yes, you gave me the clue buddy!  I should have converted to days first then check for the years.

 

here is the final code which works great. :smileyhappy:  just incase if anyone wants it.

 

long todaysDate = (new Date()).getTime(); // gets today's date
long dob = dobDateField.getDate(); // gets the entered dob

// finds the no of days between today and the dob in long
long dateDiff = todaysDate - dob; 

// converts the date diff into no of days
int NoOfDays = (int) (dateDiff / DateTimeUtilities.ONEDAY); 

// see the no of days are more than a year (ignore the leap year for
// now)
if (NoOfDays > 365) {
   // finds the no years in difference
   int NoOfYears = NoOfDays / 365;
   ageEditField.setText(String.valueOf(NoOfYears));
} else {
   // paints the no of days in the age field
   ageEditField.setText(String.valueOf(NoOfDays) + "D");
}

 

Once again thank you for your quick response.

Developer
Posts: 19,616
Registered: ‎07-14-2008
My Device: Not Specified

Re: Finding Age using two dates...

[ Edited ]

Removed - redundant

Contributor
Posts: 41
Registered: ‎07-19-2011
My Device: 9500
My Carrier: airtel

Re: Finding Age using two dates...

public class Age {
	static int[] monthMaxDates = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
			31 };
	static int[] monthMaxDates_l = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31,
			30, 31 };

	
	public static String calculateAgeUptoToday(long dob) {
		Calendar birthDayCalendar = Calendar.getInstance();
		birthDayCalendar.setTime(new Date(dob));
		Calendar currentDayCalendar = Calendar.getInstance();
		currentDayCalendar.setTime(new Date(System.currentTimeMillis()));
		int year = currentDayCalendar.get(Calendar.YEAR)
				- birthDayCalendar.get(Calendar.YEAR);
		int month = currentDayCalendar.get(Calendar.MONTH)
				- birthDayCalendar.get(Calendar.MONTH);
		int days = currentDayCalendar.get(Calendar.DAY_OF_MONTH)
				- birthDayCalendar.get(Calendar.DAY_OF_MONTH);
		if (days < 0) {
			month -= 1;
			if ((currentDayCalendar.get(Calendar.YEAR) / 4) == 0) {
				days = monthMaxDates_l[currentDayCalendar.get(Calendar.MONTH)]+days;
			} else {
				days = monthMaxDates[currentDayCalendar.get(Calendar.MONTH)]+days;
			}

		}
		if (month < 0) {
			year -= 1;
			month = 12 + month;
		}

		return "" + year + " years " + month + " months "+days+" days";
	}
}