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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM