3.7
fxl
2023-03-07 52cffc4ab8e9787a6f233295502c7c9788dddae1
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
package com.hxzkoa.udp;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
 
public class GetNowTime {
 
    /** 显示年月日时分秒2020-11-23 21:47:45 */
    public static String now() {// 获取时间方法
        Date date = new Date();// 日期对象
        String GreatTime1 = String.format("%tF", date);// 格式时间年月日
        String GreatTime2 = String.format("%tT", date);// 格式时间时分
        String GreatTime = GreatTime1 + " " + GreatTime2;// 以年月日 时分秒显示
        date = null;
        return GreatTime;
 
    }
 
    /** 获取年月日时间 格式为20210309 */
    public static String getyearmd() {
        Date date = new Date();// 日期对象
        String GreatTime1 = String.format("%tF", date);// 格式时间年月日
        String time = GreatTime1.replace("-", "");
        return time;
    }
 
    /** 增加或者减去n天后的日期 */
    public static String addDay(int n) {
        try {
 
            Date date = new Date();// 日期对象
            String GreatTime1 = String.format("%tF", date);// 格式时间年月日
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cd = Calendar.getInstance();
            cd.setTime(sdf.parse(GreatTime1));
            cd.add(Calendar.DATE, n);// 增加一天
            // cd.add(Calendar.MONTH, n);//增加一个月
 
            String time = sdf.format(cd.getTime()).replace("-", "");
            return time;
 
        } catch (Exception e) {
            return null;
        }
    }
 
    /** 增加或者减去n天后的日期 */
    public static String addDay1(int n) {
        try {
 
            Date date = new Date();// 日期对象
            String GreatTime1 = String.format("%tF", date);// 格式时间年月日
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cd = Calendar.getInstance();
            cd.setTime(sdf.parse(GreatTime1));
            cd.add(Calendar.DATE, n);// 增加一天
 
            String time = sdf.format(cd.getTime());
            return time;
 
        } catch (Exception e) {
            return null;
        }
    }
 
    /** 显示年月日时分秒2020-11-23_21:47:45 */
    public static String nowfinename() {// 获取时间方法
        Date date = new Date();// 日期对象
        String GreatTime1 = String.format("%tF", date);// 格式时间年月日
        String GreatTime2 = String.format("%tT", date);// 格式时间时分
        String GreatTime = GreatTime1 + "_" + GreatTime2;// 以年月日 时分秒显示
        date = null;
        return GreatTime;
 
    }
 
    /** 获取年月日时间2020-11-23 */
    public static String y_m_d() {// 获取时间方法
        Date date = new Date();// 日期对象
        String GreatTime1 = String.format("%tF", date);// 格式时间年月日
        return GreatTime1;
 
    }
 
    /** 时间格式 年-月-日 时:分:秒 */
    public static String getTimeDay(int index) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        calendar.add(Calendar.DAY_OF_MONTH, index);
        String date = fmt.format(calendar.getTime());
        return date;
    }
 
    /** 获取当前时间的时分秒 形如:19:19:20 */
    public static String HH_MM_SS() {// 获取时间方法
        Date date = new Date();// 日期对象
        String hms = String.format("%tT", date);// 格式时间时分
        String GreatTime = hms;
        return GreatTime;
 
    }
 
    /** 时间格式月-日 时分秒11-23 21:48:48 **/
    public static String now2() {// 获取时间方法
        Date date = new Date();// 日期对象
        String mounth = String.format("%tm", date);// 格式时间时分
        String day = String.format("%td", date);// 格式时间年月日
        String hms = String.format("%tT", date);// 格式时间时分
        String GreatTime = mounth + "-" + day + " " + hms;
        return GreatTime;
 
    }
 
    /** 获取年月日时分秒毫秒时间 2020-05-30 12:33:45.23 */
    public static String timestamp2() {
        String d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(Calendar.getInstance().getTime());
        return d;
    }
 
    /** 获取年月日时分秒毫秒时间 2020-05-30 12:33:45.23 */
    @SuppressWarnings("unused")
    public static String gethm() {
        Date date = new Date();// 日期对象
        String mounth = String.format("%tm", date);// 格式时间时分
        String day = String.format("%td", date);// 格式时间年月日
        String hms = String.format("%tT", date);// 格式时间时分
        String GreatTime = hms;
        return GreatTime;
    }
 
    /** 获取毫秒时间戳21:49:27.519 */
    public static String sss() {// 获取时间方法
        String msg = "";
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
        msg += sdf.format(date);
        return msg;
 
    }
 
    /**
     * 获取JAN需要的时间戳timestamp时间戳 (小时*3600+分钟*60+秒)*1000 +毫秒 举例: 78598245
     */
    public static int timestamp() {
 
        int time = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        String inputString = sss();
        Date date = null;
        try {
            date = sdf.parse("1970-01-01 " + inputString);
        } catch (ParseException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
 
        time = (int) date.getTime();
        return time;
    }
 
    /** 将年月日时间戳转为毫秒整数 */
    public static int timestamp(String timestamp) {
        int time = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date date = null;
        try {
            date = sdf.parse("1970-01-01 " + timestamp.substring(11, 22));
        } catch (ParseException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
 
        time = (int) date.getTime();
        return time;
    }
 
}