Solved

Java Dates

Posted on 2004-10-13
8
207 Views
Last Modified: 2010-03-31
I would like to compare two dates for equality in Java.

here is how I am generating a timestamp for a new record into an Access db

Calendar date = Calendar.getInstance();
java.util.Date today = date.getTime();
java.sql.Date sqltoday;
sqltoday = new java.sql.Date(today.getTime());

I need to retrieve all rows in a database where one date is equal to today's date.

How can I do this?
0
Comment
Question by:vcgDevelopers
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12300870
SimpleDateFormat format =
        new SimpleDateFormat("dd/MM/yyyy");

  //inserted date into database
    Date d = new Date();
    String parsed = format.format(d);
    Date d1=format.parse(parsed);
    java.sql.Date d2=new java.sql.Date(d.getTime());

   
    //Fetched Sql Date
    long l=d2.getTime();
    Date sqlDate =new Date(l);
    parsed=format.format(sqlDate);
    Date d3=format.parse(parsed);
   
    //Current Date
    Date now=new Date();
    parsed = format.format(now);
    Date d4=format.parse(parsed);

    //equality check for Sql date and current date
    System.err.print(d4.equals(d3));

Hope this helps you
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12302367
PreparedStatement ps = conn.prepareStatement("select x from sometable where somedate = ?");
ps.setDate(1, sqltoday);
ResultSet rs = ps.executeQuery();
0
 
LVL 92

Expert Comment

by:objects
ID: 12303550
You need to use Timestamp, and not java.sql.Date. You don't need to create a Calendar instance either:

java.sql.Timestamp sqltoday = new java.sql.Timestamp(System.currentTimeMillis());
PreparedStatement ps = conn.prepareStatement("select x from sometable where somedate = ?");
ps.setTimestamp(1, sqltoday);
ResultSet rs = ps.executeQuery();
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12303599
@CEHJ and objects

Initially I thought the same. But my perception is different

Say suppose if a record is inserted 20 minutes back and if we fetch using  ur procedures, will they work??
the date value which is inserted is not same as current date when we consider time too.. right?? Just a doubt ;-)


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 12303656
>>the date value which is inserted is not same as current date when we consider time too.. right??

That's right, which is why vcgDevelopers does *not* want to use a Timestamp
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 12304297
if thats the case then it should be:

Calendar date = Calendar.getInstance();
java.sql.Date sqltoday = new java.sql.Date(date.getTime().getTime());
date.add(Calendar.DAY_OF_YEAR, 1);
java.sql.Date sqltomorrow = new java.sql.Date(date.getTime().getTime());
PreparedStatement ps = conn.prepareStatement("select x from sometable where somedate >= ? and somedate<?");
ps.setDate(1, sqltoday);
ps.setDate(1, sqltomorrow);
ResultSet rs = ps.executeQuery();
0
 
LVL 1

Expert Comment

by:nadhuvi
ID: 12305606
u can use java.util.compareTO()
Date d = new Date();
Date date2Compare= //getting from someehere
int difference = d.copmareTo(date2Compare)
will give the no. of days ok...........

0
 
LVL 92

Expert Comment

by:objects
ID: 12312860
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

911 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

22 Experts available now in Live!

Get 1:1 Help Now