| package com.dsh.course.util; | 
|   | 
| import java.text.ParseException; | 
| import java.time.DayOfWeek; | 
| import java.time.Duration; | 
| import java.time.LocalDate; | 
| import java.time.LocalDateTime; | 
| import java.util.Calendar; | 
| import java.util.Date; | 
|   | 
| /** | 
|  * LocalDateTimeUtils | 
|  * LocalDateTime 时间工具 | 
|  * | 
|  * @author yudeshan | 
|  * @version V1.0 | 
|  */ | 
| public class LocalDateTimeUtils { | 
|     private final static int[] dayArr = new int[] { 20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22 }; | 
|     private final static String[] constellationArr = new String[] { "摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座" }; | 
|   | 
|   | 
|     /** | 
|      * 取两个时间相差毫秒数 | 
|      * | 
|      * @param start 开始时间 | 
|      * @param end   结束时间 | 
|      * @return | 
|      */ | 
|     public static long betweenMillis(LocalDateTime start, LocalDateTime end) { | 
|         Duration duration = Duration.between(start, end); | 
|         return duration.toMillis(); | 
|     } | 
|   | 
|     /** | 
|      * 通过生日计算年龄 | 
|      * @param birthDay 生日 | 
|      * @return | 
|      * @throws ParseException | 
|      */ | 
|     public static int getAgeByBirth(LocalDate birthDay) throws ParseException { | 
|         int age = 0; | 
|        //出生日期晚于当前时间,无法计算 | 
|         LocalDate now=LocalDate.now(); | 
|         if (birthDay.isAfter(now)) { | 
|             throw new IllegalArgumentException( "生日大于当前时间!"); | 
|         } | 
|         //当前年份 | 
|         int yearNow = now.getYear(); | 
|         //当前月份 | 
|         int monthNow = now.getMonthValue(); | 
|         //当前日期 | 
|         int dayOfMonthNow = now.getDayOfMonth(); | 
|   | 
|         int yearBirth =birthDay.getYear(); | 
|         int monthBirth = birthDay.getMonthValue(); | 
|         int dayOfMonthBirth = birthDay.getDayOfMonth(); | 
|         //计算整岁数 | 
|         age = yearNow - yearBirth; | 
|         if (monthNow <= monthBirth) { | 
|             if (monthNow == monthBirth) { | 
|                 if (dayOfMonthNow < dayOfMonthBirth){ | 
|                     //当前日期在生日之前,年龄减一 | 
|                     age--; | 
|                 } | 
|             } else { | 
|                 //当前月份在生日之前,年龄减一 | 
|                 age--; | 
|             } | 
|         } | 
|         return age; | 
|     } | 
|   | 
|     /** | 
|      * 根据月份计算星座 | 
|      */ | 
|   | 
|     public static String getConstellation(LocalDate date) { | 
|         Integer month=date.getMonthValue(); | 
|         Integer day=date.getDayOfMonth(); | 
|         return day < dayArr[month - 1] ? constellationArr[month - 1] : constellationArr[month]; | 
|     } | 
|   | 
|     /** | 
|      * 获取当天的星期几 | 
|      * @return | 
|      */ | 
|     public static int getWeekStr(){ | 
|         LocalDate today = LocalDate.now(); | 
|         DayOfWeek dayOfWeek = today.getDayOfWeek(); | 
|         return dayOfWeek.getValue(); | 
|     } | 
|   | 
|     /** | 
|      * 根据输入本周的周几,获取当天的时间 | 
|      * @param dayOfWeekInput 周几 | 
|      * @return | 
|      */ | 
|     public static Date getDateOfWeekNum(int dayOfWeekInput){ | 
|         // 获取当前日期 | 
|         Calendar calendar = Calendar.getInstance(); | 
|         calendar.setTime(new Date()); | 
|   | 
|         // 设置为本周的第一天(星期一) | 
|         calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); | 
|   | 
|         // 根据输入的星期几数值,计算出相应的偏移量 | 
|         int dayOfWeekIndex = (dayOfWeekInput + 6) % 7; | 
|         calendar.add(Calendar.DAY_OF_WEEK, dayOfWeekIndex); | 
|   | 
|         // 获取计算后的日期 | 
|         return calendar.getTime(); | 
|     } | 
|   | 
|   | 
| } |