Java计算两个日期之间的时间差(毫秒数)

Java计算两个日期之间的时间差

package com.test1;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GetTimeDistance {
    
    public static void main(String[] args) throws ParseException {
        Date date1 = new Date();
        Date date2 = new Date();
        
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss.SSS");  
        
    
        String d_1w_ifc_start = "2020/01/11 17:21:36.645";
        String d_1w_ifc_end = "2020/01/11 17:22:51.347";

        String d_1w_mysql_start = "2020/01/11 17:22:51.347";
        String d_1w_mysql_end = "2020/01/11 17:22:51.716";
        System.out.println(getTime(d_1w_ifc_start,d_1w_ifc_end)+"  "+getTime(d_1w_mysql_start,d_1w_mysql_end));
        
        
    }
    
    // 获取两个时间相差分钟数
    public static long getTime(String oldTime,String newTime) throws ParseException {
 
        SimpleDateFormat df = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss.SSS");
        long NTime =df.parse(newTime).getTime();
        //从对象中拿到时间
        long OTime = df.parse(oldTime).getTime();
        long diff=(NTime-OTime);
        return diff;
    }

}

 

74702    369

posted @ 2020-01-11 17:30  西北逍遥  阅读(23338)  评论(0编辑  收藏  举报