From 6a4b079181d22a54a11dfdafd5400b171a1f3fc4 Mon Sep 17 00:00:00 2001
From: lisy <java@lishouyideAir.lan>
Date: 星期三, 14 六月 2023 12:11:10 +0800
Subject: [PATCH] 开始上课:用户的学员信息主页查询接口(实体类,表的工具类)

---
 cloud-server-other/src/main/java/com/dsh/other/util/DateUtil.java |   33 +++++++++++++++++++++++++++++++++
 1 files changed, 33 insertions(+), 0 deletions(-)

diff --git a/cloud-server-other/src/main/java/com/dsh/other/util/DateUtil.java b/cloud-server-other/src/main/java/com/dsh/other/util/DateUtil.java
index d23cc1b..38365c2 100644
--- a/cloud-server-other/src/main/java/com/dsh/other/util/DateUtil.java
+++ b/cloud-server-other/src/main/java/com/dsh/other/util/DateUtil.java
@@ -329,4 +329,37 @@
         return dt1.getTime();
     }
 
+
+    /**
+     * 计算年龄
+     * @param birthDate
+     * @return
+     */
+    public static int age(Date birthDate) {
+        // 当前日历
+        Calendar nowCalendar = Calendar.getInstance();
+        // 生日大于当前日期
+        if (nowCalendar.before(birthDate)) {
+            throw new IllegalArgumentException("The birth date is before current time, it's unbelievable");
+        }
+        // 当前年月日
+        int yearNow = nowCalendar.get(Calendar.YEAR);
+        int monthNow = nowCalendar.get(Calendar.MONTH);
+        int dayNow = nowCalendar.get(Calendar.DAY_OF_MONTH);
+        // 出生日期年月日
+        Calendar birthCalendar = Calendar.getInstance();
+        birthCalendar.setTime(birthDate);
+        int yearBirth = birthCalendar.get(Calendar.YEAR);
+        int monthBirth = birthCalendar.get(Calendar.MONTH);
+        int dayBirth = birthCalendar.get(Calendar.DAY_OF_MONTH);
+        // 粗计算年龄
+        int age = yearNow - yearBirth;
+        // 当前月份小于出生月份年龄减一
+        if (monthNow < monthBirth) { age--; }
+        // 当前月份等于出生月份,日小于生日年龄减一
+        else if (monthNow == monthBirth && dayNow < dayBirth) { age--; }
+        // 返回年龄值
+        return age;
+    }
+
 }

--
Gitblit v1.7.1