Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Parse Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parse Date" Watch "Parse Date" New topic
Author

Parse Date

Imre Tokai
Ranch Hand

Joined: Jun 04, 2008
Posts: 130
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

Joined: Feb 23, 2009
Posts: 1183
There are many amazing services out there. One of them is Google ;-)

which returned this --> http://www.exampledepot.com/egs/java.text/parsedate.html


JDBCSupport - An easy to use, light-weight JDBC framework -
Imre Tokai
Ranch Hand

Joined: Jun 04, 2008
Posts: 130
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Parse Date
 
Similar Threads
simple date format question
Conversion of String to Date
how to convert String into Date format
Difficulty assigning Java Util Date to TemporalType field in Entity Bean
simple date format