Java
package com.qyj.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class IdCardUtil {
/**
* 通过身份证号获取性别
* 1男 2女 0未知
*
* @param idCard
* @return
*/
public static Integer getSexByIdCard(String idCard) {
if(!StringUtils.isEmpty(idCard)
&& idCard.length() >= 17){
String sexStr = idCard.substring(16, 17);
int sexInt = Integer.parseInt(sexStr);
if(sexInt%2 == 0){//1男 2女 0未知
return 2;//女
}else{
return 1;//男
}
}
return null;
}
/**
* 通过身份证号获取出生年月
*
* @param idCard
* @return
*/
public static Date getBirthdayByIdCard(String idCard) {
if(!StringUtils.isEmpty(idCard)
&& idCard.length() >= 14){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String bornStr = idCard.substring(6, 10) +
'-' + idCard.substring(10, 12) +
'-' + idCard.substring(12, 14)
;
Date birthday = null;
try {
birthday = df.parse(bornStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return birthday;
}
return null;
}
/**
* 根据出生日期计算年龄
*
* @param birthDay
* @param nowDate
* @return
*/
public static int getAgeByBirthDay(Date birthDay, Date nowDate) {
if(birthDay == null){
return -1;
}
//兼容不传比较时间
if(nowDate == null){
nowDate = new Date();
}
//开始
Calendar cal = Calendar.getInstance();//当前日期
Calendar cal2 = Calendar.getInstance();//出生日期
//当前日期(报名日期)
cal.setTime(nowDate);
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH);
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
//出生日期
cal2.setTime(birthDay);
int yearBirth = cal2.get(Calendar.YEAR);
int monthBirth = cal2.get(Calendar.MONTH);
int dayOfMonthBirth = cal2.get(Calendar.DAY_OF_MONTH);
//当前日期 < 出生日期
if (cal.before(birthDay)) {
return -1;
}
//年龄
int age = yearNow - yearBirth;
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
if (dayOfMonthNow < dayOfMonthBirth){
age--;
}
} else {
age--;
}
}
//返回
return age;
}
}
//通过身份证号获取性别
if(studentIdCard.length() >= 17){
String sexStr = studentIdCard.substring(16, 17);
int sexInt = Integer.parseInt(sexStr);
if(sexInt%2 == 0){//女
studentGender = 2;
}else{//男
studentGender = 1;
}
}
JS
//自动判定性别
if(idCardText.length >= 17){
var sexStr = idCardText.substr(16, 1);
var sexInt = parseInt(sexStr);
if(sexInt%2 == 0){//女
view.get("#employeeDS").getData("#").set('sex', '2');
}else{//男
view.get("#employeeDS").getData("#").set('sex', '1');
}
}
if(idCardText.length >= 14){
//自动产生出生日期
var bornStr = idCardText.substr(6, 4) + '-' + idCardText.substr(10, 2) + '-' + idCardText.substr(12, 2);
employeeDS.getData("#").set('birthday', bornStr);
}
使用实例:
idCardText.onTextEdit
idCardText.onPost(貌似不行)
/**
* like12 add,20141119,输入身份证号码,自动产生出生日期
* TextEditor内容被修改
*/
//@Bind #idCardText.onTextEdit
!function(self, arg){
var idCardText = view.get("#idCardText.value");
if(idCardText.length >= 14){
var bornStr = idCardText.substr(6, 4) + '-' + idCardText.substr(10, 2)
+ '-' + idCardText.substr(12, 2);
view.get("#employeeDataSet").getData("#").set('employeeBirthday', bornStr);
}
//like12 add,20170209,自动判定性别
if(idCardText.length >= 17){
var sexStr = idCardText.substr(16, 1);
var sexInt = parseInt(sexStr);
if(sexInt%2 == 0){//女
view.get("#employeeDataSet").getData("#").set('employeeSex', '0');
}else{//男
view.get("#employeeDataSet").getData("#").set('employeeSex', '1');
}
}
};
/**
* 身份证号验证
*/
//@Bind #idCardText.onPost
!function(self, arg){
var idCardText = view.get("#idCardText.value");
var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
if(reg.test(idCardText) === false){
dorado.MessageBox.alert("身份证输入不合法");
}
};
方法一:
//like12 add,20170921,计算年龄,不太准确 有数天的误差(估计是闰年原因)
var birthDay = view.get("#employeeDS").getData().get('birthday');
var nowDate = new Date();
var diff = nowDate.getTime() - birthDay.getTime();
// 向下取整 例如 10岁 20天 会计算成 10岁
// 如果要向上取整 计算成11岁,把floor替换成 ceil
var age = Math.floor(diff / 1000 / 60 / 60 / 24 / 365);
方法二:
//like12 add,20170921,计算年龄
var birthDay = view.get("#employeeDS").getData().get('birthday');
var age = jsGetAge(birthDay.format("yyyy-MM-dd"));
view.get("#employeeDS").getData().set('age', age);
//根据出生日期计算年龄
function jsGetAge(strBirthday) {
var returnAge;
var strBirthdayArr = strBirthday.split("-");
var birthYear = strBirthdayArr[0];
var birthMonth = strBirthdayArr[1];
var birthDay = strBirthdayArr[2];
var d = new Date();
var nowYear = d.getFullYear();
var nowMonth = d.getMonth() + 1;
var nowDay = d.getDate();
if (nowYear == birthYear) {
returnAge = 0;//同年 则为0岁
} else {
var ageDiff = nowYear - birthYear; //年之差
if (ageDiff > 0) {
if (nowMonth == birthMonth) {
var dayDiff = nowDay - birthDay;//日之差
if (dayDiff < 0) {
returnAge = ageDiff - 1;
} else {
returnAge = ageDiff;
}
} else {
var monthDiff = nowMonth - birthMonth;//月之差
if (monthDiff < 0) {
returnAge = ageDiff - 1;
} else {
returnAge = ageDiff;
}
}
} else {
returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
}
}
return returnAge;//返回周岁年龄
}