peter
2025-11-26 566a1b9fda0276e2cc4a35f7ba322c0e599a2c84
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
251
252
253
254
255
package com.nq.utils;
 
 
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
 
public class DateTimeUtil {
    private static final Logger log = LoggerFactory.getLogger(DateTimeUtil.class);
 
 
    public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
 
 
    public static final String YMD_FORMAT = "yyyy-MM-dd";
 
 
    public static final String HM_FORMAT = "HH:mm";
 
 
    public static Date getCurrentDate() {
        return new Date();
    }
 
 
    public static Date strToDate(String dateTimeStr, String formatStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }
 
 
    public static String dateToStr(Date date, String formatStr) {
        if (date == null) {
            return "";
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }
 
    public static Date strToDate(String dateTimeStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }
 
    /**
     * 当前日期
     *
     * @param time
     * @return
     */
    public static String dateToStr1(Date time) {
        if (time == null) {
            return "";
        }
        DateTime dateTime = new DateTime(time);
        return dateTime.toString("yyyy-MM-dd");
    }
 
    /*
     * 將時間轉換為時間戳
     */
    public static String dateToStamp(String time) {
        String stamp = "";
        if (!"".equals(time)) {//時間不為空
            try {
                String res;
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = simpleDateFormat.parse(time);
                long ts = date.getTime();
                res = String.valueOf(ts);
                return res;
            } catch (Exception e) {
                System.out.println("參數為空!");
            }
        } else {    //時間為空
            long current_time = System.currentTimeMillis();  //獲取當前時間
            stamp = String.valueOf(current_time / 1000);
        }
        return stamp;
    }
 
 
    /*
     * 將時間戳轉換為時間
     */
    public static String stampToDate(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
 
    /*獲取當前時間戳*/
    public static String getStampNow() {
        Long startTs = System.currentTimeMillis(); // 當前時間戳
        return startTs.toString();
    }
 
 
    public static Timestamp searchStrToTimestamp(String dateTimeStr) {
        return Timestamp.valueOf(dateTimeStr);
    }
 
 
    public static String dateToStr(Date date) {
        if (date == null) {
            return "";
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString("yyyy-MM-dd HH:mm:ss");
    }
 
 
    public static Date longToDate(Long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
        String d = format.format(time);
 
        Date date = null;
        try {
            date = format.parse(d);
        } catch (Exception e) {
            log.error("datetime utils longToDate error");
        }
        return date;
    }
 
 
    public static Date doEndTime(Date begintime, int month) {
        Long begintimelong = Long.valueOf(begintime.getTime() / 1000L);
        log.info("計算時間 傳入時間 = {} , 時間戳 = {}", dateToStr(begintime), begintimelong);
 
        Long endtimelong = Long.valueOf(begintimelong.longValue() + (2592000 * month));
        Date endtimedate = longToDate(Long.valueOf(endtimelong.longValue() * 1000L));
 
        log.info("endtime 時間戳 = {},時間 = {} , 格式化時間={}", new Object[]{endtimelong, endtimedate,
                dateToStr(endtimedate)});
 
        return endtimedate;
    }
 
 
    public static String getCurrentTimeMiao() {
        return String.valueOf(System.currentTimeMillis() / 1000L);
    }
 
    //获取当前时间戳转年月日时分 秒都是0
    public static String getCurrentTimeMiaoZero() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(new Date());
        String[] split = date.split(" ");
        String[] split1 = split[0].split("-");
        String[] split2 = split[1].split(":");
        String s = split1[0] + "-" + split1[1] + "-" + split1[2] + " " + split2[0] + ":" + split2[1] + ":00";
        //s转编码
        String s1 = null;
        try {
            s1 = URLEncoder.encode(s, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return s1;
    }
 
 
    public static Date parseToDateByMinute(int minuteTimes) {
        Date nowDate = new Date();
        Long nowtimes = Long.valueOf(nowDate.getTime() / 1000L);
 
        Long beginTimesLong = Long.valueOf(nowtimes.longValue() - (minuteTimes * 60));
        return longToDate(Long.valueOf(beginTimesLong.longValue() * 1000L));
    }
    //分钟时间戳
 
 
    public static boolean isCanSell(Date buyDate, int maxMinutes) {
        Long buyDateTimes = Long.valueOf(buyDate.getTime() / 1000L);
 
        buyDateTimes = Long.valueOf(buyDateTimes.longValue() + (maxMinutes * 60));
 
        Long nowDateTimes = Long.valueOf((new Date()).getTime() / 1000L);
 
        if (nowDateTimes.longValue() > buyDateTimes.longValue()) {
            return true;
        }
        return false;
    }
 
    /*日期年月日是否相同*/
    public static boolean sameDate(Date d1, Date d2) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
        //fmt.setTimeZone(new TimeZone()); // 如果需要設置時間區域,可以在這裏設置
        return fmt.format(d1).equals(fmt.format(d2));
    }
 
    /**
     * 【參考】https://www.cnblogs.com/zhaoKeju-bokeyuan/p/12125711.html
     * 基於指定日期增加天數
     *
     * @param date
     * @param num  整數往後推,負數往前移
     * @return
     */
    public static Date addDay(Date date, int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, num);
        return cal.getTime();
    }
 
    //获取当前时间 判断是否是周六日 如果是周六日 则获取周五的时间
    public static String getWeekDay() {
        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DAY_OF_WEEK);
        if (day == 1) {
            cal.add(Calendar.DATE, -2);
        } else if (day == 7) {
            cal.add(Calendar.DATE, -1);
        }
        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }
 
 
    public static void main(String[] args) {
        String DateStr = "2023-03-09 00:00:00";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date dateTime1 = dateFormat.parse(DateStr);
            System.out.println("dateTime1.getTime()=" + dateTime1.getTime());
            System.out.println("系统=" + DateTimeUtil.getCurrentDate().getTime());
            System.out.println("系统=" + DateTimeUtil.dateToStr(DateTimeUtil.getCurrentDate(), "yyyy-MM-dd HH:mm:ss"));
 
 
            System.out.println(DateTimeUtil.getCurrentDate().getTime() < dateTime1.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}