【问题标题】:How to determine if 48 hours has passed from a specific time?如何确定是否从特定时间过去了 48 小时?
【发布时间】:2012-01-18 20:54:12
【问题描述】:

我想查看是否已从特定时间粘贴了 48 小时? 我正在使用这种日期时间格式(yyyy/MM/dd hh:mm:ss) 有没有这个的 java 函数?

【问题讨论】:

标签: java time


【解决方案1】:

当然。 我强烈建议您接听Joda DateTime。正如@Basil Bourque 在评论中所说,Joda 现在处于维护模式,从 Java 8 开始,您应该使用 java.time 方法。

当前建议的代码不依赖于库,并且更清楚它的作用:

// How to use Java 8's time utils to calculate hours between two dates
LocalDateTime dateTimeA = LocalDateTime.of(2017, 9, 28, 12, 50, 55, 999);
LocalDateTime dateTimeB = LocalDateTime.of(2017, 9, 30, 12, 50, 59, 851);
long hours = ChronoUnit.HOURS.between(dateTimeA, dateTimeB);
System.out.println(hours);

原始建议代码(也不依赖于库):

// pseudo-code
DateTime a = new DateTime("old time");
DateTime b = new DateTime(" now    ");

// get hours
double hours = (a.getMillis() - b.getMillis()) / 1000 / 60 / 60;
if(hours>48) ...

【讨论】:

  • 我同意使用 Joda DateTime 是最好的;否则你可以使用本机日历(但 Joda DateTime 更简单)。
  • 仅供参考,Joda-Time 项目现在处于维护模式,团队建议迁移到 java.time 类。
  • @BasilBourque 感谢更新。我已经相应地编辑了答案。
【解决方案2】:

我能够通过在我的项目中使用 JodaTime 库来实现这一点。我得到了这段代码。

String datetime1 = "2012/08/24 05:22:34";
String datetime2 = "2012/08/24 05:23:28";

DateTimeFormatter format = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss");
DateTime time1 = format.parseDateTime(datetime1);
DateTime time2 = format.parseDateTime(datetime2);
Minutes Interval = Minutes.minutesBetween(time1, time2);
Minutes minInterval = Minutes.minutes(20);

if(Interval.isGreaterThan(minInterval)){
  return true;
}
else{
  return false;
}

这将检查 datetime1 和 datetime2 之间的时间间隔是否大于 20 分钟。将属性更改为天。现在对你来说会更容易。这将返回 false。

【讨论】:

    【解决方案3】:

    您可以将 48 小时添加到给定日期,然后如果结果早于您的开始日期,您就知道您已经过了 48 小时。

    Date dateInQuestion = getDateInQuestion();
    
    Calendar cal = Calendar.getInstance();
    cal.setTime(dateInQuestion);
    cal.add(Calendar.HOUR, 48);
    Date futureDate = cal.getTime();
    
    if (dateInQuestion.after(futureDate)) {
      // Then more than 48 hours have passed since the date in question
    }
    

    【讨论】:

      【解决方案4】:
      Date twoDaysAgo = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 2);
      Date parsedDate = new SimpleDateFormat("y/M/d h:m:s").parse(myDateString);
      boolean hasTimePasssed = parsedDate.before(twoDaysAgo);
      

      【讨论】:

        【解决方案5】:

        如果不需要处理特殊情况,可以使用.before()函数,组成一个日期对象来表示48小时前:

        long millisIn48Hours = 1000 * 60 * 60 * 48;
        Date timestamp = new Date(0);//use the date you have, parse it using SimpleDateFormat if needed.
        Date hours48ago = new Date(new Date().getTime() - millisIn48Hours);
        
        if (timestamp.before(hours48ago)) {
            //48 hours has passed.
        }
        

        编辑:我不会为这么简单的事情添加库依赖项,但如果你要使用 JodaTime,我会使用他们的便捷方法,而不是像其他答案那样计算时间偏移:

        DateTime original = new DateTime("your original date object");
        DateTime now = new DateTime();
        DateTime minus48 = now.minusHours(48);
        
        if (original.isBefore(minus48)) {
            //48 hours has elapsed. 
        } 
        

        【讨论】:

          【解决方案6】:

          您可以使用以下内容:

           DateTime specificDate = new DateTime(" some specific date");
           DateTime now = new DateTime();
          
           if(TimeUnit.MILLISECONDS.toHours(now.getMillis() - specificDate.getMillis()) > 48) {
              //Do something
            }
          

          希望对你有帮助。

          【讨论】:

            【解决方案7】:

            除了Joda DateTime

            如果你想检查一个日期是否在另一个时间范围之间,比如date1 4hrs 在date2 之间,joda 有不同的类只适用于你可以使用的那些场景:

            Hours h = Hours.hoursBetween(date1, date2);
            Days s = Days.daysBetween(date1, date2);
            Months m = Months.monthsBetween(date1,date2);
            

            http://joda-time.sourceforge.net/apidocs/org/joda/time/base/BaseSingleFieldPeriod.html

            【讨论】:

            • 仅供参考,Joda-Time 项目现在处于维护模式。团队建议迁移到 java.time 类。
            【解决方案8】:

            Joda-Time 项目现在处于维护模式。团队建议迁移到 java.time 类。

            java.time

            现代方法使用 java.time 类。

            解析格式为yyyy/MM/dd hh:mm:ss 的字符串输入。这几乎是标准 ISO 8601 格式。调整以符合要求。

            String input = "2017/01/23 12:34:56".replace( "/" , "-" ).replace( " " , "T" ).concat( "Z" ) ;
            

            如果可能,在将日期时间值交换为文本时,请使用标准格式,而不是自己发明。 java.time 类在解析和生成字符串时默认使用标准格式。

            解析为 UTC 值。

            Instant instant = Instant.parse( input ) ;
            

            获取当前时刻。

            Instant now = Instant.now() ;
            

            与 48 小时的时间跨度进行比较。

            Duration d = Duration.ofHours( 48 ) ;
            if ( instant.plus( d ).isBefore( now ) ) { … }
            

            【讨论】:

              猜你喜欢
              • 2015-09-17
              • 2020-11-05
              • 1970-01-01
              • 1970-01-01
              • 2018-10-29
              • 1970-01-01
              • 2016-11-17
              • 1970-01-01
              • 2012-02-03
              相关资源
              最近更新 更多