Java 주민번호로 생년월일, 성별 추출

박현우·2024년 1월 5일

package string;

public class Main03 {

public static void main(String[] args) {
	// 주민등록번호에서 생년월일 추출하기
	// ex) 800501234567 -> 1980년 05월 01일 남자
	// 1 or 2 -> 19~
	// 3 or 4 -> 20~
	// 1 or 3 -> 남자
	// 2 or 4 -> 여자
	
	String jumin = "800501124567";
	
	// 주민번호를 년, 월, 일 단위로 각 두 글자씩 자르기
	String year = jumin.substring(0, 2);
	String month = jumin.substring(2, 4);
	String day = jumin.substring(4, 6);
	
	// 뒷 부분의 첫번째 글자는 성별코드이므로 별도로 추출한다.
	String gender_code = jumin.substring(6, 7);
	if (gender_code.equals("1")|| gender_code.equals("2")) {
		year = "19" + year;
	} else {
		year = "20" + year;
				
	}
	
	// 기본 성별을 남자
	String gender = "남자";
	if(gender_code.equals("2")|| gender_code.equals("4")) {
		gender = "여자";
	}
	
	// 형식에 맞춘 내용 출력
	// (String.format + System.out.println)
	System.out.printf("%s년 %s월 %s일 %s", year, month, day, gender );
	
	
	
}

}

0개의 댓글