Java中如何比较时间

1.根据Date日期类比较

先看代码,再来说明

  public static void main(String[] args) throws InterruptedException {
        long begin = System.currentTimeMillis();
        System.out.println("开始时间毫秒值:" + begin); //开始时间毫秒值:1591152816382
        Date beginDate = new Date();
        beginDate.setTime(begin);
        System.out.println("beginDate:" + beginDate); //beginDate:Wed Jun 03 10:53:36 CST 2020

        Thread.sleep(2000);

        long end = System.currentTimeMillis();
        System.out.println("结束时间毫秒值:" + end); //结束时间毫秒值:1591152818444
        Date endDate = new Date();
        endDate.setTime(end);
        System.out.println("endDate:" + endDate); //endDate:Wed Jun 03 10:53:38 CST 2020

        if (beginDate.before(endDate)) {
            long difValue = (endDate.getTime() - beginDate.getTime()) / 1000;
            System.out.println(beginDate + "比" + endDate + "早" + difValue + "秒"); //Wed Jun 03 10:53:36 CST 2020比Wed Jun 03 10:53:38 CST 2020早2秒
        }
        if (beginDate.after(endDate)) {
            long difValue = (beginDate.getTime() - endDate.getTime()) / 1000;
            System.out.println(beginDate + "比" + endDate + "晚" + difValue + "秒");
        }
        int difValue = beginDate.compareTo(endDate);
        if (difValue == -1) {
            System.out.println(beginDate + "在" + endDate + "之前"); //Wed Jun 03 10:53:36 CST 2020在Wed Jun 03 10:53:38 CST 2020之前
        } else if (difValue == 0) {
            System.out.println("时间相同");
        } else if (difValue == 1) {
            System.out.println(beginDate + "在" + endDate + "之后");
        }
    }

对应Date类型,我们可以通过其自带的API进行时间比较,主要有三种

1.1 before() 判断前边的时间是否在后边的时间之前,返回boolean值

  public boolean before(Date when) {
        return getMillisOf(this) < getMillisOf(when);
    }

1.2 after() 判断前边的时间是否在后边的时间之后,返回boolean值

  public boolean after(Date when) {
        return getMillisOf(this) > getMillisOf(when);
    }

1.3compareTo() 判断前后时间的大小,返回int值

例如1.compareTo(2) 如果1在2之前,返回-1;如果1在2之后,返回1;如果1和2时间相同,返回0

 public int compareTo(Date anotherDate) {
        long thisTime = getMillisOf(this);
        long anotherTime = getMillisOf(anotherDate);
        return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
    }

 2. 根据Calendar日历类比较

Calendar类可以看做是Date类的加强,比较方法类似,代码即可说明

public static void main(String[] args) throws InterruptedException {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;//默认从0开始
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        String currentTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
        System.out.println("系统当前时间:" + currentTime); //系统当前时间:2020-6-3 14:15:38
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date currentDate = simpleDateFormat.parse(currentTime);
            System.out.println("系统当前时间" + currentDate); //系统当前时间Wed Jun 03 14:15:38 CST 2020
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(currentDate);

            Calendar calendar2 = Calendar.getInstance();
            calendar2.add(Calendar.DAY_OF_MONTH, 1); 
            System.out.println("当前时间加上1天后的时间:" + calendar2.getTime()); //当前时间加上1天后的时间:Thu Jun 04 14:15:38 CST 2020

            long difValue = 0;
            if (calendar1.before(calendar2)) {
                difValue = (calendar2.getTime().getTime() - calendar1.getTime().getTime()) / 3600000;
                System.out.println(calendar1.getTime() + "比" + calendar2.getTime() + "早" + difValue + "小时"); //Wed Jun 03 14:15:38 CST 2020比Thu Jun 04 14:15:38 CST 2020早24小时
            }
            int value = calendar1.compareTo(calendar2);
            if (value == -1) {
                difValue = (calendar2.getTime().getTime() - calendar1.getTime().getTime()) / 3600000;
                System.out.println(calendar1.getTime() + "比" + calendar2.getTime() + "早" + difValue + "小时"); //Wed Jun 03 14:15:38 CST 2020比Thu Jun 04 14:15:38 CST 2020早24小时
            } else if (value == 1) {
                System.out.println(calendar1.getTime() + "比" + calendar2.getTime() + "晚");
            } else if (value == 0) {
                System.out.println("时间一致");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

 

posted @ 2020-06-03 14:32  狼_少_年  阅读(2728)  评论(0编辑  收藏  举报