【问题标题】:Java finding difference between timesJava发现时间之间的差异
【发布时间】:2015-10-06 03:48:45
【问题描述】:

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

    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);
    }

【问题讨论】:

  • 认真的吗?使用 Java 8 的 Time API,使用 Joda Time,如果你真的非常绝望,使用 Calendar,但不要这样做!
  • 这个s4 = s2+s3;对我来说似乎很奇怪,你不应该将两点之间的差加到第三点上,而不是加两次(12:00 + 24:00)吗?

标签: java time difference


【解决方案1】:

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

Java 8

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

【讨论】:

    【解决方案2】:

    查看一些选项here

    如:

         LocalDate today = LocalDate.now()
         LocalDate yeasterday = today.minusDays(1);
         Duration oneDay = Duration.between(today, yesterday);
         Duration.between(today.atTime(0, 0), yesterday.atTime(0, 0)).toDays()   // another option
    

    【讨论】:

      【解决方案3】:
      String time1 = "08:00:00";
      String time2 = "17:00:00";
      
      SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
      Date date1 = format.parse(time1);
      Date date2 = format.parse(time2);
      long difference = date2.getTime() - date1.getTime(); 
      

      【讨论】:

        【解决方案4】:

        不使用 Java 8 或 Joda-Time,并确保不涉及本地时区 DST。

        public static void printDiff(String time1, String time2) throws ParseException {
            SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
            df.setTimeZone(TimeZone.getTimeZone("UTC"));
            int seconds = (int)(df.parse(time2).getTime() - df.parse(time1).getTime()) / 1000;
            if (seconds < 0)
                seconds += 24 * 60 * 60;
            int minutes = seconds / 60;
            int hours = minutes / 60;
            seconds %= 60;
            minutes %= 60;
            System.out.printf("There are %d hours %d minutes %d seconds from %s to %s%n",
                              hours, minutes, seconds, time1, time2);
        }
        

        测试

        public static void main(String[] args) throws Exception {
            printDiff("08:00:00", "10:00:00");
            printDiff("20:00:00", "08:00:00");
            printDiff("15:47:22", "11:12:38");
            printDiff("08:00:00", "07:59:59");
            printDiff("08:00:00", "08:00:00");
        }
        

        输出

        There are 2 hours 0 minutes 0 seconds from 08:00:00 to 10:00:00
        There are 12 hours 0 minutes 0 seconds from 20:00:00 to 08:00:00
        There are 19 hours 25 minutes 16 seconds from 15:47:22 to 11:12:38
        There are 23 hours 59 minutes 59 seconds from 08:00:00 to 07:59:59
        There are 0 hours 0 minutes 0 seconds from 08:00:00 to 08:00:00
        

        【讨论】:

          猜你喜欢
          • 2015-03-13
          • 2014-06-09
          • 2012-06-26
          • 2018-08-14
          • 2018-06-21
          • 1970-01-01
          • 1970-01-01
          • 2018-10-12
          • 2011-05-17
          相关资源
          最近更新 更多