【问题标题】:How can i check the difference in minutes when comparing current date with the previous date将当前日期与前一个日期进行比较时,如何检查以分钟为单位的差异
【发布时间】:2015-07-29 12:16:27
【问题描述】:

我有这样格式的日期:2015-07-29 16:29:32

如何检查当前日期和给定日期之间的分钟差?

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Test {
    public static void main(String[] args) throws ParseException {
        String datetocomparestr = "2015-07-29 16:29:32";
        SimpleDateFormat datetocomparesdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");  
        Date d1 = null;
          d1 = datetocomparesdf.parse(datetocomparestr);
          System.out.println(d1);
          SimpleDateFormat dateFormatcurrentsdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
          Date date = new Date();
          System.out.println(dateFormatcurrentsdf.format(date));

    }
}

请告诉我如何解决这个问题。提前致谢。

【问题讨论】:

标签: java


【解决方案1】:

一个简单的解决方案是使用Date.getTime() 方法(返回“自 1.1.1970 以来的毫秒数”),获取这些值的差并将该值除以 1000 * 60(1000 毫秒每秒,每分钟 60 秒)。

【讨论】:

    【解决方案2】:

    为什么这么复杂?只需使用java的long表示来确定时间:

    long timeToComp = parseSomeTime().getTime();
    long timeCurrent = System.currentTimeMillis();
    
    long dif = timeCurrent - timeToComp;
    long mins = dif / (1000 * 60);//dif is the number of milliseconds between the current date and the parsed one
    

    【讨论】:

      【解决方案3】:

      您可以使用localdatetime,如下所示

      LocalDateTime d1=new LocalDateTime(date1);
      LocalDateTime d2=new LocalDateTime(now);
      
      int minutesDiff=Minutes.minutesBetween(d1, d2).getMinutes();
      

      【讨论】:

        【解决方案4】:
        double minutes = (date.getTime() - d1.getTime()) / (1000 * 60);
        

        【讨论】:

          【解决方案5】:

          Date#getTime() 在 Java 中以毫秒为单位返回时间。

          将以下内容添加到您的代码中。

           long minutesDiff = (date.getTime() - d1.getTime()) / 1000 / 60;    //convert to minutes
           System.out.println("Difference in minutes : " + minutesDiff);
          

          看看这个直播demo

          这是完整的代码。

          import java.text.ParseException;
          import java.text.SimpleDateFormat;
          import java.util.Date;
          
          
          public class Test {
              public static void main(String[] args) throws ParseException {
                  String datetocomparestr = "2015-07-29 16:29:32";
                  SimpleDateFormat datetocomparesdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");  
                  Date d1 = null;
                  d1 = datetocomparesdf.parse(datetocomparestr);
                  System.out.println(d1);
                  SimpleDateFormat dateFormatcurrentsdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
                  Date date = new Date();
                  System.out.println(dateFormatcurrentsdf.format(date));
          
                  long minutesDiff = (date.getTime() - d1.getTime()) / 1000 / 60;
                  System.out.println("Difference in minutes : " + minutesDiff);
          
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-07-24
            • 2017-05-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多