user1157157
user1157157

Reputation: 61

How to determine if 48 hours has passed from a specific time?

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?

Upvotes: 6

Views: 17069

Answers (8)

Frankie
Frankie

Reputation: 25165

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

Upvotes: 11

Basil Bourque
Basil Bourque

Reputation: 339422

The Joda-Time project is now in maintenance mode. The team advises migration to the java.time classes.

java.time

The modern approach uses the java.time classes.

Parse your string input of format yyyy/MM/dd hh:mm:ss. That is nearly in standard ISO 8601 format. Adjust to comply.

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.

Parse as a value in UTC.

Instant instant = Instant.parse( input ) ;

Get current moment.

Instant now = Instant.now() ;

Compare to a span of time of 48 hours.

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

Upvotes: 0

mel3kings
mel3kings

Reputation: 9405

In addition to 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:

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

Upvotes: 0

ziniestro
ziniestro

Reputation: 696

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.

Upvotes: 0

John Paul Manoza
John Paul Manoza

Reputation: 1735

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.

Upvotes: 6

Casey
Casey

Reputation: 398

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:

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:

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. 
} 

Upvotes: 2

Cuga
Cuga

Reputation: 17904

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.

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
}

Upvotes: 4

cheeken
cheeken

Reputation: 34675

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

Upvotes: 3

Related Questions