• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get difference between dates

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic