?
Solved

Java compare current date to db value

Posted on 2011-09-07
6
Medium Priority
?
246 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

831 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