I looking to check and see if 48 hours has pasted from a specific time? I am using this date time format (yyyy/MM/dd hh:mm:ss) Is there any java function for this?
我想检查并查看特定时间是否粘贴了48小时?我正在使用这个日期时间格式(yyyy / MM / dd hh:mm:ss)这是否有任何java函数?
8 个解决方案
#1
6
I was able to accomplish this by using a JodaTime Library in my project. I came out with this code.
我能够通过在我的项目中使用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;
}
This will check if the Time Interval between datetime1 and datetime2 is GreaterThan 20 Minutes. Change the property to Days. It will be easier for you now. This will return false.
这将检查datetime1和datetime2之间的时间间隔是否大于20分钟。将属性更改为天。现在对你来说会更容易。这将返回false。
#2
6
Sure. I would strongly advice you to pick up Joda DateTime. As @Basil Bourque put it in a comment, Joda is now in maintenance mode and since Java 8 you should use the java.time methods.
当然。我强烈建议你选择Joda DateTime。正如@Basil Bourque在评论中所说,Joda现在处于维护模式,从Java 8开始,你应该使用java.time方法。
Current suggested code that's not library dependent and is more clear on what it does:
当前建议的代码不依赖于库,并且更清楚它的作用:
// 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);
Original suggested code (also not library dependent):
原始建议代码(也不依赖于库):
// 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) ...
#3
4
You can add 48 hours to a given date, and then if the result is earlier than your starting date, you know you're past 48 hours.
您可以在指定日期添加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
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);
#5
1
If you don't need to handle special cases, you can use the .before() function and make up a date object to represent 48 hours ago:
如果您不需要处理特殊情况,可以使用.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.
}
EDIT: I wouldn't add a library dependency for something so simple, but if you're going to use JodaTime, I would use their convenience methods rather than calculate the time offset as in the other answer:
编辑:我不会为这么简单的东西添加库依赖项,但是如果你要使用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
0
You can use the following:
您可以使用以下内容:
DateTime specificDate = new DateTime(" some specific date");
DateTime now = new DateTime();
if(TimeUnit.MILLISECONDS.toHours(now.getMillis() - specificDate.getMillis()) > 48) {
//Do something
}
Hope this can help you.
希望这可以帮到你。
#7
0
In addition to Joda DateTime
除了Joda DateTime
If you want to check if say one date is in between another time frame, say is date1
4hrs in between date2
, joda has different classes just for those scenarios you can use:
如果你想检查一个日期是否在另一个时间框架之间,比如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
http://joda-time.sourceforge.net/apidocs/org/joda/time/base/BaseSingleFieldPeriod.html
#8
0
The Joda-Time project is now in maintenance mode. The team advises migration to the java.time classes.
Joda-Time项目现在处于维护模式。该团队建议迁移到java.time类。
java.time
The modern approach uses the java.time classes.
现代方法使用java.time类。
Parse your string input of format yyyy/MM/dd hh:mm:ss
. That is nearly in standard ISO 8601 format. Adjust to comply.
解析格式为yyyy / MM / dd hh:mm:ss的字符串输入。这几乎是标准的ISO 8601格式。调整以符合要求。
String input = "2017/01/23 12:34:56".replace( "/" , "-" ).replace( " " , "T" ).concat( "Z" ) ;
If at all possible, use the standard formats rather than invent your own when exchanging date-time values as text. The java.time classes use the standard formats by default when parsing and generating strings.
如果可能的话,使用标准格式而不是在将日期时间值作为文本交换时创建自己的格式。在解析和生成字符串时,java.time类默认使用标准格式。
Parse as a value in UTC.
解析为UTC中的值。
Instant instant = Instant.parse( input ) ;
Get current moment.
获取当前时刻。
Instant now = Instant.now() ;
Compare to a span of time of 48 hours.
比较48小时的时间跨度。
Duration d = Duration.ofHours( 48 ) ;
if ( instant.plus( d ).isBefore( now ) ) { … }
#1
6
I was able to accomplish this by using a JodaTime Library in my project. I came out with this code.
我能够通过在我的项目中使用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;
}
This will check if the Time Interval between datetime1 and datetime2 is GreaterThan 20 Minutes. Change the property to Days. It will be easier for you now. This will return false.
这将检查datetime1和datetime2之间的时间间隔是否大于20分钟。将属性更改为天。现在对你来说会更容易。这将返回false。
#2
6
Sure. I would strongly advice you to pick up Joda DateTime. As @Basil Bourque put it in a comment, Joda is now in maintenance mode and since Java 8 you should use the java.time methods.
当然。我强烈建议你选择Joda DateTime。正如@Basil Bourque在评论中所说,Joda现在处于维护模式,从Java 8开始,你应该使用java.time方法。
Current suggested code that's not library dependent and is more clear on what it does:
当前建议的代码不依赖于库,并且更清楚它的作用:
// 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);
Original suggested code (also not library dependent):
原始建议代码(也不依赖于库):
// 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) ...
#3
4
You can add 48 hours to a given date, and then if the result is earlier than your starting date, you know you're past 48 hours.
您可以在指定日期添加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
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);
#5
1
If you don't need to handle special cases, you can use the .before() function and make up a date object to represent 48 hours ago:
如果您不需要处理特殊情况,可以使用.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.
}
EDIT: I wouldn't add a library dependency for something so simple, but if you're going to use JodaTime, I would use their convenience methods rather than calculate the time offset as in the other answer:
编辑:我不会为这么简单的东西添加库依赖项,但是如果你要使用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
0
You can use the following:
您可以使用以下内容:
DateTime specificDate = new DateTime(" some specific date");
DateTime now = new DateTime();
if(TimeUnit.MILLISECONDS.toHours(now.getMillis() - specificDate.getMillis()) > 48) {
//Do something
}
Hope this can help you.
希望这可以帮到你。
#7
0
In addition to Joda DateTime
除了Joda DateTime
If you want to check if say one date is in between another time frame, say is date1
4hrs in between date2
, joda has different classes just for those scenarios you can use:
如果你想检查一个日期是否在另一个时间框架之间,比如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
http://joda-time.sourceforge.net/apidocs/org/joda/time/base/BaseSingleFieldPeriod.html
#8
0
The Joda-Time project is now in maintenance mode. The team advises migration to the java.time classes.
Joda-Time项目现在处于维护模式。该团队建议迁移到java.time类。
java.time
The modern approach uses the java.time classes.
现代方法使用java.time类。
Parse your string input of format yyyy/MM/dd hh:mm:ss
. That is nearly in standard ISO 8601 format. Adjust to comply.
解析格式为yyyy / MM / dd hh:mm:ss的字符串输入。这几乎是标准的ISO 8601格式。调整以符合要求。
String input = "2017/01/23 12:34:56".replace( "/" , "-" ).replace( " " , "T" ).concat( "Z" ) ;
If at all possible, use the standard formats rather than invent your own when exchanging date-time values as text. The java.time classes use the standard formats by default when parsing and generating strings.
如果可能的话,使用标准格式而不是在将日期时间值作为文本交换时创建自己的格式。在解析和生成字符串时,java.time类默认使用标准格式。
Parse as a value in UTC.
解析为UTC中的值。
Instant instant = Instant.parse( input ) ;
Get current moment.
获取当前时刻。
Instant now = Instant.now() ;
Compare to a span of time of 48 hours.
比较48小时的时间跨度。
Duration d = Duration.ofHours( 48 ) ;
if ( instant.plus( d ).isBefore( now ) ) { … }