【问题标题】:Is there any utility in java 8 which can calculate the number of days or minutes or years between two dates?java 8中是否有任何实用程序可以计算两个日期之间的天数或分钟数或年数?
【发布时间】:2016-12-07 09:26:34
【问题描述】:

计算两个日期之间的年数、天数或分钟数的最佳方法是什么?

我得到了很多搜索结果,但没有一个使用 java 8 实用程序回答了这个问题。

我知道我们可以得到这两天的milis之间的差异,然后计算差异如下:

 System.currentTimeMillis() - oldDate.getTime()

但是还有其他方法可以做到这一点吗?有什么 java 8 方法可以做到这一点吗?

【问题讨论】:

    标签: date time calendar java-8


    【解决方案1】:

    我尝试使用 java.time.LocalDateTime 和 java.time.temporal.ChronoUnit。 ChronoUnit 提供了 between() 方法,可用于此类需求。 以下代码 sn-p 帮助我解决了问题:

        LocalDateTime timeNow = LocalDateTime.now();
        LocalDateTime timeAfterSometime = timeNow.plusHours(4).plusMinutes(11);
    
        System.out.println("timeNow = "+timeNow);
        System.out.println("timeAfterSometime = "+timeAfterSometime);
    
        long minutesDiff = ChronoUnit.MINUTES.between(timeNow, timeAfterSometime); // 251
        long hoursDiff = ChronoUnit.HOURS.between(timeNow, timeAfterSometime); // 4
    
        System.out.println("minutesDiff = "+minutesDiff);
        System.out.println("hoursDiff = "+hoursDiff);
    

    输出如下:

    timeNow = 2016-12-07T15:20:11.022
    timeAfterSometime = 2016-12-07T19:31:11.022
    minutesDiff = 251
    hoursDiff = 4
    

    【讨论】:

      【解决方案2】:

      你可以试试 java.time.period 类。您可以使用两个日期创建一个 period 对象,并且 period.getDays() 将为您提供天数,类似 getYears()、getMonths() 等。

      enter code here
      public static void main(String[] args) {
          LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 9, 19, 46, 45);
          LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55);
          Period period = Period.between(dob.toLocalDate(), now.toLocalDate());
          long time[] = getTime(fromDateTime, toDateTime);
          System.out.println(period.getYears() + " years " + 
                  period.getMonths() + " months " + 
                  period.getDays() + " days " +
                  time[0] + " hours " +
                  time[1] + " minutes " +
                  time[2] + " seconds.");
      
      
      }
      enter code here
      
      
      private static long[] getTime(LocalDateTime dob, LocalDateTime now) {
              LocalDateTime today = LocalDateTime.of(now.getYear(),
                      now.getMonthValue(), now.getDayOfMonth(), dob.getHour(), dob.getMinute(), dob.getSecond());
              Duration duration = Duration.between(today, now);
      
              long seconds = duration.getSeconds();
      
              long hours = seconds / SECONDS_PER_HOUR;
              long minutes = ((seconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE);
              long secs = (seconds % SECONDS_PER_MINUTE);
      
              return new long[]{hours, minutes, secs};
          }
      

      【讨论】:

      • 但是我怎么才能得到两者之间的区别呢?你身边的任何代码sn-p?
      • LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 9, 19, 46, 45); LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55);期间 period = getPeriod(fromDateTime, toDateTime);长时间[] = getTime(fromDateTime, toDateTime); System.out.println(period.getYears() + "years" + period.getMonths() + "months" + period.getDays() + "days" + time[0] + " hours" + time[1] + “分钟”+时间[2]+“秒”); }
      猜你喜欢
      • 2023-03-29
      • 1970-01-01
      • 2013-11-10
      • 2015-01-16
      • 2021-07-08
      • 1970-01-01
      • 2013-12-08
      相关资源
      最近更新 更多