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 use lazy property loading with glassfish + toplink essentials
Gang-of-four pattern usage in aspect-oriented-programming?
Is there a Java package to read the UNIX /etc/group file?
virtual listbox in Swing
Python-style integer division & modulus in C
Authentication using URI userinfo component http://userinfo#hostname:port/path in a servlet
SAX Premature End to a Parse?
Hibernate Annotations, specifying which members of a component to make persistent
Interfacing Java and Quartz Composer
Can an EJB3 bean “self inject” and call its own methods via EJB container?
Selenium click() event seems not to be always triggered => results in timeout?
Changing struts-rest idParameterName per action class
Strange syntax for instantiating an inner class
How to match SOAP responses to the schema
Splitting up the drawing of a GUI Element
How can you force recompilation of jsps in JBoss 4.2?

Categories

HOME
netsuite
netbeans
path-finding
bookshelf.js
cvs
at-command
paw-app
frameworks
jpeg
ravendb
enterprise-library-5
mouse
webrequest
esper
adfs
session-variables
windows-10-universal
apache-cayenne
jplayer
designer
ab-testing
shared-hosting
visual-composer
telerik-reporting
microsoft-r
su
selectedindexchanged
chromebook
applozic
oracle-fusion-middleware
repo
normal-distribution
suricata
vxworks
squib
mapzen
ruby-on-rails-3.1
vao
import-from-excel
dotcover
zip4j
pubmed
dds
boost-multi-index
jlink
powercli
crypt
snmptrapd
np-complete
xcb
firmata
static-ip-address
httplistener
sts-springsourcetoolsuite
nsfilemanager
impresspages
hill-climbing
superstack
captivenetwork
dataview
ipconfig
srand
teamcity-8.0
endeca-workbench
eclipse-clp
atk4
fouc
iis-arr
pundit
browser-bugs
splash
directoryservices
rake-task
markers
document-classification
responsive-slides
mbr
ms-project-server-2010
access-rights
file-locking
eclipse-memory-analyzer
sublist
first-responder
chrono
qtkit
bubble-chart
cinema-4d
rfc1123
coda-slider
gamequery
dmx512
icanhaz.js
asp.net-profiles
fixed-width
meego
mediarss
msdev
ugc

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App