aspose file tools*
The moose likes Beginning Java and the fly likes How to get difference between dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to get difference between dates" Watch "How to get difference between dates" New topic
Author

How to get difference between dates

Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
Hello,
I have a created date in database which looks like '2006-12-01 11:02:17.0'
I need to get the number of days since created
implying I need to get the number of days between the created date and today's date.

Somebody please help.This is an urgent requirement.

P.S : I am not sure if I have posted in the right forum.Please bear with me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39863
    
  28
Originally posted by Shriya Kishore:
Somebody please help.This is an urgent requirement.

P.S : I am not sure if I have posted in the right forum.Please bear with me.
Ease Up Maybe the JDBC forum would have been better, but I am feeling benign at the moment.

Try the datediff(date_after, date_before) function
Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
no.
My problemm is,I am picking the createddate from the database i.e a String
My current date will be in date format.
How do I find the difference between them that too in days.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39863
    
  28
Still probably easier to get the Database to calculate . . .

Use the SimpleDateFormat class to parse the String. I have never actually used it myself.
Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
ok.
I did that.
But the column 'createdDate' is nullable in the db.
So how shall I check for nullpointer before parsing.
Code snippet :
if(rs.getString("creationDate") != null || rs.getString("creationDate")!= ""){
String str = rs.getString("creationDate");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
date = df.parse(str);

now my first record from database is having createdDate as (null)
But the if is getting executed and throwing me a null pointer exception at the line where I am parsing.

Please help
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39863
    
  28
You have got a null test. But you are not using it properly. Your != test on the String object will always return true. Try only the bit about == null.If you have reached the stage of database connection you ought to know better than to use == and != for checking objects for equality.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get difference between dates