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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package com.consum.base.util;
 
import org.apache.commons.lang3.StringUtils;
 
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
 
/**
 * 功能描述: 时间处理
 *
 * @author lxk
 * @date  2024/4/9 10:34
 * @version 1.0
 **/
public class DateUtil {
    /**
     * @author: hyf
     * @create: 2023/4/26 0026
     * @description: 获取当前时间
     *
     **/
    public static Timestamp getNowTimestamp(){
        return new Timestamp(System.currentTimeMillis());
    }
 
    /**
     * 获取当前时间的long值
     *
     * @return 返回当前时间的long值:例 20230101101010
     */
    public static Long getNowDate() {
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        long currentTimeMillis = System.currentTimeMillis();
        Date date = new Date(currentTimeMillis);
        String format = df.format(date);
        return new Long(format);
    }
 
    /**
     * 把14位的时间long,转化时间字符串
     * @param dateLong  long时间14位
     * @param format 要转的时间格式
     * @return
     */
    public static String getStrDateByLong(Long dateLong,String format) {
        String dateStr = dateLong.toString();
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        DateFormat df2 = new SimpleDateFormat(format);
        try {
            Date parse = df.parse(dateStr);
            String format1 = df2.format(parse);
            return format1;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
 
    public static Long DateToLong(Date date) {
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = df.format(date);
        return new Long(format);
    }
 
    /**
     * 获取当前时分
     * @return
     */
    public static Long getNowTime4HHMM() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHmm");
        LocalTime localTime = LocalTime.now();
        String format = localTime.format(formatter);
        return Long.parseLong(format);
    }
 
    /**
     * 判断当前时间在某个区间
     * @param startTime
     * @param endTime
     * @return
     */
    public static Boolean betweenNowTime(Long startTime, Long endTime) {
        Long nowTime4HHMM = DateUtil.getNowTime4HHMM();
        if (startTime <= nowTime4HHMM && endTime >= nowTime4HHMM) {
            return true;
        }
        return false;
    }
 
    /**
     * 时间添加天
     * @param time
     * @param day
     * @return
     */
    public static Long addDay(Long time, Long day) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime localDateTime = LocalDateTime.parse(time.toString(time), formatter);
        LocalDateTime newTime = localDateTime.plusDays(day);
        String format = newTime.format(formatter);
        return Long.parseLong(format);
    }
 
    /**
     * 时间减少天
     * @param time
     * @param day
     * @return
     */
    public static Long subDay(Long time, Long day) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime localDateTime = LocalDateTime.parse(time.toString(time), formatter);
        LocalDateTime newTime = localDateTime.minusDays(day);
        String format = newTime.format(formatter);
        return Long.parseLong(format);
    }
 
    private static final DateTimeFormatter dayFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
    private static final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    private static final SimpleDateFormat birthdayFormat = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 方法描述: 日期类型格式转换
     * eg:20240428163146 转换为 自定义日期格式
     *
     **/
    public static String longToStr(Long time,String fmt) {
        DateTimeFormatter defaultFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if(StringUtils.isNotBlank(fmt)){
            defaultFormatter = DateTimeFormatter.ofPattern(fmt);
        }
        if(time.toString().length() == 8) {
            LocalDate start = LocalDate.parse(time.toString(), dayFormatter);
            return start.format(defaultFormatter);
        } else {
            LocalDateTime start = LocalDateTime.parse(time.toString(), timeFormatter);
            return start.format(defaultFormatter);
        }
    }
 
    /**
     * 根据生日获取年龄
     * eg:出生年月日=2000-04-30;当前时间=2024-04-29;则age=24
     * @param birthday  出生日期
     * @return
     */
    private static int getAgeByBirth(String birthday) {
        int age = 0;
        try {
            Date date = birthdayFormat.parse(birthday);
            Calendar now = Calendar.getInstance();
            // 当前时间
            now.setTime(new Date());
            Calendar birth = Calendar.getInstance();
            birth.setTime(date);
            //如果传入的时间,在当前时间的后面,返回0岁
            if (birth.after(now)) {
                age = 0;
            } else {
                age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
                if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
                    age += 1;
                }
            }
            return age;
        } catch (Exception e) {
            e.getStackTrace();
            return 0;
        }
    }
 
    /**
     * 根据年龄获取生日
     * eg:age=24;当前时间=2024-04-30;则出生年月日=2000-04-30;
     * @param age  年龄
     * @return
     */
    public static LocalDate getBirthByAge(int age){
        LocalDate localDate = LocalDate.now();
        return localDate.minusYears(age);
    }
 
    /**
     * 获取当前日期
     */
    public static Long getNowTime(String fmt) {
        if(StringUtils.isBlank(fmt)){
            fmt = "yyyyMMddHHmmss";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(fmt);
        if(fmt.length() == 8) {
            LocalDate localTime = LocalDate.now();
            String format = localTime.format(formatter);
            return Long.parseLong(format);
        } else {
            LocalDateTime localTime = LocalDateTime.now();
            String format = localTime.format(formatter);
            return Long.parseLong(format);
        }
 
 
    }
 
    /**
     * 判断当前日期在某个区间
     * @param startDate 开始日期
     * @param endDate   截止日期
     * @param fmt   自定义格式;默认=yyyyMMddHHmmss
     */
    public static Boolean betweenNowDateTime(Long startDate, Long endDate,String fmt) {
        Long nowDate = DateUtil.getNowTime(fmt);
        if (startDate <= nowDate && endDate >= nowDate) {
            return true;
        }
        return false;
    }
 
    /**
     * 获取当前时间的long值
     * @param fmt   自定义格式
     * @return
     */
    public static Long getNowDateLong(String fmt) {
        if(StringUtils.isBlank(fmt)) {
            fmt = "yyyyMMddHHmmss";
        }
        DateFormat df = new SimpleDateFormat(fmt);
        long currentTimeMillis = System.currentTimeMillis();
        Date date = new Date(currentTimeMillis);
        String format = df.format(date);
        return new Long(format);
    }
 
    public static void main(String aaa[]){
        //System.out.println(getAgeByBirth("2025-04-30"));
 
//        System.out.println(betweenNowDateTime(20240429135709L,20240429170909L,""));
//        System.out.println(getBirthByAge(23).toString());
//        System.out.println(longToStr(20240515L,"yyyy-MM-dd"));
        System.out.println(getNowDateLong("yyyyMMdd"));
    }
 
 
}