1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.yiboshi.science.utils;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
/**
* 年龄计算
*/
public class AgeUtil {
/**
* 根据出生日期计算月龄
*
* @param birthDate 出生日期 格式为 yyyy-MM-dd
* @return 月龄
*/
public static int getResidentAge(Date birthDate) {
if (null == birthDate) {
return 0;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateBirth = sdf.format(birthDate);
LocalDate now = LocalDate.now();
LocalDate birth = null;
try {
birth = LocalDate.parse(dateBirth, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
} catch (Exception e) {
throw new RuntimeException("日期格式应该为:yyyy-MM-dd,例如:20xx-01-01");
}
int year = now.getYear() - birth.getYear();
int month = now.getMonthValue() - birth.getMonthValue();
if (month<0){
year=year-1;
}
return year;
}
/**
* 时间加减年份
* @param time
* @param num
* @return
*/
public static Date yearAddNum(Date time, Integer num) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
calendar.add(Calendar.YEAR, num);
Date newTime = calendar.getTime();
return newTime;
}
/**
* 时间加减天数
* @param time
* @param days
* @return
*/
public static Date dateAddDays(Date time,Integer days) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
calendar.add(Calendar.DAY_OF_MONTH, days);
Date newTime = calendar.getTime();
return newTime;
}
}