yyyymmdd format으로부터 한국나이 계산
public static int calculateAgeForKorean(String ssn) { // ssn의 형식은 yyyymmdd 임
String today = ""; // 오늘 날짜
int manAge = 0; // 만 나이
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
today = formatter.format(new Date()); // 시스템 날짜를 가져와서 yyyyMMdd 형태로 변환
// today yyyyMMdd
int todayYear = Integer.parseInt(today.substring(0, 4));
int todayMonth = Integer.parseInt(today.substring(4, 6));
int todayDay = Integer.parseInt(today.substring(6, 8));
int ssnYear = Integer.parseInt(ssn.substring(0, 4));
int ssnMonth = Integer.parseInt(ssn.substring(4, 6));
int ssnDay = Integer.parseInt(ssn.substring(6, 8));
manAge = todayYear - ssnYear;
if (todayMonth < ssnMonth) { // 생년월일 "월"이 지났는지 체크
manAge--;
} else if (todayMonth == ssnMonth) { // 생년월일 "일"이 지났는지 체크
if (todayDay < ssnDay) {
manAge--; // 생일 안지났으면 (만나이 - 1)
}
}
return manAge + 1; // 한국나이를 측정하기 위해서 +1살 (+1을 하지 않으면 외국나이 적용됨)
}
'안드로이드 개발 > 개발팁' 카테고리의 다른 글
안드로이드 알림창 띄우기 (0) | 2015.02.25 |
---|---|
Android의 Context ( getBaseContext()와 getApplicationContext() 차이) (0) | 2015.02.24 |
JSON String을 JSONObject로 변환하기 (0) | 2015.02.24 |
쌓여있는 액티비티 모두 지우기 (0) | 2015.01.20 |
폴더 생성및 삭제 (0) | 2015.01.12 |