Solved

Java Dates

Posted on 2004-10-13
8
212 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
[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
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

752 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