Solved

Java Dates

Posted on 2004-10-13
8
211 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 292
ejb stateless example 2 40
eclipse console opening separately 2 36
maven disable workspace resolution 1 37
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 make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

762 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