Win a copy of Spark in Action this week in the Open Source Projects forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to get difference between dates

 
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.
 
Marshal
Posts: 69738
277
  • 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
Marshal
Posts: 69738
277
  • 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
Marshal
Posts: 69738
277
  • 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.
 
Do not threaten THIS beaver! Not even with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic