Solved

Java compare current date to db value

Posted on 2011-09-07
6
235 Views
Last Modified: 2012-05-12
I need a routine to determine if the current date is more than 1 day later than a createDate value in a database.  So given the two dates, how to determine this:

   Date createDate = // get createDate from database
   Date curDate = new Date();
  // is curDate more than 1 day after createDate
0
Comment
Question by:jkavx
  • 5
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36500083
java.sql.Date dd = //from datbase

java.util.Dtae  d = new java.util.Date();

if(d.getTime() - dd.getTime() > 3600L*24L*1000L) {
//do somthing
}else
{
//do somthing else
}
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500103
in general it is probably betetr to use it in the slect statemnet

select * from table where sysdate - date_field > 1

that would be in Oracle

In other databses ssysdate will be similar but different sysntax
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500164
One more way with Calendar
object

java.sql.Date dd = //from datbase

java.util.Date  d = new java.util.Date();


Calendar c = Calendar.getInstance();
c.setTime(dd);

Calendar c1 = Calendar.getInstance();
c1.setTime(d);

c.add(Calendar.DATE, 1);

java.util.Date d2 = c.getTime();
if(dd.after(d2)){
// do somthing
}
else
{
//do something
}





0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:jkavx
ID: 36500232
These are great, thx.  It seems now like the business rule may be changing, so the test might be whether the currentDate is the next calendar day from the createdDate or more.  So if the createdDate is 9/7 and the currentDate is 9/8 then the condition is met.  How would you do that?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500274
I think something like taht should work.

Let me check it

GregorianCalendar gc1 = new GregorianCalendar(dd.getYear(), dd.getMonth(); dd.getDate());

GregorianCalendar gc2 = new GregorianCalendar(d.getYear(), d.getMonth(); d.getDate());

gc1.add(Calendar.DATE,1);

if(g1.equals(gc2)) {


} else

{

}






0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36500316
Sure t worrks fine:

       SimpleDateFormat sdf2 = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");

        java.util.Date dd = sdf2.parse("09-07-2011 16:15:13", new ParsePosition(0));

         java.util.Date dd2 = sdf2.parse("09-08-2011 08:15:13", new ParsePosition(0));

GregorianCalendar gc1 = new GregorianCalendar(dd.getYear(), dd.getMonth(), dd.getDate());

GregorianCalendar gc2 = new GregorianCalendar(dd2.getYear(), dd2.getMonth(), dd2.getDate());

gc1.add(Calendar.DATE,1);

if(gc1.equals(gc2)) {

     System.out.println("comparing dates true");

} else

{
    System.out.println("comparing dates false");
}

Open in new window


output:

comparing dates true

Open in new window

0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JUnit 4 @Before and @BeforeClass differences 3 59
session migration servlets 2 36
JavaFX TableView not displaying correctly 3 18
printf performancy 11 42
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question