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; } }