原文地址:https://www.yundashi168.com/396.html
输入:
System.out.println("时间差:"+getDistanceTime("2016-05-01 12:00:00:000","2016-05-01 12:50:02:157"));
打印:(其它天,时,分都可以打印)
public static String getDistanceTime(String str1, String str2) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
Date one;
Date two;
long day = 0;
long hour = 0;
long min = 0;
long second=0;
long diff=0 ;
String result = null;
try {
final Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
one = df.parse(str1);
c.setTime(one);
two = df.parse(str2);
long time1 = one.getTime();
long time2 = two.getTime();
diff = time2 - time1;
day = diff / (24 * 60 * 60 * 1000);
hour = (diff / (60 * 60 * 1000) - day * 24);
min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
second = diff/1000;
System.out.println("day="+day+" hour="+hour+" min="+min+" ss="+second%60+" SSS="+diff%1000);
result=second%60+"秒"+diff%1000+"毫秒";
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
|