Last active
August 29, 2015 14:25
-
-
Save TheFinestArtist/83d616afb5ef540c5ef3 to your computer and use it in GitHub Desktop.
AgeHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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