Trouble parsing date/time with SimpleDateFormat

S

Scott Harper

I have the following sequence of code:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-DD'T'HH:mm:ss'Z'");
ParsePosition pos = new ParsePosition(0);
Date date = df.parse("2006-04-03T12:00:00Z", pos);
System.out.println(date);

It yields the following output:

Tue Jan 03 12:00:00 CST 2006

No matter what I provide to the parse method for the date/time string, the
month *always* comes back as January. The 'T' and 'Z' are literal
characters (even though I realize that Z is a valid format field for time
zone)... I've tried taking those literals out of my pattern i.e.

"yyyy-MM-DD HH:mm:ss"

but I still get the same results. Can anyone see what I might be doing wrong?
This is with JDK 5...


thanks
scott
 
O

Oliver Wong

Scott Harper said:
I have the following sequence of code:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-DD'T'HH:mm:ss'Z'");
ParsePosition pos = new ParsePosition(0);
Date date = df.parse("2006-04-03T12:00:00Z", pos);
System.out.println(date);

It yields the following output:

Tue Jan 03 12:00:00 CST 2006

No matter what I provide to the parse method for the date/time string, the
month *always* comes back as January. The 'T' and 'Z' are literal
characters (even though I realize that Z is a valid format field for time
zone)... I've tried taking those literals out of my pattern i.e.

"yyyy-MM-DD HH:mm:ss"

but I still get the same results. Can anyone see what I might be doing
wrong?
This is with JDK 5...

Use lowercase d for "days in month", i.e. "yyyy-MM-dd'T'HH:mm:ss'Z'".
Uppercase D means "days in year", and 03rd day in the year is January 3.

- Oliver
 
S

Scott Harper

I have the following sequence of code:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-DD'T'HH:mm:ss'Z'");
ParsePosition pos = new ParsePosition(0);
Date date = df.parse("2006-04-03T12:00:00Z", pos);
System.out.println(date);

It yields the following output:

Tue Jan 03 12:00:00 CST 2006

No matter what I provide to the parse method for the date/time string, the
month *always* comes back as January. The 'T' and 'Z' are literal
characters (even though I realize that Z is a valid format field for time
zone)... I've tried taking those literals out of my pattern i.e.

"yyyy-MM-DD HH:mm:ss"

but I still get the same results. Can anyone see what I might be doing wrong?
This is with JDK 5...

Ok, asked and answered... DOH!!

from http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6351033

"DD" specifies day of year. The correct letter for day of month is "d". The
reason why November is ignored is that the year + month combination doesn't
designate any date.


scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top