Java 计算年龄
public static String getAgeTxt(String birthTime,String beginTime,int level){
if(StringUtils.isBlank(birthTime)||StringUtils.isBlank(beginTime)){
System.out.println("参数中有空值!");
}
int year = 0,month=0,day=0,hour=0;
Date birthDate = getDateByString(birthTime,"yyyy-MM-dd HH:mm:ss");
Date beginDate = getDateByString(beginTime,"yyyy-MM-dd HH:mm:ss");
Calendar cBirthDate = Calendar.getInstance();
Calendar cBeginDate = Calendar.getInstance();
cBirthDate.setTime(birthDate);
cBeginDate.setTime(beginDate);
if(cBeginDate.get(Calendar.YEAR) < cBirthDate.get(Calendar.YEAR)){
return "出生日期大于当前时间";
}
//计算出生小时
if(cBeginDate.get(Calendar.HOUR_OF_DAY) < cBirthDate.get(Calendar.HOUR_OF_DAY)){
hour = cBeginDate.get(Calendar.HOUR_OF_DAY)+24 - cBirthDate.get(Calendar.HOUR_OF_DAY);
day = day-1;
}else{
hour = cBeginDate.get(Calendar.HOUR_OF_DAY) - cBirthDate.get(Calendar.HOUR_OF_DAY);
}
//计算出生日
if(cBeginDate.get(Calendar.DAY_OF_MONTH)<cBirthDate.get(Calendar.DAY_OF_MONTH)){
day = day + cBeginDate.get(Calendar.DAY_OF_MONTH)+ cBirthDate.getActualMaximum(Calendar.DAY_OF_MONTH)-cBirthDate.get(Calendar.DAY_OF_MONTH);
month = month-1;
}else {
day = day + cBeginDate.get(Calendar.DAY_OF_MONTH)-cBirthDate.get(Calendar.DAY_OF_MONTH);
}
//计算出生月
if(cBeginDate.get(Calendar.MONTH) < cBirthDate.get(Calendar.MONTH)){
month = month + cBeginDate.get(Calendar.MONTH)+12 - cBirthDate.get(Calendar.MONTH);
year = year -1;
}else{
month = month + cBeginDate.get(Calendar.MONTH) - cBirthDate.get(Calendar.MONTH);
}
//计算出生年
year = year + cBeginDate.get(Calendar.YEAR) - cBirthDate.get(Calendar.YEAR);
if(year >7){
return year+"岁";
}else if (year >0 && year <7){
if(month>0 && month <10){
return year+"岁零"+month+"月";
}else if(month >=10 ){
return year+"岁"+month+"月";
}else {
if(day>0){
return year+"岁零"+day+"天";
}else{
return year+"岁";
}
}
}else {
if(month >0){
if( day >0 && day <10){
return month+"个月零"+day+"天";
}else if(day >=10){
return month+"个月"+day+"天";
}else{
return month+"个月";
}
}else{
if(day >0){
if( hour >0 && hour <10){
return day+"天零"+hour+"小时";
}else if(hour >=10){
return day+"天"+hour+"小时";
}else{
return day+"天";
}
}else{
if(hour>0){
return hour+"小时";
}else{
return "出生不足一小时";
}
}
}
}
}
其中比较重要的几点
1、Calendar.getActualMaximum()计算对应的范围内最大值
2、计算年龄应该是从小到大计算年龄,这样方便计算后面的年龄(利用减法的原理);
Java 计算年龄的更多相关文章
- java计算年龄
精确到天计算年龄 public static int getAgeByCardId(String card) throws Exception { Integer len = card.length( ...
- Java 根据年月日精确计算年龄
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created b ...
- Java 根据出生日期计算年龄
1.把出生日期字符串转换为日期格式. public static Date parse(String strDate) throws ParseException { SimpleDateFormat ...
- java身份证计算年龄
技术交流群: 233513714 /** * 根据身份证计算年龄 * * @param idcard * @return */ public static Integer idCardToAge(St ...
- java 根据生日计算年龄 Java问题通用解决代码
根据生日计算年龄可以通过Calendar实现.最简单可以考虑get(Calendar.DAY_OF_YEAR)来简单修正年龄,但是遇到生日在闰年的2月29之后,或者今年是闰年的2月29之后可能出现计算 ...
- SQL语句通过身份证号计算年龄
SQL语句通过身份证号计算年龄 1.截取身份证号上的出生日期 身份证一般为18位数和15位数 18位数身份证的第7-10位数是出生年份,第11-14位数是出生月日,所以18位身份证的年龄计算如下 su ...
- 用Java计算某个日期100天后的日期
用Java计算日期时间,首先考虑到时间类,Java中提供的和时间日期相关的类里,有一个Calendar类,可以获取某个时间日期. 计算某个日期100天后的日期,思路如下: 1.先设定时间,例如2017 ...
- java计算两个日期相差多少天
java计算两个日期相差多少天 public class DateUtil{ public static int betweenDays(Date startDate, Date endDate ) ...
- iOS 获取当前时间以及计算年龄(时间差)
获取当前时间 NSDate *now = [NSDate date]; NSLog(@"now date is: %@", now); NSCalendar *calendar = ...
随机推荐
- CentOS下安装go语言编译环境
1.下载Go语言的安装包 这里给大家一个百度的分享连接http://pan.baidu.com/s/1qY3xPaG下载到CentOS的系统之中 $ tar -xzf go1.5.2.linux-xx ...
- 深入cocos2d-x中的touch事件
深入cocos2d-x中的touch事件 在文章cocos2d-x中处理touch事件中简单讨论过怎样处理touch事件, 那么今天来深入了解下cocos2d-x中是怎样分发touch事件的. 我们最 ...
- 中小研发团队架构实践之应用监控Metrics
一.Metrics简介 应用监控系统Metrics由Metrics.NET+InfluxDB+Grafana组合而成,通过客户端Metrics.NET在业务代码中埋点,Metrics.N ...
- CENTOS6.6 下mysql MHA架构搭建
本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 摘要: 本篇是自己搭建的一篇mysql MHA文章 前面的安装步骤基 ...
- datatables行编辑中,某个字段用户显示和用于行编辑名称不同时的处理。
比如tag这个字段,对应服务端bean的tag,但是在页面显示时需要为String类型的tagName,那么在行编辑时可以用以下的方式处理.
- Java经典编程题50道之二十八
对10个数进行排序. public class Example28 { public static void main(String[] args) { int[] s = { 5 ...
- 第十八章 DjangoWeb开发框架
第十八章 DjangoWeb开发框架 第一课 内容概要: 1.JS正则 -登录注册验证 2.组件 1.BootStrap -css -js 学习BootStrap规则 2.jQueryUI -css ...
- 在Angular中利用trackBy来提升性能
在Angular的模板中遍历一个集合(collection)的时候你会这样写: <ul> <li *ngFor="let item of collection"& ...
- 1.3 fractions模块
数学世界中,浮点数还可以用分数形式展示,不可约简的分数形式往往更简洁直观. 问题来了,Python中如何输出不可约简的分数形式呢? 答案:用Fraction类来实现.这个类属于标准库的fracti ...
- 【BZOJ3309】DZY Loves Math
Time Limit: 5000 ms Memory Limit: 512 MB Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * ...