Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Calendar;

public class Main {
    public static int getAge(String year, String month, String day) throws NumberFormatException {
        int yearInt = Integer.parseInt(year);
        int monthInt = Integer.parseInt(month);
        int dayInt = Integer.parseInt(day);
        monthInt--;
        Calendar birthCal = Calendar.getInstance();
        birthCal.set(yearInt, monthInt, dayInt);
        Calendar todayCal = Calendar.getInstance();

        int diff = todayCal.get(Calendar.YEAR) - birthCal.get(Calendar.YEAR);
        if (birthCal.get(Calendar.MONTH) > todayCal.get(Calendar.MONTH)
                || (birthCal.get(Calendar.MONTH) == todayCal.get(Calendar.MONTH)
                        && birthCal.get(Calendar.DATE) > todayCal.get(Calendar.DATE))) {
            diff--;
        }
        return diff;
    }
}