一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - JAVA 根據身份證計算年齡的實現代碼

JAVA 根據身份證計算年齡的實現代碼

2021-04-28 11:06HrlSnow Java教程

這篇文章主要介紹了JAVA 根據身份證計算年齡的實例代碼及java根據出生日期獲得年齡的方法,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧

下面一段代碼給大家分享java根據身份證計算年齡的方法,具體代碼如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
birthdate = idcard.substring(6,10)+"-"+idcard.substring(10,12)+"-"+idcard.substring(12,14)
public static int getagefrombirthtime(string birthtimestring){
 // 先截取到字符串中的年、月、日
 string strs[] = birthtimestring.trim().split("-");
 int selectyear = integer.parseint(strs[0]);
 int selectmonth = integer.parseint(strs[1]);
 int selectday = integer.parseint(strs[2]);
 // 得到當前時間的年、月、日
 calendar cal = calendar.getinstance();
 int yearnow = cal.get(calendar.year);
 int monthnow = cal.get(calendar.month) + 1;
 int daynow = cal.get(calendar.date);
 // 用當前年月日減去生日年月日
 int yearminus = yearnow - selectyear;
 int monthminus = monthnow - selectmonth;
 int dayminus = daynow - selectday;
 int age = yearminus;
 if (yearminus < 0) {// 選了未來的年份
  age = 0;
 } else if (yearminus == 0) {// 同年的,要么為1,要么為0
  if (monthminus < 0) {// 選了未來的月份
   age = 0;
  } else if (monthminus == 0) {// 同月份的
   if (dayminus < 0) {// 選了未來的日期
    age = 0;
   } else if (dayminus >= 0) {
    age = 1;
   }
  } else if (monthminus > 0) {
   age = 1;
  }
 } else if (yearminus > 0) {
  if (monthminus < 0) {// 當前月>生日月
  } else if (monthminus == 0) {// 同月份的,再根據日期計算年齡
   if (dayminus < 0) {
   } else if (dayminus >= 0) {
    age = age + 1;
   }
  } else if (monthminus > 0) {
   age = age + 1;
  }
 }
 return age;
}

下面在看下java根據出生日期獲得年齡

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static int getage(date birthday) throws exception {
  calendar cal = calendar.getinstance();
  if (cal.before(birthday)) {
   throw new illegalargumentexception(
    "the birthday is before now.it's unbelievable!");
  }
  int yearnow = cal.get(calendar.year);
  int monthnow = cal.get(calendar.month);
  int dayofmonthnow = cal.get(calendar.day_of_month);
  cal.settime(birthday);
  int yearbirth = cal.get(calendar.year);
  int monthbirth = cal.get(calendar.month);
  int dayofmonthbirth = cal.get(calendar.day_of_month);
  int age = yearnow - yearbirth;
  if (monthnow <= monthbirth) {
   if (monthnow == monthbirth) {
    if (dayofmonthnow < dayofmonthbirth) age--;
   }else{
    age--;
   }
  }
  system.out.println("age:"+age);
  return age;
 }

總結

以上所述是小編給大家介紹的java 根據身份證計算年齡,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://blog.csdn.net/HrlSnow/article/details/80266906

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费毛片大全 | 波多野结衣xxxx性精品 | 亚洲sss综合天堂久久久 | 女攻双性 | 第一次破学生处破 | 校园全黄h全肉细节文 | 美国雪白人妖sarina | 美女林柏欣21p人体之仓之梦 | 亚洲精品资源在线 | 日本妻子迷妹网 | 天天综合网网欲色 | 欧美日韩不卡视频 | 国产精品视频视频久久 | 99精品热 | 关晓彤一级做a爰片性色毛片 | 男女拍拍拍免费视频网站 | 国产精品二区高清在线 | 色婷婷影院在线视频免费播放 | 星空无限传媒xk8129 | 99久久中文字幕伊人 | 99热这里只有精品在线 | 日本三级在线观看免费 | 2020精品极品国产色在线观看 | 色中色软件 | 草草影院国产 | 草草影院永久在线观看 | 日本高清色视影www日本 | 91精品国产91热久久久久福利 | 男女视频在线观看网站 | 天天爽天天干天天操 | 国产欧美一区二区三区久久 | 大香焦在线观看 | 日本啊v在线观看 | 毛片啪啪视频 | 亚洲精选在线观看 | 日本手机在线 | 果冻传媒九一制片厂网站 | 久久91精品国产91久久户 | 国产香蕉国产精品偷在线观看 | 国产精品制服丝袜白丝www | 亚洲人成绝费网站色ww |