SimpleDateFormat


Last Updated:

  1. creatiive

    creatiive Member This Topic's Starter

    Joined:
    Jan 14, 2010
    Messages:
    7
    Likes Received:
    0
    hey,

    im trying to parse a datetime... and im having a lot of trouble. The code i have is pretty simple, just trying to parse a string in to a Date object.

    In this code, the 'datetime' variable is "01-13-2010 21:56".
    After the df.parse(datetime) line, the date is "Sat Jan 01 21:56:00 GMT+00:00 2011"... which is wrong.

    Then, after the calls to getYear, etc... the values are different again!
    mYear = 111
    mMonth = 0
    mDay = 6
    mHour = 21
    mMinute = 56

    I've tried lots of different SimpleDateFormat formats and i cannot get any to work! Any help here would be great!

    Code (Text):
    1.  
    2. SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm");
    3.     if (datetime != null && !datetime.equals("")) {
    4.     Date date = df.parse(datetime);
    5.     mYear = date.getYear();
    6.     mMonth = date.getMonth();
    7.     mDay = date.getDay();
    8.     mHour = date.getHours();
    9.     mMinute = date.getMinutes();
    10. }
    11.  
     

    Advertisement
  2. KlaymenDK

    KlaymenDK Well-Known Member

    Joined:
    May 29, 2009
    Messages:
    1,217
    Likes Received:
    130
    Ugh, I get the same kind of problems in Lotus Notes. Horrible. Here's what's going on:

    Your string date appears to be 'mm-dd-yyyy' (based on the '13' in there), but when you try to parse it, the day becomes '1' and the year shifts up by one. Why? Because the actual date format is 'dd-mm-yyyy', and so the month is read as 12+1, meaning January of the following year. This is most likely also the cause for getYear returning '111', but I'm not so sure what's going on with getDay.

    So, looking at your specific code, it seems to me that you are telling SimpleDateFormat to use a wrong format to interpret your given datetime string. Shouldn't you be using "new SimpleDateFormat("MM-dd-yyyy HH:mm")"? I'd need to see a bit more of the surrounding context to be certain, but you can probably check it yourself.
     
  3. Christophe2010

    Christophe2010 Active Member

    Joined:
    May 13, 2010
    Messages:
    35
    Likes Received:
    4
    isn't there something in the API concerning the DateFormat class?
     
  4. KlaymenDK

    KlaymenDK Well-Known Member

    Joined:
    May 29, 2009
    Messages:
    1,217
    Likes Received:
    130
  5. Christophe2010

    Christophe2010 Active Member

    Joined:
    May 13, 2010
    Messages:
    35
    Likes Received:
    4
    Do I sense a bit of hostility? :-/ Sorry to get you rauled up, but I'm not very in the know of how to use DateFormat class or methods, lol. Sorry, I wish I could help. When in doubt, use the API ;).
     
  6. KlaymenDK

    KlaymenDK Well-Known Member

    Joined:
    May 29, 2009
    Messages:
    1,217
    Likes Received:
    130
    If that's what you sense, then I'm very glad you asked -- because I do try my best to NOT be hostile. :) If anything, it was meant as a tip for good practice, perhaps phrased as a friendly jab. I do apologise.

    No hard feelings I hope!
     
  7. Christophe2010

    Christophe2010 Active Member

    Joined:
    May 13, 2010
    Messages:
    35
    Likes Received:
    4
    lol, mhmmmm. riiiight. :p
     

Share This Page

Loading...