Java工具模块:时间间隔计算

1 包

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

2 时间间隔计算

2.1 计算总间隔

场景:计算间隔总时间.

  • 总天数
    间隔总天数,满24小时,加1天,不满24小时,不计,取整.
  • 总小时数
    间隔总小时数,满60分,加1小时,不满60分钟,不计,取整.
  • 总分钟数
    间隔总分钟数,满60秒,加1分钟,不满60秒,不计,取整.
package basic.datatype.datatest;

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

/**
 * @author xindaqi
 * @since 2020-08-20
 */
public class SimpleDateFormatTest {

    public static void main(String[] args){
        // First method.
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // Time String.
            String startTime = "2020-08-10 10:10:10";
            String endTime = "2020-08-12 09:10:12";
            // Date.
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
            // Time difference: milliseconds. 
            long diffFirst = endDate.getTime() - startDate.getTime();
            // Milliseconds perday: 24*60*60*1000
            long millisecondsPerDay = 24 * 60 * 60 * 1000;
            // Milliseconds per hour: 60*60*100
            long millisecondsPerHour = 60 * 60 * 1000;
            // Milliseconds per minute: 60 * 1000
            long millisecondsPerMinute = 60 * 1000;
            // Total days difference.
            long day = diffFirst / millisecondsPerDay;
            // Total hours difference.
            long hour = diffFirst / millisecondsPerHour;
            // Total minutes differenc.
            long min = diffFirst / millisecondsPerMinute;
            System.out.println("总共相差:" + day + "天" +
            "\n" + "总共相差:" + hour + "小时" + 
            "\n" + "总共相差:" + min + "分钟");
            System.out.println("Different Day:" + day + 
            "\n" + "Different Hours:" + hour + 
            "\n"+"Different Minutes:"+min);

        }catch(Exception e){
            e.printStackTrace();
        }
       
    }
    
}
  • 结果
总共相差:1天
总共相差:47小时
总共相差:2820分钟
Different Day:1
Different Hours:47
Different Minutes:2820

2.2 各自间隔

场景:计算相差的时间,格式化为相差:天:时:分

  • 相差天数
    满24小时加1天,不满24小时算作小时,如49小时,2天,取整.
  • 相差小时数
    满60分钟加1小时,不满60分钟,算作分钟,如90分钟,1小时,取整.
  • 相差分钟数
    满60秒加1分钟,不满60秒,算作秒,如90秒,1分钟,取整.
package basic.datatype.datatest;

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

/**
 * @author xindaqi
 * @since 2020-08-20
 */
public class SimpleDateFormatTest {

    public static void main(String[] args){
        // Second method.
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // Time String.
            String startTime = "2020-08-10 10:10:10";
            String endTime = "2020-08-12 09:10:12";
            // Date.
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
            // Time difference: milliseconds. 
            long diffSecond = endDate.getTime() - startDate.getTime();
            // Milliseconds perday: 24*60*60*1000
            long millisecondsPerDay = 24 * 60 * 60 * 1000;
            // Milliseconds per hour: 60*60*100
            long millisecondsPerHour = 60 * 60 * 1000;
            // Milliseconds per minute: 60 * 1000
            long millisecondsPerMinute = 60 * 1000;
            // Milliseconds per seconds: 1000
            long millisecondsPerSecond = 1000;
            long days = diffSecond / millisecondsPerDay;
            long hours = (diffSecond % millisecondsPerDay) / millisecondsPerHour;
            long minutes = (diffSecond % millisecondsPerHour) / millisecondsPerMinute;
            long seconds = (diffSecond % millisecondsPerMinute) / millisecondsPerSecond;
            System.out.println("相差:" + days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒");
            System.out.println("Different Days:" + days +
            "\n" + "Different Hours:" + hours +
            "\n" + "Different Minutes:" + minutes + 
            "\n" + "Different seconds:" + seconds);
            
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
}
  • 结果
相差:1天23小时0分钟2秒
Different Days:1
Different Hours:23
Different Minutes:0
Different seconds:2

3 小结

  • SimpleDateFormat将特定格式转化为时间对象,通过parse方法解析对应格式的时间字符串.
  • Date的getTime()方法计算时间的毫秒数,时间单位:ms.
  • 时间差为毫秒,计算时将毫秒转化为秒,1s=1000ms.

你可能感兴趣的:(#,Java,ABC,时间间隔,Date,SDF)