Solved

Java compare current date to db value

Posted on 2011-09-07
6
234 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:jkavx
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now