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

date.after() problem

 
Chris Montgomery
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two strings that I am converting to dates:

SimpleDateFormat sdf_date = new SimpleDateFormat("dd-MMM-yyyy hh:mm");
s_now = "24-Oct-2004 17:14"
s_task_complete_by_datetime = "24-Oct-2004 17:15"

dt_now = sdf_date.parse(s_now);
dt_complete_by = sdf_date.parse(s_task_complete_by_datetime);

if(dt_complete_by.after(dt_now)){
//do something
}
else
{
//do something else
}

when I run this code, I get the error: invalid date

Also, I wrote to a log dt_complete_by and dt_now:
dt_now = Sun Oct 24 17:14:00 CDT 2004
dt_complete_by = Sun Oct 24 17:15:00 CDT 2004

Any ideas?
 
Greg T Robertson
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you may have a problem in your formatter per the SimpleDateFormat doc here
it looks to me like hh as you have specified is for 1 - 12 (am/pm) and the HH is for 0 - 23. It appears your time actually uses 24 hour format.
 
Chris Montgomery
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I updated it to HH and then added the seconds, so now it's
SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");

still got the same error.

Since I'm geting a log of what my dates evaluate to, that tells me the dates were set correctly, right?

Perhaps my problem lies elsewhere...
 
Chris Montgomery
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually, now it works....


Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic