Skip to content

Instantly share code, notes, and snippets.

@TheFinestArtist
Last active August 29, 2015 14:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheFinestArtist/83d616afb5ef540c5ef3 to your computer and use it in GitHub Desktop.
Save TheFinestArtist/83d616afb5ef540c5ef3 to your computer and use it in GitHub Desktop.
AgeHelper.java
import android.text.format.DateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Created by TheFinestArtist
*/
public class AgeHelper {
public static String getFromBirthDay(Date birthDay) {
if (birthDay == null)
return "?";
int year = Integer.parseInt((String) DateFormat.format("yyyy", birthDay));
int month = Integer.parseInt((String) DateFormat.format("MM", birthDay));
int day = Integer.parseInt((String) DateFormat.format("dd", birthDay));
Calendar dob = Calendar.getInstance();
Calendar today = Calendar.getInstance();
dob.set(year, month - 1, day);
int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {
age--;
}
return String.format("%d", age);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment