Results 1 to 4 of 4
  1. #1
    johan313 is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default Problem with SimpleDateFormat

    Due to a bug I am forced to parse the String given by java.util.Date.toString() back to a Date object (lets forget about the bug - it will be fixed).

    I tried writing a simple test:

    Java Code:
    private static final SimpleDateFormat longDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
       
        @Test
        public void testParseDateString() throws Throwable {
            Date date = longDateFormat.parse(new Date().toString());
        }
    But this does not work:
    java.text.ParseException: Unparseable date: "Mon Jan 17 16:52:35 EET 2011"

    I'm actually quite confused as why parse fails.

    Johan

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by johan313 View Post
    I'm actually quite confused as why parse fails.
    Same here because it works for me; are you sure you haven't messed up something else?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi,
    You need to set Locale for SimpleDateFormat
    for example
    Java Code:
     private static final SimpleDateFormat longDateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy", Locale.ENGLISH);

  4. #4
    johan313 is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    Locale! I forgot about that totally. Thank you very much Petr!

    BTW, your locale example was empty =)

    Johan

Similar Threads

  1. How to use SimpleDateFormat in J2ME Applications
    By thirupathik in forum CDC and Personal Profile
    Replies: 9
    Last Post: 08-13-2010, 09:18 PM
  2. JTextField and SimpleDateFormat
    By mine0926 in forum New To Java
    Replies: 15
    Last Post: 07-06-2010, 04:10 AM
  3. JFormattedTextField + SimpleDateFormat
    By Ralphw in forum AWT / Swing
    Replies: 1
    Last Post: 07-07-2009, 11:53 AM
  4. Locale with SimpleDateFormat
    By swati.jyoti in forum New To Java
    Replies: 4
    Last Post: 07-02-2009, 03:34 PM
  5. Using SimpleDateFormat
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 07-18-2008, 09:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •