JAVA根据生日计算年龄(年月日)

根据生日计算距离今天几年几个月几天

1Date birthday = xxxxxxxx 2Calendar now = Calendar.getInstance(); 3 Calendar b = Calendar.getInstance(); 4 b.setTime(birthday); 5 int year = now.get(Calendar.YEAR) - b.get(Calendar.YEAR); 6 int month = now.get(Calendar.MONTH) - b.get(Calendar.MONTH); 7 int day = now.get(Calendar.DAY_OF_MONTH) - b.get(Calendar.DAY_OF_MONTH); 8 if (month < 0) { 9 month = 12 - b.get(Calendar.MONTH) + now.get(Calendar.MONTH); 10 year -= 1; 11 } 12 if (day < 0) { 13 day = b.getMaximum(Calendar.DAY_OF_MONTH) - b.get(Calendar.DAY_OF_MONTH) + now.get(Calendar.DAY_OF_MONTH); 14 month -= 1; 15 } 16 // year + "岁" + month + "个月" + day + "天";
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

日期

根据险种的可选择年龄区间(年)获取出生年月日展示区间规则当前年年龄/当前月/当前日1/util.getAgeRangeWithYearfunction(age)letnowDateYearnewDate().getFullYear()1;letnowDateMonthnewDate().getMonth(

java根据开始时间和结束时间,计算中间天数,并打印

java根据开始时间和结束时间,计算中间天数,并打印import java.text.SimpleDateFormat;import java.util.Date;public class Calcdate {    static SimpleDateFormat sdf  new Simple

JavaScript 通过身份证号获取出生日期、年龄、性别 、籍贯

JavaScript通过身份证号获取出生日期、年龄、性别、籍贯(很全)效果图:!(https://oscimg.oschina.net/oscnet/805f0196321a1ac3f774290c93d33d6fa71.png)示例代码://由于没有写外部JS,所以代码比较长!!!<!DOCTYPEhtmlPUB