?
Solved

Java compare current date to db value

Posted on 2011-09-07
6
Medium Priority
?
240 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

801 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