Here you can find the source of toAge(String birthDay)
public static String toAge(String birthDay)
//package com.java2s; import java.sql.Date; import java.sql.Time; import java.text.*; import java.util.Calendar; public class Main { public static final String DEFAULT_DATE_YMD_FORMAT = "yyyy-MM-dd"; public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss"; public static String toAge(String birthDay) { if (birthDay == null || birthDay.length() != 8) { return null; }/* w w w . j a v a 2 s . c om*/ int birthYear = Integer.parseInt(birthDay.substring(0, 4)); int birthMonth = Integer.parseInt(birthDay.substring(4, 6)); int birthDayOfMonth = Integer.parseInt(birthDay.substring(6, 8)); return toAge(birthYear, birthMonth, birthDayOfMonth); } public static String toAge(int birthYear, int birthMonth, int birthDayOfMonth) { Calendar cl = Calendar.getInstance(); int year = cl.get(Calendar.YEAR); int month = cl.get(Calendar.MONTH) + 1; int day = cl.get(Calendar.DAY_OF_MONTH); int sa = 0; if (month > birthMonth) { sa = 0; } else if (month == birthMonth && day >= birthDayOfMonth) { sa = 0; } else { sa = 1; } int age = year - birthYear - sa; return Integer.toString(age); } public static String toString(Date date) { return toString((java.util.Date) date); } public static String toString(java.util.Date date) { return toString(date, DEFAULT_DATE_YMD_FORMAT); } public static String toString(Date date, String format) { return toString((java.util.Date) date, format); } public static String toString(java.util.Date date, String format) { if (date == null) { return null; } if (format == null) { throw new IllegalArgumentException("The value of an argument is inaccurate."); } SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } public static String toString(Time time, String format) { if (time == null) { return null; } if (format == null) { throw new IllegalArgumentException("The value of an argument is inaccurate."); } SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(time); } public static String toString(Time time) { return toString(time, DEFAULT_TIME_FORMAT); } }