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

I want to check and see if 48 hours are inserted from a certain time? I use this date format (yyyy / MM / dd hh: mm: ss). Is there any java function for this?

+4
source share
8 answers

Sure. I would strongly advise you to pick a Joda DateTime . As @Basil Bourque said in a comment, Joda is now in maintenance mode, and with Java 8 you have to use java.time methods.

The current proposed code, which is library independent, and more clearly what it does:

// How to use Java 8 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 library independent):

 // 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) ... 
+6
source

I was able to accomplish this using the 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. Now it will be easier for you. This will return false.

+6
source

You can add 48 hours to a given date, and then if the result is earlier than the start date, you know that 48 hours have passed.

 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
source
 Date twoDaysAgo = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 2); Date parsedDate = new SimpleDateFormat("y/M/dh:m:s").parse(myDateString); boolean hasTimePasssed = parsedDate.before(twoDaysAgo); 
+3
source

If you don't need to handle special cases, you can use the .before () function and compose 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 would not add a library dependency on something so simple, but if you were going to use JodaTime, I would use their convenience methods instead of calculating the time offset, as in another 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. } 
+1
source

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 helps you.

0
source

In addition to Joda DateTime

If you want to check if someone says that the date is between another time interval, say date1 4 hours between date2 , joda has different classes only for those scenarios that 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

0
source

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

java.time

The modern approach uses java.time classes.

Parse string input in yyyy/MM/dd hh:mm:ss format. It is almost in the standard ISO 8601 format. Adjust the match.

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

If at all possible, use standard formats, and don't come up with your own when exchanging date and time values ​​as text. The java.time classes use standard default formats when parsing and generating strings.

Parse as a value in UTC format.

 Instant instant = Instant.parse( input ) ; 

Get the current moment.

 Instant now = Instant.now() ; 

Compare with a period of 48 hours.

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

Source: https://habr.com/ru/post/1391705/


All Articles