专注Java教育14年 全国咨询/投诉热线:444-1124-454
赢咖4LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 Java时间差的计算

Java时间差的计算

更新时间:2022-10-31 10:13:21 来源:赢咖4 浏览2067次

Java 计算时间差

1. Date 计算时间差

2. 两 LocalDate 相差年份,返回Integer类型

3. LocalDateTime 计算时间差

Date 计算时间差

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatDemo {
    public static void main(String[] args) {
        //设置时间格式,为了 能转换成 字符串
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //当前时间
        Date beginTime = new Date();
        //利用时间格式,把当前时间转为字符串
        String start = df.format(beginTime);
        //当前时间 转为 长整型 Long
        Long begin = beginTime.getTime();
        System.out.println("任务开始,开始时间为:"+ start);
        int num;
        //循环睡眠 5次,每次 1秒
        for(int i = 0; i < 5 ; i++){
            num = i +1;
            try {
                //调阻塞(睡眠)方法,这里睡眠 1 秒
                Thread.sleep(1000);
                System.out.println(num+"秒");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //获取结束时间
        Date finishTime = new Date();
        //结束时间 转为 Long 类型
        Long end = finishTime.getTime();
        // 时间差 = 结束时间 - 开始时间,这样得到的差值是毫秒级别
        long timeLag = end - begin;
        //天
        long day=timeLag/(24*60*60*1000);
        //小时
        long hour=(timeLag/(60*60*1000)-day*24);
        //分钟
        long minute=((timeLag/(60*1000))-day*24*60-hour*60);
        //秒,顺便说一下,1秒 = 1000毫秒
        long s=(timeLag/1000-day*24*60*60-hour*60*60-minute*60);
        System.out.println("用了 "+day+"天 "+hour+"时 "+minute+"分 "+s+"秒");
        System.out.println("任务结束,结束时间为:"+ df.format(finishTime));
    }
}

两 LocalDate 相差年份,返回Integer类型

/**
     * 传过来的LocalDate类型的日期,距当前时间,相差多少年
     * 可计算年龄,工龄等
     * 返回Integer类型的数
     */
    public static Integer getYear(LocalDate date){
        //传过来的日期
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy");
        String dateStr = df.format(date);
        Long yearLong = Long.parseLong(dateStr);
        //当前日期
        LocalDate yearNow = LocalDate.now();
        String yearNowStr = df.format(yearNow);
        Long yearNowLong = Long.parseLong(yearNowStr);
        //当前 - 传过来的参数
        long age = yearNowLong - yearLong;
        Integer year = Math.toIntExact(age);
        return year;
    }

LocalDateTime 计算时间差

import lombok.extern.slf4j.Slf4j;
import java.time.Duration;
import java.time.LocalDateTime;
/**
 * @author 孙永潮
 * @date 2022/8/25
 */
@Slf4j
public class LocalDateTimeDemo {
    public static void main(String[] args) {
        //开始时间
        LocalDateTime startTime = LocalDateTime.now();        
        int num;
        //循环睡眠 3次,每次 1秒
        for(int i = 0; i < 3 ; i++){
            num = i +1;
            try {
                //调阻塞(睡眠)方法,这里睡眠 1 秒
                Thread.sleep(1000);
                System.out.println(num+"秒");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }        
        //结束时间
        LocalDateTime endTime  = LocalDateTime.now();        
        // 获得两个时间之间的相差值
        Duration dur= Duration.between(startTime, endTime );
        //两个时间差的分钟数
        long minute = dur.toMinutes();
        //纳秒
        dur.toNanos();
        //毫秒
        long millisecond = dur.toMillis();
        //秒  ( 1秒 = 1000毫秒 )
        long s = dur.toMillis()/1000;
        //分钟
        dur.toMinutes();
        //小时
        dur.toHours();
        //天数
        dur.toDays();
        log.info("用了 {}分", minute);
        log.info("用了 {}秒", s);
        log.info("用了 {}毫秒", millisecond);
    }
}

 

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>