Here you can find the source of calculateAgeInWeek(final Date birthDate, final Date asOfDate)
Parameter | Description |
---|---|
birthDate | the birth date |
asOfDate | the reference date to do the age calculation |
private static int calculateAgeInWeek(final Date birthDate, final Date asOfDate)
//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; } }