1
yangbin
2024-08-15 fc38e2635216775a80210d0df109dc6174d66813
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
package org.jeecg.modules.utils;
 
import lombok.extern.slf4j.Slf4j;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
/**
 * Created by Administrator on 2015/8/28.
 */
@Slf4j
public class DateUtil {
    public static final String STR_DATE = "yyyy-MM-dd";
    public static final String STR_YEAR_MONTH = "yyyy-MM";
    public static final String STR_DATE_TIME = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String STR_DATE_TIME_SMALL = "yyyy-MM-dd HH:mm:ss";
    public static final String STR_DATE_TIME_MIN = "yyyy-MM-dd HH:mm";
    public static final String STR_DATE_TIME_HOUR = "yyyy-MM-dd HH";
    public static final String STR_DATE_TIME_FULL = "yyyyMMddHHmmssSSS";
    public static final String STR_YEARMONTH = "yyyyMM";
    public static final String STR_YEAR = "yyyy";
 
    /**
     * 获取当前时间
     * @return
     */
    public static Date getNow() {
        return new Date(System.currentTimeMillis());
    }
 
    /**
     * <p>
     * Description: 去掉日期时间中的时间部分
     * </p>
     * 如: 2013-11-11 18:56:33 ---> 2013-11-11 00:00:00
     *
     * @param date 需要修改的时间
     * @return 修改后的时间
     */
    public static Date removeTime(Date date) {
        Date result = null;
        try {
            SimpleDateFormat df = new SimpleDateFormat(STR_DATE);
            String dateStr = df.format(date);
            result = df.parse(dateStr);
        } catch (ParseException e) {
            log.error(e.getMessage(), e.getStackTrace());
            return null;
        }
        return result;
    }
 
    /**
     * <p>
     * Description: 按指定格式获取时间字符串
     * </p>
     *
     * @param date   要转换的日期
     * @param format 格式,例如:yyyy-MM-dd HH:mm:ss.SSS
     * @return 转换后的时间字符串
     */
    public static String format(Date date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }
 
    /**
     * 获取日期的天字符串
     * @param date
     * @return
     */
    public static String getDayStr(Date date) {
        Calendar calendar = Calendar.getInstance(); // 得到日历
        calendar.setTime(date);// 把当前时间赋给日历
        int day = calendar.get(Calendar.DATE);//获取日
        String dayStr = day < 10 ? "0" + day : day + "";
        return dayStr;
    }
 
    /**
     * 获取日期的年份字符串
     * @param date
     * @return
     */
    public static String getYearStr(Date date) {
        Calendar calendar = Calendar.getInstance(); // 得到日历
        calendar.setTime(date);// 把当前时间赋给日历
        String yearStr = calendar.get(Calendar.YEAR) + "";//获取日
        return yearStr;
    }
 
    /**
     * 获取日期的月份字符串
     * @param date
     * @return
     */
    public static String getMonthStr(Date date) {
        Calendar calendar = Calendar.getInstance(); // 得到日历
        calendar.setTime(date);// 把当前时间赋给日历
        int month = calendar.get(Calendar.MONTH);//获取日
        String monthStr = month < 10 ? "0" + month : month + "";
        return monthStr;
    }
 
    /**
     * 获取当前时间 前后i天
     * 精确到秒
     * @param i 为正数代表加i天,为负数代表减i天
     * @return
     */
    public static Date addDay(Date date, int i){
        Calendar calendar = Calendar.getInstance(); // 得到日历
        calendar.setTime(date);// 把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, i); // 设置天数加减
        return calendar.getTime();
    }
 
    /**
     * 获取当前时间 前后i秒
     * 精确到秒
     * @param i 为正数代表加i秒,为负数代表减i秒
     * @return
     */
    public static Date addSeconds(Date date, int i){
        Calendar calendar = Calendar.getInstance(); // 得到日历
        calendar.setTime(date);// 把当前时间赋给日历
        calendar.add(Calendar.SECOND, i); // 设置秒数加减
        return calendar.getTime();
    }
 
    public static Date toDate(String date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        try {
            return df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}