• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parse Date

 
Imre Tokai
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,


Need to convert string Wed Apr 21 16:02:00 CEST 2010 to a java.util.Date object;

Aim is to implement totally generic solution (format might differ based on Local on each machine);
Is there any way to determine format of the date that the String contains?

Any quick hint how to make this work, please?


Regards
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many amazing services out there. One of them is Google ;-)

which returned this --> http://www.exampledepot.com/egs/java.text/parsedate.html
 
Imre Tokai
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply, Sebastian!


Is there any way to determine format of the date from the String? That would bring a generic solution, because i'm not sure what to submit to
new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
constructors parameter


Regards
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imre Tokai wrote:Is there any way to determine format of the date from the String?


Well, sure. You look at it. It starts with "Wed", that's a day of the week. Then it carries on with "Apr", that's the name of a month. Then it has "21", which is almost certainly a day of the month, although it might possibly be a 2-digit year. Then it goes "16:02:00" which is clearly a time stamp, hours plus minutes plus seconds. Then it's got "CEST" which is the name of a time zone. Sort of. And finally it has "2010" which must be the year. That removes the possibility that the "21" earlier was a 2-digit year.

But if you were hoping to write code which implements that sort of intelligence, it isn't going to be easy. In fact I would try to change the requirements rather than being forced into doing that. Instead, control the requirements so that you say what format the date string should be in.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic