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

SimpleDateFormat turns 0000-00-00 into 0002-11-30 !

2,133 views
Skip to first unread message

Stephen

unread,
Jul 19, 2002, 5:38:59 AM7/19/02
to
The following code,

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
long t = sdf.parse("0000-00-00 00:00:00").getTime();
System.out.println(t);
Date d = new Date(t);
System.out.println(sdf.format(d));

outputs the following,

-62170185600000
0002-11-30 12:00:00

is this a bug in SimpleDateFormat or am I using it wrong to turn
a date string (eg. "2002-07-19") into a Long ?

Stephen.

Gustavo Serrano

unread,
Jul 19, 2002, 6:16:24 AM7/19/02
to
You are messing up the date logic, day 0 and month 0 are values that should
never happen, and the parse method returns a strange value (i don't know
why..) the rest of the code is correct for realistic dates

Gustavo


"Stephen" <shr...@gmx.co.uk> wrote in message
news:97ae44ee.02071...@posting.google.com...

John O'Conner

unread,
Jul 19, 2002, 11:14:34 AM7/19/02
to

Probably more than you need to know about DateFormat objects:
http://www.javai18n.com/articles/DateFormat.html

Regards,
John O'Conner

Roedy Green

unread,
Jul 24, 2002, 11:38:46 AM7/24/02
to
On 19 Jul 2002 02:38:59 -0700, shr...@gmx.co.uk (Stephen) wrote or
quoted :

>long t = sdf.parse("0000-00-00 00:00:00").getTime();

parse wants human-formatted dates, not 0-based ones for day and month.

--
Available for tutoring, problem solving or contract
programming for $50 US per hour or fixed price.
The Java glossary is at
http://www.mindprod.com/jgloss.html
or http://64.251.89.39/jagg.html
-
canadian mind products, roedy green

Daniel Bratell

unread,
Jul 25, 2002, 3:17:14 AM7/25/02
to
Roedy Green wrote:
> On 19 Jul 2002 02:38:59 -0700, shr...@gmx.co.uk (Stephen) wrote or
> quoted :
>
>
>>long t = sdf.parse("0000-00-00 00:00:00").getTime();
>
>
> parse wants human-formatted dates, not 0-based ones for day and month.

Also, there never existed a year 0. The year before 1 AD was the year 1
BC (as all who followed the discussion on when the millenium really
started knows all too well).

/Daniel

0 new messages