Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get difference between dates

 
Viidhya Kishore
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic