智慧保安后台管理-外网
Administrator
2022-06-16 8b375fe00a241b3a769b82fe3dac8d1c9dce8a02
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
package org.springblade.common.utils;
 
import bsh.ParseException;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * 时间装换
 * @author zhongrj
 */
public class TimeSwitchUtil {
    public static void main(String[] args) {
        String time1 = secondToTime(85959);
        String time2 = dateToTime("2017/10/25 16:42:46", "yyyy/MM/dd HH:mm:ss");
        System.out.println(time1);
        System.out.println(time2);
    }
 
    /**
     * 将秒数转换为日时分秒,
     * @param second
     * @return
     */
    public static String secondToTime(long second){
        //转换天数
        long days = second / 86400000;
        //剩余秒数
        second = second % 86400000;
        //转换小时
        long hours = second / 3600000;
        //剩余秒数
        second = second % 3600000;
        //转换分钟
        long minutes = second /60000;
        //剩余秒数
        second = second % 60000;
        //装换秒
        long s = second /1000;
        if(days>0){
            return days + "天" + hours + "小时" + minutes + "分" + s + "秒";
        }else{
            return hours + "小时" + minutes + "分" + s + "秒";
        }
    }
 
    /**
     * 将日期转换为日时分秒
     * @param date
     * @return
     */
    public static String dateToTime(String date, String dateStyle){
        SimpleDateFormat format = new SimpleDateFormat(dateStyle);
        Date oldDate = null;
        try {
            oldDate = format.parse(date);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        //输入日期转换为毫秒数
        long time = oldDate.getTime();
        long nowTime = System.currentTimeMillis();        //当前时间毫秒数
        long second = nowTime - time;                    //二者相差多少毫秒
        second = second / 1000;                            //毫秒转换为妙
        long days = second / 86400;
        second = second % 86400;
        long hours = second / 3600;
        second = second % 3600;
        long minutes = second /60;
        second = second % 60;
        if(days>0){
            return days + "天" + hours + "小时" + minutes + "分" + second + "秒";
        }else{
            return hours + "小时" + minutes + "分" + second + "秒";
        }
    }
}