Date parsing always returning the month of January

L

laredotornado

Hi,

I'm using Java 6. Hoping someone can see the simple error I have
missed. I'm trying to parse a date. I have

final String formattedStr = "2012-03-01 01:30:00";
final String format = "yyyy-MM-DD HH:mm:ss";
final SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
ret = sdf.parse(formattedStr);
} catch (ParseException e) {
e.printStackTrace();
}
String retStr = ret.toString();

The value of "retStr" is "Sun Jan 01 01:30:00 CST 2012". In fact, the
month of the date string is always getting converted to January, even
though above it shoudl be March. Where is my date parsing going
wrong?

Thanks, - Dave
 
M

markspace

Hi,

I'm using Java 6. Hoping someone can see the simple error I have
missed. I'm trying to parse a date. I have

final String formattedStr = "2012-03-01 01:30:00";
final String format = "yyyy-MM-DD HH:mm:ss";
final SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
ret = sdf.parse(formattedStr);
} catch (ParseException e) {
e.printStackTrace();
}
String retStr = ret.toString();

The value of "retStr" is "Sun Jan 01 01:30:00 CST 2012". In fact, the
month of the date string is always getting converted to January, even
though above it shoudl be March. Where is my date parsing going
wrong?


First, good example, even though I could have used a complete compilable
one.

However, the answer is simple, and I'm going to have to ask you to
re-read the documentation for SimpleDateFormat. You missed the meaning
of one of those letters.
 
D

Daniel Pitts

Hi,

I'm using Java 6. Hoping someone can see the simple error I have
missed. I'm trying to parse a date. I have

final String formattedStr = "2012-03-01 01:30:00";
final String format = "yyyy-MM-DD HH:mm:ss";
final SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
ret = sdf.parse(formattedStr);
} catch (ParseException e) {
e.printStackTrace();
}
String retStr = ret.toString();

The value of "retStr" is "Sun Jan 01 01:30:00 CST 2012". In fact, the
month of the date string is always getting converted to January, even
though above it shoudl be March. Where is my date parsing going
wrong?
The "DD" should be "dd". "DD" is "Day of Year", so this is saying its
the first day of the year. "dd" is "Day of Month".

Often, when I'm having trouble with date parsing or formatting, its
because I made a mistake on the format string. The javadocs fix my
mistake in seconds.

HTH,
Daniel.
 
D

Daniel Pitts

First, good example, even though I could have used a complete compilable
one.

However, the answer is simple, and I'm going to have to ask you to
re-read the documentation for SimpleDateFormat. You missed the meaning
of one of those letters.
Heh, I my post has a spoiler, yours is better ;-)
 
M

markspace

Heh, I my post has a spoiler, yours is better ;-)


Yeah, I was hoping I wasn't coming across as snarky or something, but in
this case it really was so simple that I felt it was appropriate, esp.
after I told him where it was specifically.
 
J

Jim Janney

laredotornado said:
Hi,

I'm using Java 6. Hoping someone can see the simple error I have
missed. I'm trying to parse a date. I have

final String formattedStr = "2012-03-01 01:30:00";
final String format = "yyyy-MM-DD HH:mm:ss";
final SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
ret = sdf.parse(formattedStr);
} catch (ParseException e) {
e.printStackTrace();
}
String retStr = ret.toString();

The value of "retStr" is "Sun Jan 01 01:30:00 CST 2012". In fact, the
month of the date string is always getting converted to January, even
though above it shoudl be March. Where is my date parsing going
wrong?

This is only indirectly related to your question, but I recommend
calling setLenient(false) any time you use a DateFormat for parsing.
Too easy to get bogus results otherwise.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top