Solved

Java Dates

Posted on 2004-10-13
8
206 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
@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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
:)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
groovy example issue 10 65
java  and programming certification ? 4 63
solarwind tftp server 2 30
recursion example 16 66
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
This video teaches viewers about errors in exception handling.
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.

772 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

11 Experts available now in Live!

Get 1:1 Help Now