Java Age Calculate calculateAgeInWeek(final Date birthDate, final Date asOfDate)

Here you can find the source of calculateAgeInWeek(final Date birthDate, final Date asOfDate)

Description

Calculate age based on the birth date and the reference date.

License

Open Source License

Parameter

Parameter Description
birthDate the birth date
asOfDate the reference date to do the age calculation

Return

age in week

Declaration

private static int calculateAgeInWeek(final Date birthDate,
        final Date asOfDate) 

Method Source Code

//package com.java2s;
/**// ww  w  .  j  av a 2s  .co  m
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */

import java.util.Calendar;
import java.util.Date;

public class Main {
    public static final Integer ONE_WEEK = 60 * 60 * 24 * 7;

    /**
     * Calculate age based on the birth date and the reference date. The returned age is in week. The remaining time will be rounded to the closest one
     * week value.
     *
     * @param birthDate the birth date
     * @param asOfDate  the reference date to do the age calculation
     * @return age in week
     */
    private static int calculateAgeInWeek(final Date birthDate,
            final Date asOfDate) {

        Calendar birthCalendar = Calendar.getInstance();
        birthCalendar.setTime(birthDate);

        Calendar todayCalendar = Calendar.getInstance();
        todayCalendar.setTime(asOfDate);

        long timeDifference = (todayCalendar.getTimeInMillis() - birthCalendar
                .getTimeInMillis()) / 1000;

        int ageInWeek = (int) (timeDifference / ONE_WEEK);
        if (timeDifference % ONE_WEEK > ONE_WEEK / 2)
            ageInWeek++;

        return ageInWeek;
    }
}

Related

  1. calculateAge(Date birthday)
  2. calculateAge(Date dateOfBirth)
  3. calculateAge(Date dob)
  4. calculateAge(Date DOB)
  5. calculateAgeInMonth(final Date birthDate, final Date asOfDate)
  6. getAge(Date birthDate, Date controlDate)
  7. getAge(Date birthDay)
  8. getAge(Date birthday)
  9. getAge(Date birthday)