通过身份证获取:性别、年龄、星座、生肖
生活随笔
收集整理的這篇文章主要介紹了
通过身份证获取:性别、年龄、星座、生肖
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
性別
/*** 根據身份證號判斷性別 奇數代表男 偶數代表女* @param idNumber* @return*/ public static String gender(String idNumber) {int gender = 0;if(idNumber.length() == 18){//如果身份證號18位,取身份證號倒數第二位char c = idNumber.charAt(idNumber.length() - 2);gender = Integer.parseInt(String.valueOf(c));}else{//如果身份證號15位,取身份證號最后一位char c = idNumber.charAt(idNumber.length() - 1);gender = Integer.parseInt(String.valueOf(c));}if(gender % 2 == 1){return "男";return "1";}else{return "女";return "2";} }年齡
/*** 根據身份證的號碼算出當前身份證持有者的年齡* @return*/ public static int countAge(String idNumber) {if(idNumber.length() != 18 && idNumber.length() != 15){throw new IllegalArgumentException("身份證號長度錯誤");}String year;String yue;String day;if(idNumber.length() == 18){year = idNumber.substring(6).substring(0, 4);// 得到年份yue = idNumber.substring(10).substring(0, 2);// 得到月份day = idNumber.substring(12).substring(0,2);//得到日}else{year = "19" + idNumber.substring(6, 8);// 年份yue = idNumber.substring(8, 10);// 月份day = idNumber.substring(10, 12);//日}Date date = new Date();// 得到當前的系統時間SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");String fyear = format.format(date).substring(0, 4);// 當前年份String fyue = format.format(date).substring(5, 7);// 月份String fday=format.format(date).substring(8,10);//int age = 0;if(Integer.parseInt(yue) == Integer.parseInt(fyue)){//如果月份相同if(Integer.parseInt(day) <= Integer.parseInt(fday)){//說明已經過了生日或者今天是生日age = Integer.parseInt(fyear) - Integer.parseInt(year);}}else{if(Integer.parseInt(yue) < Integer.parseInt(fyue)){//如果當前月份大于出生月份age = Integer.parseInt(fyear) - Integer.parseInt(year);}else{//如果當前月份小于出生月份,說明生日還沒過age = Integer.parseInt(fyear) - Integer.parseInt(year) - 1;}}System.out.println("您今年的年齡為 = " + age);return age; }星座
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;public class BirthUtils {public static final String[] CONSTELLATION_ARR = { "水瓶座", "雙魚座", "白羊座", "金牛座", "雙子座", "巨蟹座", "獅子座", "處女座", "天秤座", "天蝎座", "射手座", "魔羯座" };public static final int[] CONSTELLATION_EDGE_DAY = { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22 };/*** 根據日期獲取星座* @return*/public static String getConstellation(Date date) {if (date == null) {return "";}Calendar cal = Calendar.getInstance();cal.setTime(date);int month = cal.get(Calendar.MONTH);int day = cal.get(Calendar.DAY_OF_MONTH);if (day < CONSTELLATION_EDGE_DAY[month]) {month = month - 1;}if (month >= 0) {return CONSTELLATION_ARR[month];}// default to return 魔羯return CONSTELLATION_ARR[11];}/*** 根據身份證號判斷用戶星座* @param cardNo* @return*/public static String getConstellation(String cardNo) {// 獲取出生日期String birthday = cardNo.substring(6, 14);Date birthdate = null;try {birthdate = new SimpleDateFormat("yyyyMMdd").parse(birthday);return getConstellation(birthdate);} catch (ParseException e) {e.printStackTrace();}return null;}public static void main(String[] args) {// 1989-03-11 雙魚座System.out.println(getConstellation("530121198903119561"));} }生肖
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;public class BirthUtils {public static final String[] ZODIAC_ARR = { "猴", "雞", "狗", "豬", "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊" };/*** 根據日期獲取生肖* @return*/public static String getZodica(Date date) {Calendar cal = Calendar.getInstance();cal.setTime(date);return ZODIAC_ARR[cal.get(Calendar.YEAR) % 12];}/*** 根據身份證號判斷用戶生肖* @param cardNo* @return*/public static String getZodica(String cardNo) {// 獲取出生日期String birthday = cardNo.substring(6, 14);Date birthdate = null;try {birthdate = new SimpleDateFormat("yyyyMMdd").parse(birthday);return getZodica(birthdate);} catch (ParseException e) {e.printStackTrace();}return null;}public static void main(String[] args) {// 1989 蛇System.out.println(getZodica("530121198903119561"));} }總結
以上是生活随笔為你收集整理的通过身份证获取:性别、年龄、星座、生肖的全部內容,希望文章能夠幫你解決所遇到的問題。