Solved

Java Dates

Posted on 2004-10-13
8
209 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thymeleaf natural templating vs JSP 2 99
Arrays.asList  VS  ArrayList 4 59
expectj telnet failing 5 36
How to determine if a string is a valid SHA value 7 33
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

773 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