Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Parse a Date String

0 views
Skip to first unread message

Norris

unread,
Sep 27, 2003, 8:52:29 AM9/27/03
to
I have a String with the format "Mon Sep 15 13:00:00 PDT 2003",
I tried to parse it using
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
Date time = df.parse(s1);
and get a ParseException. I still wonder how to parse it?
I have looked into Date class, DateFormat class. But I still have not got
a clue.

Thank you


Owen Green

unread,
Sep 27, 2003, 9:22:51 AM9/27/03
to

Try SimpleDateFormat with your own format string.

hth
--
Owen

Norris

unread,
Sep 28, 2003, 2:46:45 AM9/28/03
to
For the format specified, Date class will do the job:

Date time = new Date(s);

I might have thought too much at the beginning...


John McGrath [TeamB]

unread,
Sep 28, 2003, 3:42:44 AM9/28/03
to
On 9/28/2003 at 2:46:45 AM, Norris wrote:

> For the format specified, Date class will do the job:
>
> Date time = new Date(s);

Note that the Date(String) constructor is deprecated and may be removed
in a future version of Java.

The recommended way to do this is with the DateFormat parse() method.
Just use a DateFormat that matches your input string.

--
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
http://www.JPMcGrath.net/newsgroups/e-mail.html

Norris

unread,
Sep 28, 2003, 7:58:49 AM9/28/03
to
> Note that the Date(String) constructor is deprecated and may be removed
> in a future version of Java.
>
> The recommended way to do this is with the DateFormat parse() method.
> Just use a DateFormat that matches your input string.

That would be a problem. But I just stuck at the DateFormat parse() method.
It did not give me a chance to define my input time format, nor did it parse
correctly.
Looking at Java API, it seems that SimpleDateFormat has a better chance to
make
it work although I have not tried it.


Norris

unread,
Oct 1, 2003, 12:20:32 AM10/1/03
to
I get it work now. Actually, I originally wrote this way. Only leave out
"Locale.CANADA". It does not work on my computer but work on some computer.
After adding "Locale.CANADA", it works everywhere.

SimpleDateFormat sdfInput = new SimpleDateFormat
("EEE MMM dd HH:mm:ss zzz yyyy", Locale.CANADA );
time = sdfInput.parse(s);

0 new messages