Despicable
Despicable

Reputation: 3957

Date comparison issue

I am comparing dates by this code

 if(new Date().after(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").
     parse(rs.getString("FromDate")))){
     permMediaRs.setTimeFrame(true);
 }

But it is not giving me accurate result. So I tried to print both of them to check whether date is coming right or wrong.So when I printed them

    System.out.println("Today: " + new Date() + " From: "+new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").parse(rs.getString("FromDate"));

Then it gave me this output.

   TOday: Fri May 24 12:34:38 PKT 2013  From: Thu Nov 03 00:00:00 PKT 35

Now you can see that today date is coming fine but in From date I am unable to understand what is 35. I am expecting a year here but it is printing just 35. Any suggestions?

Note: rs.getString("FromDate")); value is coming from DB and its value is 2013-05-22 00:00:00.0

Upvotes: 1

Views: 117

Answers (3)

Kai
Kai

Reputation: 39651

You dateformat is just wrong. You are using dd-MM-yyyy HH:mm:ss but in your database is 2013-05-22 00:00:00.0 stored.

Try yyyy-MM-dd HH:mm:ss.S instead. And by the way: you should better use a Date field in your database than a String.

See the Javadoc of SimpleDateFormat.

Upvotes: 1

talipkorkmaz
talipkorkmaz

Reputation: 332

Your SimpleDateFormat pattern is not valid for the string "2013-05-22 00:00:00.0"

Try this one yyyy-MM-dd HH:mm:ss

Upvotes: 2

UmNyobe
UmNyobe

Reputation: 22920

The format specified is "dd-MM-yyyy HH:mm:ss",while in the DB you say you have 2013-05-22 00:00:00.0. The parsing fail of course. It should be "yyyy-MM-dd HH:mm:ss"

Upvotes: 2

Related Questions