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

 1 import java.text.SimpleDateFormat;
 2 import java.util.Calendar;
 3 import java.util.Date;
 4
 5 /**
 6  * Created by qing on 2017/3/28.
 7  */
 8 public class AgeUtils {
 9     // 根据年月日计算年龄,birthTimeString:"1994-11-14"
10     public static int getAgeFromBirthTime(String birthTimeString) {
11         // 先截取到字符串中的年、月、日
12         String strs[] = birthTimeString.trim().split("-");
13         int selectYear = Integer.parseInt(strs[0]);
14         int selectMonth = Integer.parseInt(strs[1]);
15         int selectDay = Integer.parseInt(strs[2]);
16         // 得到当前时间的年、月、日
17         Calendar cal = Calendar.getInstance();
18         int yearNow = cal.get(Calendar.YEAR);
19         int monthNow = cal.get(Calendar.MONTH) + 1;
20         int dayNow = cal.get(Calendar.DATE);
21
22         // 用当前年月日减去生日年月日
23         int yearMinus = yearNow - selectYear;
24         int monthMinus = monthNow - selectMonth;
25         int dayMinus = dayNow - selectDay;
26
27         int age = yearMinus;// 先大致赋值
28         if (yearMinus < 0) {// 选了未来的年份
29             age = 0;
30         } else if (yearMinus == 0) {// 同年的,要么为1,要么为0
31             if (monthMinus < 0) {// 选了未来的月份
32                 age = 0;
33             } else if (monthMinus == 0) {// 同月份的
34                 if (dayMinus < 0) {// 选了未来的日期
35                     age = 0;
36                 } else if (dayMinus >= 0) {
37                     age = 1;
38                 }
39             } else if (monthMinus > 0) {
40                 age = 1;
41             }
42         } else if (yearMinus > 0) {
43             if (monthMinus < 0) {// 当前月>生日月
44             } else if (monthMinus == 0) {// 同月份的,再根据日期计算年龄
45                 if (dayMinus < 0) {
46                 } else if (dayMinus >= 0) {
47                     age = age + 1;
48                 }
49             } else if (monthMinus > 0) {
50                 age = age + 1;
51             }
52         }
53         return age;
54     }
55
56     // 根据时间戳计算年龄
57     public static int getAgeFromBirthTime(long birthTimeLong) {
58         Date date = new Date(birthTimeLong * 1000l);
59         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
60         String birthTimeString = format.format(date);
61         return getAgeFromBirthTime(birthTimeString);
62     }
63 }  

精确到月:

 1 // 根据年月日计算年龄,birthTimeString:"1994-11-14"
 2     public static String getAgeFromBirthTime(String birthTimeString) {
 3         // 先截取到字符串中的年、月、日
 4         String strs[] = birthTimeString.trim().split("-");
 5         int selectYear = Integer.parseInt(strs[0]);
 6         int selectMonth = Integer.parseInt(strs[1]);
 7         int selectDay = Integer.parseInt(strs[2]);
 8         // 得到当前时间的年、月、日
 9         Calendar cal = Calendar.getInstance();
10         int yearNow = cal.get(Calendar.YEAR);
11         int monthNow = cal.get(Calendar.MONTH) + 1;
12         int dayNow = cal.get(Calendar.DATE);
13
14         // 用当前年月日减去生日年月日
15         int yearMinus = yearNow - selectYear;
16         int monthMinus = monthNow - selectMonth;
17         int dayMinus = dayNow - selectDay;
18         String  ageToMonth= "1月";
19
20         int age = yearMinus;// 先大致赋值
21         if (yearMinus < 0) {// 选了未来的年份
22             age = 0;
23             ageToMonth = "1月";
24         } else if (monthMinus > 0) {
25             age = 1;
26             ageToMonth = String.valueOf(monthMinus)+"月";
27         } else if (yearMinus > 0) {
28             if (monthMinus < 0) {// 当前月>生日月
29             } else if (monthMinus == 0) {// 同月份的,再根据日期计算年龄
30                 if (dayMinus < 0) {
31                 } else if (dayMinus >= 0) {
32                     age = age + 1;
33                     ageToMonth = String.valueOf(age)+"岁";
34                 }
35             } else if (monthMinus > 0) {
36                 age = age + 1;
37                 ageToMonth = String.valueOf(age)+"岁";
38             }
39         }
40         return ageToMonth;
41     }
1 public static void main(String[] args) {
2         String dataOfBirth = "2017-10-28";
3         String age = getAgeFromBirthTime(dataOfBirth);
4         System.out.println("age:" + age);
5     }
时间: 2024-08-14 05:04:03

Java 根据年月日精确计算年龄的相关文章

JS根据身份证号码精确计算年龄和性别

1 /** 2 * 根据身份证号得到姓别和精确计算年龄 3 */ 4 function analyzeIDCard(IDCard){ 5 var sexAndAge = {}; 6 //获取用户身份证号码 7 var userCard = IDCard; 8 //如果身份证号码为undefind则返回空 9 if(!userCard){ 10 return sexAndAge; 11 } 12 //获取性别 13 if(parseInt(userCard.substr(16,1)) % 2 ==

java实现根据身份证计算年龄的两种方式

第一种(推荐使用): import org.apache.hadoop.hive.ql.exec.UDF; import java.util.Calendar; public class GetAge extends UDF { public String evaluate(String sfzjh){ if(sfzjh == null || "".equals(sfzjh) ){ return "身份证件号有误,无法计算年龄"; } if (sfzjh.lengt

java的数字精确计算问题-BigDecimal

java的数字运算,偶尔喝出现精度的问题,以下阐述的 java的BigDecimal类的使用. 例如: System.out.println(0.9+0.3); 结果1.2 System.out.println(1.9+0.3); 结果2.1999999999999997 System.out.println(1.9+0.4); 结果2.3 java为了让float或者double类型能够精确的计算,提供了BigDecimal类. 示例如下: public static BigDecimal g

精确计算工具类,提供加减乘除的计算

package com.ljq.util; import java.math.BigDecimal; /** * 精确计算工具类,提供加减乘除的计算 * * @author jqlin */ public class CompuUtils { /**小数点后保留的位数*/ public final static int SCALE = 5; /** 0 */ public final static int ZERO = 0; /** * BigDecimal大小比较 * * @param a *

PHP计算年龄精确到年月日

<?php class CalAge { /* * 计算年龄精准到年月日 * @param type $byear * @param type $bmonth * @param type $bday * @return type */ public function getAge($byear, $bmonth, $bday) { $year = date('Y'); $month = date('m'); $day = date('d'); $bmonth = intval($bmonth);

第二章 Java浮点数精确计算

1.实际意义 在实际开发中,如果需要进行float或double的精确计算(尤其是财务计算),直接使用float或double是不行的(具体的例子看下边的代码的main方法的测试结果),需要使用BigDecimal. 2.代码 package com.xxx.util; import java.math.BigDecimal; /** * 浮点数精准算法 */ public class BigDecimalArithUtil { private static final int DIV_SCAL

Java中浮点型数据Float和Double进行精确计算的问题

一.浮点计算中发生精度丢失  大概很多有编程经验的朋友都对这个问题不陌生了:无论你使用的是什么编程语言,在使用浮点型数据进行精确计算时,你都有可能遇到计算结果出错的情况.来看下面的例子. // 这是一个利用浮点型数据进行精确计算时结果出错的例子,使用Java编写,有所省略. double a = (1.2 - 0.4) / 0.1;System.out.println(a); 如果你认为这个程序的输出结果是“8”的话,那你就错了.实际上,程序的输出结果是“7.999999999999999”.好

通过出生年月日按要求计算年龄的算法

最近项目里碰到个需求,有关于出生年月日计算年龄的,无奈网上搜了搜,感觉都不满足客户的需求只得自己动手了. 不废话,上需求 上算法: 类似于上面这个 yyyy-MM-dd 给一个这个值 算出来了多大年龄 l 不足一月新生儿显示日龄.2 不足一年婴幼儿显示月龄+日龄,如3个月12天.3 超过1周岁到12周岁,显示年龄+月龄,如12岁3个月. public static String getAgeAchen(Context context, Calendar birthday, Calendar no

Java使用BigDecimal解决精确计算的问题

最近有人在微信上给我发了一个数学题目,如下图: 我看了之后感觉很是简单,但是却想了半天才解出来.解出来后我想到了用程序再解一遍,然而精确计算的问题却让人头疼不已. 解题思路: 思路其实很简单,暴力求解就可以,但是当你写了一个四重for循环后你会发现解不出来.由此考虑到结果可能是小数,便把增量改成了float类型,每次自增0.1. 当你写完满心欢喜地运行的时候会发现还是出不来结果.再改成double类型也同样是不行. 这是因为java中float类型相加是把十进制转化为二进制后相加然后把二进制结果