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?
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.
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) ...
I was able to accomplish this by using a JodaTime Library in my project. I came out with this code.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With