Get age from a birthday date
import java.util.Date;
public class Main {
public static final long MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
public static final long MILLIS_PER_YEAR = 365 * MILLIS_PER_DAY;
public static int getAge(Date birthDate) {
if (birthDate == null)
return 0;
long ageMillis = System.currentTimeMillis() - birthDate.getTime();
return (int) (ageMillis / MILLIS_PER_YEAR);
}
}
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateHelper {
public static float getAge(final Date birthdate) {
return getAge(Calendar.getInstance().getTime(), birthdate);
}
public static float getAge(final Date current, final Date birthdate) {
if (birthdate == null) {
return 0;
}
if (current == null) {
return getAge(birthdate);
} else {
final Calendar calend = new GregorianCalendar();
calend.set(Calendar.HOUR_OF_DAY, 0);
calend.set(Calendar.MINUTE, 0);
calend.set(Calendar.SECOND, 0);
calend.set(Calendar.MILLISECOND, 0);
calend.setTimeInMillis(current.getTime() - birthdate.getTime());
float result = 0;
result = calend.get(Calendar.YEAR) - 1970;
result += (float) calend.get(Calendar.MONTH) / (float) 12;
return result;
}
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Date Get:
- Day of Week
- Number of days in a month
- Month of year
- Current month name
- Full date time
- Get all attributes from a Calendar
- Get year, month, day, minute, hour, second, milli second from java.util.Date through Calendar
- Get age from a birthday date
- Get Monday
- Get next Sunday
- Get today's date
- Get the last day of a month
- Get date of yesterday
- Get Day-of-Week for a Particular Date
- Get date of last week
- Get date of last month
- Get last Date of This Month
- Get Month in a leap year
- Get start of a month
- Get the end of a month
- Get noon of a day
- Get days since a date
- Get TimeZone
- Get Last day fo previous Month
- Is an hour between an interval
- Is a Year a Leap Year