如何判断特定时间过去48小时?

时间:2012-01-18 20:54:13

标签: java time

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

8 个答案:

答案 0 :(得分:7)

不确定。 我强烈建议你选择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) ...

答案 1 :(得分:6)

我能够通过在我的项目中使用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。

答案 2 :(得分:4)

您可以在指定日期添加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
}

答案 3 :(得分:3)

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

答案 4 :(得分:1)

如果您不需要处理特殊情况,可以使用.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. 
} 

答案 5 :(得分:0)

您可以使用以下内容:

 DateTime specificDate = new DateTime(" some specific date");
 DateTime now = new DateTime();

 if(TimeUnit.MILLISECONDS.toHours(now.getMillis() - specificDate.getMillis()) > 48) {
    //Do something
  }

希望这可以帮到你。

答案 6 :(得分:0)

除了 Joda DateTime

如果你想检查一个日期是否介于另一个时间范围之间,比如date1之间的date2 4小时,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

答案 7 :(得分:0)

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 ) ) { … }