aspose file tools*
The moose likes JDBC and the fly likes Comparing two Dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Comparing two Dates " Watch "Comparing two Dates " New topic
Author

Comparing two Dates

sinasi susam
Ranch Hand

Joined: Jul 15, 2005
Posts: 67
I read a Date from db,
and now i need another java.sql.Date object,so that i can compare two Dates.

java.sql.Date my=new Date(2010,12,12);

I used this one .However when i printed it with System.out.println(),

it says;

3911-01-12

???

I could not understand that.Before i compile it i get a warning of deprecation.

1. question for me is

How to create a Date manually?

2. is how to compare them?

I saw the compareTo(Date) method that it returns an int.

i could not see any static int type in java.sql.Date.So what kind of values can take it?
How can i know which Date is bigger to another?
praveen kanakarajan
Ranch Hand

Joined: Apr 27, 2005
Posts: 54
the constructor you are using is a deprecated onethe value it adds the year with 1900.im just pasting the details in the java api dcoumentation refernce here

public Date(int year,
int month,
int date)
Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).

Allocates a Date object and initializes it so that it represents midnight, local time, at the beginning of the day specified by the year, month, and date arguments.

Parameters:
year - the year minus 1900.
month - the month between 0-11.
date - the day of the month between 1-31.

abt the questions

1)
you can use Calendar instead of the Date

2)

abt the comparison

if <0 the date is smaller
if =0 the date is equal
if >0 the date is greater

[ August 04, 2005: Message edited by: praveen k ]
[ August 04, 2005: Message edited by: praveen k ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

"praveen k",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kuldeep Vaishnav
Ranch Hand

Joined: May 23, 2004
Posts: 72
to generate sql date use


import java.sql.Date;
.............
.............
Date date = Date.valueOf("yyyy-mm-dd");


comparison as per Praveen K.


Kuldeep
sinasi susam
Ranch Hand

Joined: Jul 15, 2005
Posts: 67
Hello again,

i wanted to call a stored procedure.In my procedure there is an "in" parameter as Date,

Problem is i am setting date with setDate method of CallableStatement but it doesnt work.

And then i try another procedure there is not Date in it,what i see?It works!

How fool?

I get Date as said above Date.valuOf("2005-07-08") and set it.
But it doesnt work!

I did not understand what it is...
sinasi susam
Ranch Hand

Joined: Jul 15, 2005
Posts: 67
In fact it works a bit, but it doesnt return values as i expected.Because if i do the same thing from sql plus i get different results.

Whats going on?I did not understand.
Kuldeep Vaishnav
Ranch Hand

Joined: May 23, 2004
Posts: 72
make sure when you are defining you are defining sql date and not util date.Sometimes great minds forget to import java.sql.Date and define util date in turn...
Kuldeep Vaishnav
Ranch Hand

Joined: May 23, 2004
Posts: 72
can you post your code please.Might provide better insight.Thanks.
sinasi susam
Ranch Hand

Joined: Jul 15, 2005
Posts: 67
Sure,Here is procedure's structure

PROCEDURE HOLIDAY_CONTROL_DB(xcompany IN NUMBER, xdate IN DATE,
ERRORNO IN OUT NUMBER,ERROR IN OUT VARCHAR2,RAISE IN OUT NUMBER)

Here is my code to call it;


thx.
[ August 09, 2005: Message edited by: sinasi susam ]
Kuldeep Vaishnav
Ranch Hand

Joined: May 23, 2004
Posts: 72
AFAIK :

for interface CallableStatement

setDate(String parameterName,java.sql.Date date)
setDate(String parameterName,java.sql.Date date,Calendar cal)

are defined but nothing like

setDate(int parameterIndex,java.sql.Date date).

So guess try changing it to one of the two above said or use PreparedStatement to use parameterIndex...

Lemme know.....
[ August 09, 2005: Message edited by: Kuldeep Vaishnav ]
Kuldeep Vaishnav
Ranch Hand

Joined: May 23, 2004
Posts: 72
aahh...dumb me..callable extends prepared.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparing two Dates