Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Date parsing always returning the month of January (http://www.velocityreviews.com/forums/t868899-date-parsing-always-returning-the-month-of-january.html)

laredotornado 02-29-2012 04:46 PM

Date parsing always returning the month of January
 
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

markspace 02-29-2012 05:20 PM

Re: Date parsing always returning the month of January
 
On 2/29/2012 8:46 AM, laredotornado wrote:
> 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.



Daniel Pitts 02-29-2012 05:21 PM

Re: Date parsing always returning the month of January
 
On 2/29/12 8:46 AM, laredotornado wrote:
> 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.


Daniel Pitts 02-29-2012 05:22 PM

Re: Date parsing always returning the month of January
 
On 2/29/12 9:20 AM, markspace wrote:
> On 2/29/2012 8:46 AM, laredotornado wrote:
>> 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.
>
>

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

markspace 02-29-2012 07:17 PM

Re: Date parsing always returning the month of January
 
On 2/29/2012 9:22 AM, Daniel Pitts wrote:
> On 2/29/12 9:20 AM, markspace wrote:
>> 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 ;-)



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.

Jim Janney 02-29-2012 10:30 PM

Re: Date parsing always returning the month of January
 
laredotornado <laredotornado@zipmail.com> writes:

> 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.

--
Jim Janney



All times are GMT. The time now is 06:14 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.