Java发现时间差异 [英] Java finding difference between times

查看:144
本文介绍了Java发现时间差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在发现时间差异时遇到了一些问题,如果我试图在今天的时间找到差异(比如t1 =08:00:00和t2 =10:00:00那么它会给出正确的输出, )但是当我试图找到差异,如t1 =20:00:00(今天的时间)和t2 =08:00:00(这是第二天早上),我希望输出为12小时但我的输出错了。请帮助我。

i have some problem while finding difference between times, if i try to find difference in todays time (say t1 = "08:00:00" and t2 = "10:00:00" then it is giving correct output,)but when i try to find the difference like t1 = "20:00:00"(which is todays time) and t2 ="08:00:00"(which is next day morning),i want the output as 12 hours but i a getting wrong outputs. kindly help me.

    String t1 = "20:00:00"; 
    String t2 = "12:00:00";
    String t3 = "24:00:00";
    SimpleDateFormat sDf = new SimpleDateFormat("HH:mm:ss");
    Date d1 = sDf.parse(t1);
    Date d2 = sDf.parse(t2);
    Date d3 = sDf.parse(t3);
    long s1,s2,s3,s4,dif1,dif2,dif3,minutes,hrs;
    dif1 = d2.getTime() - d1.getTime();
    s1 = dif1/1000;
    System.out.println("s1 "+s1);
    if(s1<0){
        dif2 = d3.getTime() - d1.getTime();
        s2 = dif2/1000;
        System.out.println("s2 "+s2);
        dif3 = d2.getTime();
        s3 = dif3/1000;
        System.out.println("s3 "+s3);
        s4 = s2+s3;
        minutes = s4 / 60;
        s4 = s4 % 60;
        hrs = minutes / 60;
        minutes = minutes % 60;
        System.out.println("time difference is : "+hrs+": "+minutes+" :"+s4);
    }else{
        minutes = s1 / 60;
        s1 = s1 % 60;
        hrs = minutes / 60;
        minutes = minutes % 60;
        System.out.println("time difference is : "+hrs+": "+minutes+" :"+s1);
    }


推荐答案

任何日期/时间操纵/计算应该通过使用定义良好且经过测试的API来完成,例如Java 8的Time API或Joda Time

Any date/time manipulation/calculation should be done though the use of well defined and tested APIs like Java 8's Time API or Joda Time

public class TestTime {

    public static void main(String[] args) {
        // Because of the ability for the time to roll over to the next
        // day, we need the date component to make sense of it, for example
        // 24:00 is actually 00:00 of the next day ... joy
        LocalDateTime t1 = LocalTime.of(20, 00).atDate(LocalDate.now());
        LocalDateTime t2 = LocalTime.of(12, 00).atDate(LocalDate.now());
        LocalDateTime t3 = LocalTime.MIDNIGHT.atDate(LocalDate.now()).plusDays(1);

        if (t1.isAfter(t2)) {
            System.out.println("Before");
            Duration duration = Duration.between(t2, t3);
            System.out.println(format(duration));
        } else {
            System.out.println("After");
            Duration duration = Duration.between(t2, t1);
            System.out.println(format(duration));
        }
    }

    public static String format(Duration duration) {
        long hours = duration.toHours();
        duration = duration.minusHours(hours);

        return String.format("%02d hours %02d minutes", hours, duration.toMinutes());
    }

}

哪些输出

12 hours 00 minutes

我在你的代码中似乎无法回答的问题是为什么你这样做 s4 = s2 + s3; ,基本上添加 12:00 24:00

The question I can't seem to answer in your code is why you did this s4 = s2+s3;, basically adding 12:00 to 24:00

这篇关于Java发现时间差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆