0

I am trying to compare a SQL date with the current date.

I figured out how to compare two SQL dates but I couldn't extract the current date.

java.sql.Date xxx = new java.sql.Date(jdatechooser1.getDate().getTime());
java.sql.Date yyy = new java.sql.Date(jdatechooser2.getDate().getTime());

if (yyy.after(xxx)) {
    System.out.println("ok");
}
6
  • 1
    new Date().after(xxx)? Commented Mar 23, 2016 at 11:31
  • the problem is sqldate doesn't have such constructor date() !!! Commented Mar 23, 2016 at 11:50
  • No, but java.util.Date does Commented Mar 23, 2016 at 11:51
  • cant import both sql and util date at the same time Commented Mar 23, 2016 at 11:57
  • 1
    new java.util.Date().after(xxx), you can use a fully qualified name instead. This is not a new problem Commented Mar 23, 2016 at 12:00

2 Answers 2

3

Uses the system date instead: System.currentTimeMillis()

java.sql.Date dateToBeChecked= new java.sql.Date(jdatechooser1.getDate().getTime());
java.sql.Date systemDate = new java.sql.Date(System.currentTimeMillis());

if(dateToBeChecked.after(systemDate)){
    System.out.println("ok");
}

But since it's long values you don't need to transform anything to an object and can do this instead:

if(jdatechooser1.getDate().getTime() > System.currentTimeMillis()){
  System.out.println("ok");
}
Sign up to request clarification or add additional context in comments.

Comments

2

At first you must create a java.util.Date object with empty constructor. Then give the long value which can be get by getTime() method, to the java.sql.Date constructor.

java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.