| | |
| | | import java.lang.management.ManagementFactory; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.LocalTime; |
| | | import java.time.ZoneId; |
| | | import java.time.ZonedDateTime; |
| | | import java.time.*; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | |
| | |
| | | w = 0; |
| | | return weekDays[w]; |
| | | } |
| | | |
| | | /** |
| | | * 根据生日计算年龄值以及年龄单位 |
| | | * @param birthday 生日 |
| | | * @return |
| | | */ |
| | | public static AgeResult calculateAge(Date birthday){ |
| | | LocalDate currentDate = LocalDate.now(); |
| | | LocalDate birthLocalDate = birthday.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); |
| | | Period period = Period.between(birthLocalDate, currentDate); |
| | | if (period.getYears() > 0){ |
| | | return new AgeResult(period.getYears(),1); |
| | | } |
| | | if (period.getMonths() > 0){ |
| | | return new AgeResult(period.getMonths(),2); |
| | | } |
| | | LocalDateTime birthDateTime = birthday.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); |
| | | LocalDateTime currentDateTime = LocalDateTime.now(); |
| | | Duration duration = Duration.between(birthDateTime, currentDateTime); |
| | | long days = duration.toDays(); |
| | | if (days > 0){ |
| | | return new AgeResult((int) days,3); |
| | | } |
| | | long hours = duration.toHours(); |
| | | if (hours > 0){ |
| | | return new AgeResult((int) hours,4); |
| | | } |
| | | long minutes = duration.toMinutes(); |
| | | return new AgeResult((int) minutes,5); |
| | | } |
| | | } |
| | | |