Get international age from birthday - Android java.util

Android examples for java.util:Day

Description

Get international age from birthday

Demo Code


//package com.java2s;

import java.util.Calendar;

public class Main {
    /**//from ww w  . java 2  s . c om
     * Get international age from birthday
     *
     * @param birth_year
     * @param birth_month Janunary is '1' although January is '0' at Calendar class
     * @param birth_day
     *
     * @return A new instance of fragment NewEventFragment.
     */
    public static int getInternationalAge(int birth_year, int birth_month,
            int birth_day) {
        Calendar cal_birth = Calendar.getInstance();
        cal_birth.set(Calendar.YEAR, birth_year);
        cal_birth.set(Calendar.MONTH, birth_month - 1);
        cal_birth.set(Calendar.DATE, birth_day);

        return getInternationalAge(cal_birth, Calendar.getInstance());
    }

    public static int getInternationalAge(long time_milis) {
        return getInternationalAge(time_milis, System.currentTimeMillis());
    }

    public static int getInternationalAge(long time_milis, long now_milis) {
        Calendar cal_birth = Calendar.getInstance();
        cal_birth.setTimeInMillis(time_milis);

        Calendar cal_now = Calendar.getInstance();
        cal_now.setTimeInMillis(now_milis);

        return getInternationalAge(cal_birth, cal_now);
    }

    public static int getInternationalAge(Calendar cal_birth,
            Calendar cal_now) {
        if (cal_now == null) {
            cal_now = Calendar.getInstance();
        }

        int age = cal_now.get(Calendar.YEAR) - cal_birth.get(Calendar.YEAR);
        if ((cal_birth.get(Calendar.MONTH) > cal_now.get(Calendar.MONTH))
                || (cal_birth.get(Calendar.MONTH) == cal_now
                        .get(Calendar.MONTH) && cal_birth
                        .get(Calendar.DAY_OF_MONTH) > cal_now
                        .get(Calendar.DAY_OF_MONTH))) {
            age--;
        }

        return age;
    }
}

Related Tutorials