java计算日期之间的时间差并转为毫秒

java计算日期之间的时间差  年月日时分秒毫秒

 

package com.vfsd.core;

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

public class TestDate {
    
    public static void main(String[] args) throws Exception {
        
        String dateStr1="2021-01-10 12:49:38.156";
        String dateStr2="2021-01-10 12:49:38.468";
        String dateStr3="2021-01-10 12:49:40.116";
        String dateStr4="2021-01-10 12:49:55.456";
        String dateStr5="2021-01-10 12:51:02.376";
        
        
        
        DecimalFormat df = new DecimalFormat("#.000");
        //String str = df.format(d);
        
        
        
        long duration1 = calculateDuration(dateStr1,dateStr2);
        int dis1=(int)duration1;
        
        double indexValue1=0.000;
        int second=0;
        if(dis1<1000) {
            indexValue1=indexValue1+dis1*0.001;
        }
        System.out.println("duration1:" + indexValue1+"\n\n");
        
        
        
        
        
        long duration2 = calculateDuration(dateStr1,dateStr3);
        int dis2=(int)duration2;
        double indexValue2=0.000;
        
        if(dis2<1000) {
            indexValue2=indexValue2+dis2*0.001;
        }else {
            double second2_1=dis2/1000;
            double second2_2=dis2%1000;
            indexValue2=second2_1+second2_2*0.001;
        }
        System.out.println("duration2:" + df.format(indexValue2)+"\n\n");
        
        
        
        
        long duration3 = calculateDuration(dateStr1,dateStr4);
        int dis3=(int)duration3;
        double indexValue3=0.000;
        
        if(dis3<1000) {
            indexValue3=indexValue3+dis3*0.001;
        }else {
            double second3_1=dis3/1000;
            double second3_2=dis3%1000;
            indexValue3=second3_1+second3_2*0.001;
        }
        System.out.println("duration3:" + df.format(indexValue3)+"\n\n");
        
        
        
        
        
        
        long duration4 = calculateDuration(dateStr1,dateStr5);
        int dis4=(int)duration4;
        double indexValue4=0.000;
        
        if(dis4<1000) {
            indexValue4=indexValue4+dis4*0.001;
        }else {
            double second4_1=dis4/1000;
            double second4_2=dis4%1000;
            indexValue4=second4_1+second4_2*0.001;
        }
        System.out.println("duration4:" + df.format(indexValue4)+"\n\n");
        
        
        
    }
    
    /**计算日期时长,以毫秒为单位返回
     * @author mingliuTester
     * @param strStart 开始日期时间字符串
     * @param strEnd 结束日期时间字符串
     * @throws ParseException 
     */
    public static long calculateDuration(String strStart, String strEnd) throws ParseException{
        //设置Date格式为“年-月-日 小时:分钟:秒.毫秒”
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        
        //字符串补0
        strStart = addZero(strStart);
        strEnd = addZero(strEnd);
        
        //String转换为Date类型
        Date dateStart = sdf.parse(strStart);
        Date dateEnd = sdf.parse(strEnd);
        
        System.out.println("dateStart.getTime:" + dateStart.getTime());
        System.out.println("dateEnd.getTime:" + dateEnd.getTime());
        
        //对date的long值相减得到以毫秒为单位相减的结果,Date.getTime()获取long值
        long duration = dateEnd.getTime() - dateStart.getTime();
        return duration;
    }
    
    /**末尾补0,这里以保留小数点到后三位为例
     * @author mingliuTester
     * @param str
     */
    public static String addZero(String str){
        //计算字符串长度和点的位置差值
        int pointPosition = str.length() - str.lastIndexOf('.');
        
        //根据位置差值补0
        switch (pointPosition){
        //正好保留到小数点后3位
        case 4:
            break;
        //正好保留到小数点后2位
        case 3:
            str += "0";
        //正好保留到小数点后1位
        case 2:
            str += "00";
        //默认当做没有保留小数
        default:
            str += ".000";
            break;
        }
        
        return str;
    }


}

 

dateStart.getTime:1610254178156
dateEnd.getTime:1610254178468
duration1:0.312


dateStart.getTime:1610254178156
dateEnd.getTime:1610254180116
duration2:1.960


dateStart.getTime:1610254178156
dateEnd.getTime:1610254195456
duration3:17.300


dateStart.getTime:1610254178156
dateEnd.getTime:1610254262376
duration4:84.220

 

posted @ 2021-04-14 14:19  西北逍遥  阅读(1262)  评论(0编辑  收藏  举报