puzhibing
2023-11-28 7b726d5ff439e7b8bb66369ecc5881e370286bc8
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.stylefeng.guns.modular.system.util;
 
import com.stylefeng.guns.core.util.DateUtil;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
 
//日期转换年龄
public class DataUtil {
 
    private static TimeZone tz = TimeZone.getTimeZone("GMT+8");
 
    public static int getAge(Date dateOfBirth) {
 
        int age = 0;
 
        Calendar born = Calendar.getInstance();
 
        Calendar now = Calendar.getInstance();
 
        if (dateOfBirth != null) {
 
            now.setTime(new Date());
 
            born.setTime(dateOfBirth);
 
            if (born.after(now)) {
                return age;
//                throw new IllegalArgumentException("年龄不能超过当前日期");
            }
            age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR);
 
            int nowDayOfYear = now.get(Calendar.DAY_OF_YEAR);
 
            int bornDayOfYear = born.get(Calendar.DAY_OF_YEAR);
 
//            logger.debug("nowDayOfYear:" + nowDayOfYear + " bornDayOfYear:" + bornDayOfYear);
 
            if (nowDayOfYear < bornDayOfYear) {
 
                age -= 1;
            }
 
        }
 
        return age;
 
    }
    /**
     * 字符串日期转Date yyyy-MM-dd HH:mm:ss
     *
     * @param
     * @return
     */
    public static Date getDate_str3(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(tz);
        if ("".equals(dateStr)) {
            dateStr = sdf.format(DateUtil.getDate());
        }
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
 
 
    /**
     * 日期转毫秒
     *
     * @param date
     * @return
     */
    public static long getMillisecond(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newDate = "";
        if (!"".equals(date)) {
            newDate = sdf.format(date);
        } else {
            newDate = sdf.format(DateUtil.getDate());
        }
        long millisecond = 0;
        try {
            millisecond = sdf.parse(newDate).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return millisecond;
    }
 
    /**
     * 两日期相差天数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static long getMinusDay(Date startDate, Date endDate) {
        long startMillisecond = getMillisecond(startDate);
        long endMillisecond = getMillisecond(endDate);
        long minusMillisecond = endMillisecond - startMillisecond;
        long day = 0;
        if (minusMillisecond < 0) {
            day = 0;
        } else {
            day = minusMillisecond / (24 * 3600 * 1000);
        }
        return day;
    }
 
 
 
 
}