java


checking if difference between 2 date is more than 20 minutes


I have a datetime in one variable previous.Now i want to check if the previous datetime is before twenty minutes from current time.How can i do it?
Date previous = myobj.getPreviousDate();
Date now = new Date();
//check if previous was before 20 minutes from now ie now-previous >=20
How can we do it?
Use
if (now.getTime() - previous.getTime() >= 20*60*1000) {
...
}
Or, more verbose, but perhaps slightly easier to read:
import static java.util.concurrent.TimeUnit.*;
...
long MAX_DURATION = MILLISECONDS.convert(20, MINUTES);
long duration = now.getTime() - previous.getTime();
if (duration >= MAX_DURATION) {
...
}
Using Joda Time:
boolean result = Minutes.minutesBetween(new DateTime(previous), new DateTime())
.isGreaterThan(Minutes.minutes(20));
You should really use Calendar object instead of Date:
Calendar previous = Calendar.getInstance();
previous.setTime(myobj.getPreviousDate());
Calendar now = Calendar.getInstance();
long diff = now.getTimeInMillis() - previous.getTimeInMillis();
if(diff >= 20 * 60 * 1000)
{
//at least 20 minutes difference
}
Java 8 solution:
private static boolean isAtleastTwentyMinutesAgo(Date date) {
Instant instant = Instant.ofEpochMilli(date.getTime());
Instant twentyMinutesAgo = Instant.now().minus(Duration.ofMinutes(20));
try {
return instant.isBefore(twentyMinutesAgo);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
Get the times in milliseconds, and check the difference:
long diff = now.getTime() - previous.getTime();
if (diff > 20L * 60 * 1000) {
// ...
}
Another solution could be to use Joda time.

Related Links

How to adjust accelerometer data for phones held in different positions.
Invalid Content Was Found Starting With Element
private static throughout code [closed]
Scan HBase rows by ignoring a part of the start and end row in Java
Mockito.when().thenReturn() doesn't work or returns null
Java duplicates(?) answers
Selenium code to find the word count in a paragraph
How to auto-format Java code
Convert SQL query to HQL or map result to hibernate entity
Define what value to put in a Spring cache
Spring: Accessing validation group in binding result?
Implicitly Inner Class Created in Java Enum After Compiling [duplicate]
Null HttpSessionManager with HeaderHttpSessionStrategy in Spring session
Serenity framework didn`t work with Firefox 45.01
How to get a specific “row” from a DB using JPA (EclipseLink) when the primary key is Auto Incremented?
How can i copy data from one DB to another oracle DB?

Categories

HOME
log4j
wso2-am
webpack
proxy
kde
google-api-php-client
homebrew
electron
jira
setup-deployment
icloud
onelogin
django-imagekit
bs4
android-youtube-api
add
iggrid
mapserver
task
systemc
jsdoc
invantive-sql
beyondcompare
lldb
cx-freeze
graphicsmagick
tapestry
tibco-mdm
virtualdub
strncpy
elasticsearch-ruby
twilio-api
usbserial
windows-dev-center
wallpaper
overriding
stacked
hot-module-replacement
withings
azure-ml
android-tabhost
sfdc
outlook-api
qcombobox
osx-mavericks
multipeer-connectivity
lowpass-filter
csound
windows-mobile-6.5
dwscript
synchronous
infix-notation
underscore.js-templating
master-slave
python-stackless
captivenetwork
apachebench
wapiti
system.management
ios8-today-widget
vhd
lib.web.mvc
ora-00900
endeca-workbench
ionic
generic-programming
pretty-print
directoryservices
javax.mail
block-device
nstableviewcell
winddk
mdt
php-5.4
ceil
navigationservice
p4java
rebol3
hyprlinkr
srs
multiprocessor
geos
runtime.exec
bulkloader
mqx
sharp-repository
cassini-dev
free-variable
quickdialog
multipage
stage
datareader
dmx512
dentrix
nhibernate.search
photoshop-cs4
ctp4
procedural-music
putchar
mdac
signal-handling

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App