yzt
2023-05-26 2f70f6727314edd84d8ec2bfe3ce832803f1ea77
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
package com.hxzkoa.udp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**该类用于时间计算*/
public class TiemDell {
    
    private static final String aa = "yyyy-MM-dd HH:mm:ss";
     /**获取两个时间相差毫秒数*/
    public static long getTime(String oldTime,String newTime)  { 
      SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss.SSS");
      long NTime = 0;
      long OTime = 0;
        try {
            NTime = df.parse(newTime).getTime();
            OTime = df.parse(oldTime).getTime();
        } catch (ParseException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
      
      long diff=(NTime-OTime);
      return diff;
  }
    
    public static int getTimeDelta(Date date1,Date date2){
        long timeDelta=(date1.getTime()-date2.getTime())/1000;//单位是秒
        int secondsDelta=timeDelta>0?(int)timeDelta:(int)Math.abs(timeDelta);
        return secondsDelta;
    }
    
    /**获取两个时间相差秒数*/
    public static int getTimeDelta(String stoptime,String Startime){
        Date date1=parseDateByPattern(stoptime, aa);
        Date date2=parseDateByPattern(Startime, aa);
        return getTimeDelta(date1, date2);
    }
   
    
    public static Date parseDateByPattern(String dateStr,String dateFormat){
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
 
}