先从String类型的出生日期(“yyyy-MM-dd”)中提取int类型的年、月、日;再计算岁数。

程序如下:

 /**
* 根据出生日期计算年龄的工具类BirthdayToAgeUtil
*/
public class BirthdayToAgeUtil { private static String birthday;
private static String ageStr;
private static int age;
//出生年、月、日
private static int year;
private static int month;
private static int day;
public static String BirthdayToAge(String birthday1) {
birthday = birthday1;
stringToInt(birthday, "yyyy-MM-dd");
// 得到当前时间的年、月、日
Calendar cal = Calendar.getInstance();
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH) + 1;
int dayNow = cal.get(Calendar.DATE);
KLog.d("yearNow:" + yearNow);
KLog.d("monthNow:" + monthNow);
KLog.d("dayNow:" + dayNow);
// 用当前年月日减去出生年月日
int yearMinus = yearNow - year;
int monthMinus = monthNow - month;
int dayMinus = dayNow - day;
age = yearMinus;// 先大致赋值
if (yearMinus <= 0) {
age = 0;
ageStr = String.valueOf(age) + "周岁啦!";
return ageStr;
}
if (monthMinus < 0) {
age = age - 1;
} else if (monthMinus == 0) {
if (dayMinus < 0) {
age = age - 1;
}
}
ageStr = String.valueOf(age) + "周岁啦!";
return ageStr;
} /**
* String类型转换成date类型
* strTime: 要转换的string类型的时间,
* formatType: 要转换的格式yyyy-MM-dd HH:mm:ss
* //yyyy年MM月dd日 HH时mm分ss秒,
* strTime的时间格式必须要与formatType的时间格式相同
*/
private static Date stringToDate(String strTime, String formatType) {
KLog.d("进入stringToDate");
try {
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
Date date;
date = formatter.parse(strTime);
return date;
} catch (Exception e) {
return null;
}
} /**
* String类型转换为long类型
* .............................
* strTime为要转换的String类型时间
* formatType时间格式
* formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
* strTime的时间格式和formatType的时间格式必须相同
*/
private static void stringToInt(String strTime, String formatType) {
KLog.d("进入stringToLong");
try {
//String类型转换为date类型
Calendar calendar = Calendar.getInstance();
Date date = stringToDate(strTime, formatType);
calendar.setTime(date);
KLog.d("调用stringToDate()获得的date:" + date);
if (date == null) {
} else {
//date类型转成long类型
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
day = calendar.get(Calendar.DAY_OF_MONTH);
}
} catch (Exception e) {
KLog.d("Exception:" + e);
}
}
}

Android由出生年月日计算年龄(周岁)的更多相关文章

  1. js根据出生年月日换算年龄

    function age_Conversion(date) { debugger var age = ''; var str = date.replace(/年|月/g, "-") ...

  2. Java 根据年月日精确计算年龄

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created b ...

  3. 通过身份证分析出生年月日、性别、年龄的SQL语句

    ),) ) ),)<>'X' ) ) )<>'X' ),)),)),)) ),)),)),)) ) as int)) where [出生日期]<>'' #字符串格式 ...

  4. Excel中最精确的计算年龄的公式

    身份证算年龄 假设A1是身份证号所在单元格 =IF(MONTH(NOW())<INT(MID(A1,11,2)),INT(YEAR(NOW())-INT(MID(A1,7,4)))-1,IF(M ...

  5. php根据身份证号码计算年龄

    代码如下 复制代码 <?php function getAgeByID($id){         //过了这年的生日才算多了1周岁         if(empty($id)) return ...

  6. Java 计算年龄

    public static String getAgeTxt(String birthTime,String beginTime,int level){ if(StringUtils.isBlank( ...

  7. 在Excel中根据某一个单元格的出生日期自动精确计算年龄

    =IF(MONTH(NOW())<MONTH(G4),INT(YEAR(NOW())-YEAR(G4))-1,IF(MONTH(NOW())>MONTH(G4),YEAR(NOW())-Y ...

  8. java身份证计算年龄

    技术交流群: 233513714 /** * 根据身份证计算年龄 * * @param idcard * @return */ public static Integer idCardToAge(St ...

  9. java 根据生日计算年龄 Java问题通用解决代码

    根据生日计算年龄可以通过Calendar实现.最简单可以考虑get(Calendar.DAY_OF_YEAR)来简单修正年龄,但是遇到生日在闰年的2月29之后,或者今年是闰年的2月29之后可能出现计算 ...

随机推荐

  1. 在springboot中集成mybatis开发

    在springboot中利用mybatis框架进行开发需要集成mybatis才能进行开发,那么如何在springboot中集成mybatis呢?按照以下几个步骤就可以实现springboot集成myb ...

  2. CSS3 @keyframes 实现匀速旋转魔方(搬运工)

    原博文 * https://www.cnblogs.com/shihao905/p/6208324.html * html <div id="wrap"> <di ...

  3. softmax+交叉熵

    1 softmax函数 softmax函数的定义为 $$softmax(x)=\frac{e^{x_i}}{\sum_j e^{x_j}} \tag{1}$$ softmax函数的特点有 函数值在[0 ...

  4. java数据结构03

    1.求二叉树的深度 https://www.cnblogs.com/xudong-bupt/p/4036190.html class TreeNode { char val; TreeNode lef ...

  5. Binary Numbers AND Sum CodeForces - 1066E (前缀和)

    You are given two huge binary integer numbers aa and bb of lengths nn and mmrespectively. You will r ...

  6. 校内题目T2695 桶哥的问题——吃桶

    同T2一样外校蒟蒻可能没看过: 题目描述: 题目背景 @桶哥 桶哥的桶没有送完. 题目描述 桶哥的桶没有送完,他还有n个桶.他决定把这些桶吃掉.他的每一个桶两个属性:种类aia_iai​和美味值bib ...

  7. ng-model 和ng-bind的区别

    也就是说 ng-model是绑定html输入的值-->到控制器的变量,输入值变了,控制器对应的变量message的值页变了,这样,在其他地方就可以使用这个变化后的值 ng-bind 是绑定控制器 ...

  8. canvas实现圆角图片 (处理原图是长方形或正方形)

    /** * 生成图片的缩略图 * @param {[type]} img 图片(img)对象或地址 * @param {[type]} width 缩略图宽 * @param {[type]} hei ...

  9. HDU-3415-Max Sum of Max-K-sub-sequence(单调队列,带限制的最大子段和)

    链接: https://vjudge.net/problem/HDU-3415 题意: Given a circle sequence A[1],A[2],A[3]......A[n]. Circle ...

  10. shiro框架学习-9-shiroSession

    1.什么是会话session : 用户和程序直接的链接,程序可以根据session识别到哪个用户,和javaweb中的session类似 2. 什么是会话管理器SessionManager : 会话管 ...