Date parsing always returning the month of January

Discussion in 'Java' started by laredotornado, Feb 29, 2012.

  1. 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
    laredotornado, Feb 29, 2012
    #1
    1. Advertising

  2. laredotornado

    markspace Guest

    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.
    markspace, Feb 29, 2012
    #2
    1. Advertising

  3. laredotornado

    Daniel Pitts Guest

    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, Feb 29, 2012
    #3
  4. laredotornado

    Daniel Pitts Guest

    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 ;-)
    Daniel Pitts, Feb 29, 2012
    #4
  5. laredotornado

    markspace Guest

    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.
    markspace, Feb 29, 2012
    #5
  6. laredotornado

    Jim Janney Guest

    laredotornado <> 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
    Jim Janney, Feb 29, 2012
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Maciejczak
    Replies:
    1
    Views:
    1,972
    Matthias Kaeppler
    Apr 23, 2005
  2. Bill
    Replies:
    10
    Views:
    722
    Fulvio
    Apr 13, 2006
  3. Replies:
    1
    Views:
    436
    Steve W. Jackson
    Dec 22, 2006
  4. Mufasa
    Replies:
    0
    Views:
    389
    Mufasa
    Sep 5, 2008
  5. Terrence Brannon
    Replies:
    0
    Views:
    111
    Terrence Brannon
    Feb 10, 2005
Loading...

Share This Page