1
zj
2025-04-17 ff2d1f5acdadc466d7e199028ef385ae8ca277e7
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package util;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
/*******************************************************************************
 * class name:DateOperate
 * 
 * description:日期时间处理类,封装了处理日期时间的常用方法。包括获取当前时间, 按指定格式格式化时间等方法
 * 
 ******************************************************************************/
public class DateOperate {
 
    public static final String DEFAULTSTYLE = "yyyy-MM-dd";//默认时间格式
    
    public static final String DATE_TIME_STYLE_All="yyyy-MM-dd HH:mm:ss";//时间格式(全)
 
    /***************************************************************************
     * 函数功能:获取当前年份
     * 
     * 参数说明:无
     * 
     * 返回值:返回当前年份字符串
     **************************************************************************/
    public static String getNowYear() {
        Calendar cd = Calendar.getInstance();
        int year = cd.get(Calendar.YEAR);
        return Integer.toString(year);
    }
 
    /***************************************************************************
     * 函数功能:获取当前月份
     * 
     * 参数说明:无
     * 
     * 返回值:返回当前月份字符串
     **************************************************************************/
    public static String getNowMonth() {
        Calendar cd = Calendar.getInstance();
        int month = cd.get(Calendar.MONTH) + 1;
        if (month < 10)
            return "0" + Integer.toString(month);
        else
            return Integer.toString(month);
    }
 
    /***************************************************************************
     * 函数功能:获取当前日期号
     * 
     * 参数说明:无
     * 
     * 返回值:返回当前日期号字符串
     **************************************************************************/
    public static String getNowDay() {
        Calendar cd = Calendar.getInstance();
        int day = cd.get(Calendar.DAY_OF_MONTH);
        if (day < 10)
            return "0" + Integer.toString(day);
        else
            return Integer.toString(day);
    }
 
    /***************************************************************************
     * 函数功能:获取当前小时
     * 
     * 参数说明:无
     * 
     * 返回值:返回当前小时字符串
     **************************************************************************/
    public static String getNowHour() {
        Calendar cd = Calendar.getInstance();
        int hour = cd.get(Calendar.HOUR_OF_DAY);
        return Integer.toString(hour);
    }
 
    /***************************************************************************
     * 函数功能:获取当前分钟
     * 
     * 参数说明:无
     * 
     * 返回值:返回当前分钟字符串
     **************************************************************************/
    public static String getNowMinute() {
        Calendar cd = Calendar.getInstance();
        int minute = cd.get(Calendar.MINUTE);
        return Integer.toString(minute);
    }
 
    /***************************************************************************
     * 函数功能:获取当前秒钟
     * 
     * 参数说明:无
     * 
     * 返回值:返回当前秒钟字符串
     **************************************************************************/
    public static String getNowSecond() {
        Calendar cd = Calendar.getInstance();
        int second = cd.get(Calendar.SECOND);
        return Integer.toString(second);
    }
 
    /***************************************************************************
     * 函数功能:按照指定的时间格式,格式化时间字符串
     * 
     * 参数说明:
     * 
     * @dateString:时间字符串
     * @formatStyle:指定的时间格式
     * 
     * 返回值:返回被格式化后的日期 /
     **************************************************************************/
    public static Date formatStringToDate(String dateString, String formatStyle) {
        Date date = null;
        if (formatStyle == null || formatStyle.equals("") == true)// 如果没有指定格式,则使用默认格式
            formatStyle = DEFAULTSTYLE;
 
        try {
            if(dateString==null || dateString.trim().equals(""))
                return null;
            return new SimpleDateFormat(formatStyle).parse(dateString);
        } catch (ParseException e) {
            date = null;
            e.printStackTrace();
        }
        return date;
    }
    
    /***************************************************************************
     * 函数功能:按照指定的格式获取当前时间。
     * 
     * 参数说明:
     * 
     * @formatStyle:指定的时间格式
     * 
     * 返回值:返回指定格式的当前日期 
     **************************************************************************/
    public static String getNowDate(String formatStyle)
    {
        if(formatStyle==null || formatStyle.equals(""))
            formatStyle=DEFAULTSTYLE;
        Date now = new Date();
        SimpleDateFormat formater = new SimpleDateFormat(formatStyle);
        return formater.format(now);
    }
    
    public static String getNowDateTime()
    {
        Date now = new Date();
        SimpleDateFormat formater = new SimpleDateFormat(DATE_TIME_STYLE_All);
        return formater.format(now);
    }
    
    public static String getNowDate()
    {
        Date now = new Date();
        SimpleDateFormat formater = new SimpleDateFormat(DEFAULTSTYLE);
        return formater.format(now);
    }
 
    /***************************************************************************
     * 函数功能:比较时间迟早
     * 
     * 参数说明:
     * 
     * @date1:时间对象1
     * 
     * @date2:时间对象2
     * 
     * 返回值:如果时间对象1在时间对象2之后则返回true; 否则如果两个时间相等或时间对象1在时间对象2之前返回false;异常也返回false;
     **************************************************************************/
    public static boolean compareDate(Date date1, Date date2) {
        boolean flag = false;
        try {
            if (date1.after(date2) == true)// 如果时间对象1在时间对象2之后
                flag = true;
            else
                flag = false;
        } catch (Exception ex) {
            flag = false;
            ex.printStackTrace();
        }
        return flag;
    }
 
    // 获取某一时间段内的双休日天数
    public static int getWeekendDays(String beginDate, String endDate) {
        int count = 0;
        Date begin = formatStringToDate(beginDate, DEFAULTSTYLE);// 将起始时间字符串转换成Date类型
        Date end = formatStringToDate(endDate, DEFAULTSTYLE);// 将结束时间字符串转换成Date类型
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(begin);// 设置从起始时间开始计算
        while (true) {
            Date tempDate = calendar.getTime();// 获取当前参与判断的时间
            if (tempDate.after(end))// 如果在结束时间之后,则退出
                break;
            else
            {
                // 如果是周6或者周日
                if((calendar.get(Calendar.DAY_OF_WEEK) == 7) || (calendar.get(Calendar.DAY_OF_WEEK) == 1))
                        count++;// 计数器递增
                    calendar.add(Calendar.DATE, 1); // 日期递增
                }
            }
        return count;
    }
    
    
    //将日期字符串转换成Calendar对象
    public static Calendar convertDateStringToCalendar(String dateString)
    {
        Date date = formatStringToDate(dateString, DEFAULTSTYLE);// 将起始时间字符串转换成Date类型
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }
    
    /**
     * 将日期字符串转换成Calendar对象
     * @param dateString 时间字符串
     * @param timeStyle  格式
     * @return
     * @date 2008-12-30
     * @author huangzr
     */
    public static Calendar convertDateStringToCalendar(String dateString,String timeStyle)
    {
        if (null != dateString && !"".equals(dateString)){
            if (null == timeStyle || "".equals(timeStyle.trim())) {
                timeStyle = DEFAULTSTYLE;
            }
            Date date = formatStringToDate(dateString, timeStyle);// 将起始时间字符串转换成Date类型
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar;
        }
        return null;
    }
    
    /**
     * 获取当前日期所在的星期中的第一天(星期一)的日期
     *@param:dateString 日期字符串
     *@return:返回当前日期所在的星期中的第一天的日期
     *@throws:
     */
    public static String getFirstDayOfWeek(String dateString)
    {
        int nowweek=DateOperate.getDayOfWeek(dateString);//获取日期的是星期几
        int distance=1-nowweek;//获取与周一相差的天数
        String firstDayOfWeek=DateOperate.getAnyDate(dateString, distance);
        return firstDayOfWeek;
    }
    
    
    /**
     * 获取当前日期所在的星期中的最后一天(星期天)的日期
     *@param:dateString 日期字符串
     *@return:返回当前日期所在的星期中的最后一天的日期
     *@throws:
     */
    public static String getLastDayOfWeek(String dateString)
    {
        int nowweek=DateOperate.getDayOfWeek(dateString);//获取日期的是星期几
        int distance=7-nowweek;//获取与周末相差的天数
        String lastDayOfWeek=DateOperate.getAnyDate(dateString, distance);
        return lastDayOfWeek;
    }
    
    
    
    /**
     * 获取指定年指定月的最后一天号数(支持闰月)
     *@param:year 年
     *@param:month 月
     *@return:返回最后一天号数
     *@throws:
     */
    public static String getLastDayOfMonth(String year,String month)
    {
         Calendar   c   =   Calendar.getInstance();   
         c.set(Calendar.YEAR, Integer.parseInt(year));   
         c.set(Calendar.MONTH,Integer.parseInt(month)-1);   
         int day=c.getActualMaximum(Calendar.DAY_OF_MONTH);
         return Integer.toString(day);
    }
    
    
    
    //获取指定的日期星期数
    public static int getDayOfWeek(String dateString)
    {
        Calendar calendar=convertDateStringToCalendar(dateString);
        int week=calendar.get(Calendar.DAY_OF_WEEK);
        if(week==1) //如果是周日,则返回7;
            week=7;
        else
            --week;//减1是因为,第一天是从周日开始算的
        return week;
    }
    
    
    //获取与当前日期相距任意天数的日期
    public static String getAnyDate(String nowDate, int days)
    {
        Calendar calendar = convertDateStringToCalendar(nowDate);
        calendar.add(Calendar.DATE, days); 
        Date nextDate= calendar.getTime();
        return formatDateToString(nextDate,DEFAULTSTYLE);  
        
    }
    
    
    
    
    
    //将日期格式化成指定格式的字符串
    public static String formatDateToString(Date date,String formatStyle)
    {
        if (formatStyle == null || formatStyle.equals("") == true)// 如果没有指定格式,则使用默认格式
            formatStyle = DEFAULTSTYLE;
        SimpleDateFormat   formatter = new SimpleDateFormat(formatStyle);   
        return formatter.format(date);  
    }
 
    // 获取两个日期之间的天数差
    public static long getApartDate(String startDate, String endDate) {
        long result=0;
        try {
            Date begin = formatStringToDate(startDate, DEFAULTSTYLE);
            Date end = formatStringToDate(endDate, DEFAULTSTYLE);
            long l = end.getTime()-begin.getTime();
            result = l / (24 * 60 * 60 * 1000);
 
        } catch (Exception ex) {
            result=-1;
            ex.printStackTrace();
        }
        return result;
    }
    
    public static Date dateAdd(Date date, long day){
        long result = (long)day * 24 * 60 * 60 * 1000;
        long d = date.getTime()+result;
        return new Date(d);
    }
    
    //获取指定月份的上一个月份
    public static String getFrontMonth(int month)
    {
        int frontMonth=0;
        
        if(month>12 || month<1)//如果传入的月份是不合法的
            frontMonth=-1;
        else
        {
            if(month==1)//如果传入的月份是一月份
                frontMonth=12;//则上一个月就是12月了
            else//否则,上一个月就是当前月减1
                frontMonth=--month;
        }
        if (frontMonth < 10)
            return "0" + Integer.toString(frontMonth);
        else
            return Integer.toString(frontMonth);
    }
    
    public static String format(long ms) {//将毫秒数换算成x天x时x分x秒x毫秒
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;
 
        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
 
        String strDay = day==0?"":day+"";
        String strHour = hour < 10 ? "0" + hour : "" + hour;
        String strMinute = minute < 10 ? "0" + minute : "" + minute;
        String strSecond = second < 10 ? "0" + second : "" + second;
        String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;
        strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;
        return (strDay.equals("")?"":strDay + "天") + strHour + "小时" + strMinute + "分" + strSecond + "秒";
        }
    
    public static void main(String[] args) {
        System.out.println(DateOperate.format(512000));
    }
}