Solved

Java compare current date to db value

Posted on 2011-09-07
6
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 306
null output 3 43
Running JavaFX on JDeveloper 12C 1 79
Is there a simpler dropbox system? 10 44
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

730 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