根据当前时间 计算年龄 以及年龄单位-- 或者前几天的时间---前几分钟的时间
1、获取当前时间及 几十分钟前的时间:
传入不同值,根据实际需求进行计算返回时间
public MinuteAgoRespVo getMinuteAgoTime (Integer i) {
MinuteAgoRespVo vo = new MinuteAgoRespVo();
Date date = new Date();//获取当前时间
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
vo.setEndTime(date);
if (i == 1) {
// 10分钟前
calendar.add(Calendar.MINUTE, -10);
//获取到完整的时间
vo.setStartTime(calendar.getTime());
}else if (i == 2) {
// 20分钟前
calendar.add(Calendar.MINUTE, -20);
//获取到完整的时间
// String before = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
vo.setStartTime(calendar.getTime());
} else if (i == 3) {
// 30分钟前
calendar.add(Calendar.MINUTE, -30);
vo.setStartTime(calendar.getTime());
}
return vo;
}
2、 根据出生时间 获取年龄(传入参数是Date类型)
public static Integer getAgeByBirth(Date birthDay) throws ParseException {
int age = 0;
Calendar cal = Calendar.getInstance();
if (cal.before(birthDay)) { //出生日期晚于当前时间,无法计算
throw new IllegalArgumentException(
"The birthDay is before Now.It's unbelievable!");
}
int yearNow = cal.get(Calendar.YEAR); //当前年份
int monthNow = cal.get(Calendar.MONTH); //当前月份
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期
cal.setTime(birthDay);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
age = yearNow - yearBirth; //计算整岁数
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
if (dayOfMonthNow < dayOfMonthBirth){
age--;//当前日期在生日之前,年龄减一
}
} else {
age--;//当前月份在生日之前,年龄减一
}
}
return age;
}
3、根据生日 计算年龄(参数为 yyyy-MM-dd字符串类型)
public static int getAgeByBirth(String birthdayStr) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date birthday = sdf.parse(birthdayStr);
//Calendar:日历
/*从Calendar对象中或得一个Date对象*/
Calendar cal = Calendar.getInstance();
/*把出生日期放入Calendar类型的bir对象中,进行Calendar和Date类型进行转换*/
Calendar bir = Calendar.getInstance();
bir.setTime(birthday);
/*如果生日大于当前日期,则抛出异常:出生日期不能大于当前日期*/
if(cal.before(birthday)){
throw new IllegalArgumentException("The birthday is before Now,It's unbelievable");
}
/*取出当前年月日*/
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH);
int dayNow = cal.get(Calendar.DAY_OF_MONTH);
/*取出出生年月日*/
int yearBirth = bir.get(Calendar.YEAR);
int monthBirth = bir.get(Calendar.MONTH);
int dayBirth = bir.get(Calendar.DAY_OF_MONTH);
/*大概年龄是当前年减去出生年*/
int age = yearNow - yearBirth;
/*如果出当前月小与出生月,或者当前月等于出生月但是当前日小于出生日,那么年龄age就减一岁*/
if(monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)){
age--;
}
return age;
} catch (ParseException e) {
e.printStackTrace();
}
return -1;
}
4、根据生日计算年龄及年龄单位并返回 ( 0- 岁 , 1- 月 , 2- 天)
注意:传入参数需为字符串类型
yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss 类型
否则报错
public static AgeRespVo calculateAge(String dateOfBirth) {
AgeRespVo ageRespVo = new AgeRespVo();
String ageUnit = "岁";
LocalDateTime dob = null;
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 时间格式转换
try {
// 尝试将输入解析为没有时间的日期
dob = LocalDate.parse(dateOfBirth, dateFormatter).atStartOfDay();
} catch (DateTimeParseException e1) {
try {
// 如果失败,请尝试将输入解析为具有时间的日期
dob = LocalDateTime.parse(dateOfBirth, dateTimeFormatter);
} catch (DateTimeParseException e2) {
// 如果两次解析尝试都失败,则返回错误消息
throw new IllegalArgumentException("日期格式无效。 请输入格式为'yyyy-MM-dd'或'yyyy-MM-dd HH:mm:ss'的日期。");
}
}
if (dob.isAfter(LocalDateTime.now())) {
// 如果输入日期大于当前日期时间,则抛出异常
throw new IllegalArgumentException("输入日期不能大于当前日期时间。");
}
LocalDateTime currentDateTime = LocalDateTime.now();
Duration duration = Duration.between(dob, currentDateTime);
int years = (int) (duration.toDays() / 365);
int months = (int) ((duration.toDays() % 365) / 30);
int days = (int) (duration.toDays() % 30);
ageRespVo.setAge(years);
ageRespVo.setUnit(0);
if (years == 0) {
if (months == 0) {
ageUnit = "天";
ageRespVo.setAge(days);
ageRespVo.setUnit(2);
return ageRespVo;
} else {
ageUnit = "月";
ageRespVo.setAge(months);
ageRespVo.setUnit(1);
return ageRespVo;
}
}
return ageRespVo;
}
// 返回类
@Data
public class AgeRespVo {
private Integer age;
private Integer unit; // 0 岁 1 月 2 天
}