SimpleDateFormat.parse() causes Unparsable date exception

I am using SimpleDateFormat to both format and parse date strings. The format is working properly, but parse results in the following exception:
java.text.ParseException: Unparseable date: "2007-08-31T12:05:05.651-0700"
at java.text.DateFormat.parse(Unknown Source)
Here is my code:
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss.SZ");
String dateTime = dateFormatter.format(new Date());
Date test = dateFormatter.parse(dateTime);
For testing purposes, I am formatting a date string, and then passing it right back into parse() and I get the exception.
I am using jre1.5.0_10.
Thank you for your help.
-Karen

You have specified the milliseconds (S) to have a minimum of 1 letter.
From the API:
Number: For formatting, the number of pattern letters is the minimum number of digits, and shorter numbers are zero-padded to this amount. For parsing, the number of pattern letters is ignored unless it's needed to separate two adjacent fields.
If you specify it like this:SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss.SSSZ");
// or like this
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss.S Z");it will work.
Message was edited by:
dwg

Similar Messages

  • SimpleDateFormat: Parse String To Date

    Hi
    I'm trying to parse Date-String to Date. But the Date-String I want to parse is embedded in a Text like:
    Bla bla bla bla bla bla 2004-05-05 bla bla bla bla bla.
    So I tried the attached Class to solve the problem, but it didn't work.
    Any ideas?
    Thanks Bona
    public class Test {
    private static SimpleDateFormat dateFormat = new SimpleDateFormat();
    private String text;
    private Date date;
    public Test() {
    /ry {
    text = "Bla bla bla bla bla bla 2004-05-05 bla bla bla bla bla.";
    date = toDate(text, "yyyy-MM-dd");
    System.out.println("Datum: "+date);
    } catch (ParseException pe) {
    System.out.println(pe.getMessage());
    public static void main(String[] args) {
    Test test1 = new Test();
    public static Date toDate(String dateString, String dateFormatPattern)
    throws ParseException {
    Date date = null;
    dateFormat.applyPattern(dateFormatPattern);
    dateFormat.setLenient(true);
    date = dateFormat.parse(dateString);
    return date;
    }

    Well, you need to extract the date pattern from that String.
    You can use regular expressions to do that. See theRegex package for info on how to do that.
    BTW - AFAIK, lenient date parsing means, that the parser accepts stuff that matches the pattern, but contains illegal values like "2004-02-30" ...

  • SimpleDateFormat.parse accepts invalid date string

    SimpleDateFormat("dd/MM/yyyy").parse(...) accepts invalid input strings (e.g. "1.1.2gsd001") without raising an exception.
    Where I do a mistake?
    Example:
    import java.text.*;
    import java.util.*;
    public class Test {
    public static void main(String[] args) throws ParseException {
    String s = "1.1.2gsd001";
    SimpleDateFormat sdf = new SimpleDateFormat("d.M.yyyy");
    sdf.setLenient(false);
    Date d = sdf.parse(s);
    System.out.println(d);
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(d);
    The result is: Sun Jan 01 00:00:00 CET 2
    Thanks for your help.

    Tahnks s lot.
    Final code is (and it workes well):
    import java.text.*;
    import java.util.*;
    public class Test {
      public static void main(String[] args) throws ParseException {
        ParsePosition pp = new ParsePosition(0);
        String s = "1.1.2001";
        SimpleDateFormat sdf = new SimpleDateFormat("d.M.yyyy");
        sdf.setLenient(false);
        Date d = sdf.parse(s, pp);
        if (pp.getIndex() != s.length())
          throw new ParseException(String.format("Unparseable date: %s", s), pp.getIndex());
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(d);
    }

  • Unparsable date exception

    Hi,
    I am getting exception "Unparseable date: "Thu May 21 18:16:42 IST 2009"" while changing my regional setting to turkey
    Exception occured by following statement
    SimpleDateFormat myDateFormat = new SimpleDateFormat(sFormat,Locale.getDefault());
    myDate = myDateFormat.parse(sDateValue);
    what would be the problem, can any body help me
    Thanks
    Tej Kiran

    TejKiran wrote:
    i am using sFormat is "E MMM dd hh:mm:ss z yyyy"I don't know if you're still around. But for what it's worth:
    "MMM" means "month in text format, abbreviated".
    When you use that with a turkish locale, then the turkish names for the months will be used. So your english-language dates won't be parsed correctly.

  • Strange problem with SimpleDateFormat.parse method

    I got something strange with this method.
    String pattern = "yyyy/MM/dd";
    String mydate = "2007/00/10";
    SimpleDateFormat formatter = new SimpleDateFormat(pattern);
    Date newdate = formatter.parse(mydate);
    I get "2006/12/10"
    is this correct.

    dongyisu wrote:
    and there no exception get thrown outYes it does. I ran this:
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    public class DateFormatTest
       public static final String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
       public static void main(String[] args)
          DateFormat formatter = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
          formatter.setLenient(false);
          for (int i = 0; i < args.length; ++i)
             try
                Date date = formatter.parse(args);
    System.out.println("input: " + args[i] + " date: " + date);
    catch (ParseException e)
    System.err.println(args[i] + " is not a valid date");
    and got this when I input "2007/00/10":
    com.intellij.rt.execution.application.AppMain DateFormatTest 2007/00/10
    2007/00/10 is not a valid date
    Process finished with exit code 0%

  • Parsing String to date

    This is my function to convert a string into a desired output format.But my Date in the desired output format is coming out to be null.Could smeone plz point out my mistake.
    Date getDateInDesiredFormat(String strInputDate,String strInputFormat,String strOutputFormat)
         try
           SimpleDateFormat sdfInput  = new SimpleDateFormat(strInputFormat);
           SimpleDateFormat sdfOutput = new SimpleDateFormat("MM-dd-yyyy");
           ParsePosition pos = new ParsePosition(0);
           Date dtInputDate=sdfInput.parse(strInputDate.trim(),pos);
           System.out.println(dtInputDate);
           String strFormattedDate=sdfOutput.format(dtInputDate);
           System.out.println(strFormattedDate);
           Date dtOutputDate=sdfOutput.parse(strFormattedDate.trim(),pos);
           if(dtOutputDate==null)
                System.out.println("dtOutputDate is null ");
           else
               System.out.println(dtOutputDate.toString());
           return dtOutputDate;
         catch (NullPointerException npex)
             return null;
          catch(Exception ex)
              return null;
       }This is how i am calling the function
    Date date=getDateInDesiredFormat("Fri Sep 30 20:30:56 IST 2006","EE MMM d HH:mm:ss ZZZ yyyy","MM-dd-yyyy");
      }

    You need to use the sdfInput object to parse the date and sdfOutput to format and print it (like you did before your 'if'):
    SimpleDateFormat sdfInput  = new SimpleDateFormat(strInputFormat);
    SimpleDateFormat sdfOutput = new SimpleDateFormat("MM-dd-yyyy");
    Date dtInputDate=sdfInput.parse(strInputDate.trim());
    String strFormattedDate=sdfOutput.format(dtInputDate);
    System.out.println(strFormattedDate);the toString() you use in the else block uses a default format, not the one you specify.

  • Java.text.SimpleDateFormat.parse()

    This method does too much than I expected, say:
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    Date d = sdf.parser("00/00/0000");The d would be 1999, Nov. 30.
    After hacked the source code and some testing, I found it will roll backward or forward to generate a date. So, 01/32/2000 would become Feb. 01 2000. But I don't think this is a good implementation, as I expect it could throw exceptions at runtime if the string is not a valid time sting. And it's very difficult to debug, since any digit could be parsed!
    I think you guys here know what's behind the scene and why it's implemented like this. Any idea? Do you agree that the parse method should throw an exception when the fields of a date string're out of range? Or am I missing something?
    Comments are welcomed, and duke dollars're ready for those good answers.

    change your code as follows at it will do what you expected in the first places:
    <code>
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    sdf.setLenient(false);
    Date d = sdf.parser("00/00/0000");
    </code>
    Spieler

  • Need to parse Japanese Full Date Format

    Hi,
    Locale japanLocale = Locale.JAPAN;
    SimpleDateFormat fullDateFormat = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, japanLocale);
    String dateValue = 2010'?'09'?'12'?' 1'?'15'?'45'?'GMT // Here Japanese character will come instead of ?
    DateFormat initialDateFormat = new SimpleDateFormat(fullPattern, japanLocale);
    Date date = initialDateFormat.parse(dateValue);
    String formattedDate = fullDateFormat.format(date);
    This throws Exception java.text.ParseException: Unparseable date: "2010'?'09'?'12'?' 1'?'15'
    ?'45'?'GMT"
    If you send some good suggestions, it will be appreciable.
    Thanks in Advance
    Suresh

    Japanese full pattern is:
    "yyyy'&#24180;'M'&#26376;'d'&#26085;' (EEEE)
    H'&#26178;'mm'&#20998;'ss'&#31186;' z"
    I need to parse this string.
    OK:
    String pattern = "yyyy'&#24180;'M'&#26376;'d'&#26085;' (EEEE) H'&#26178;'mm'&#20998;'ss'&#31186;' z";
    String date = "2004&#24180;8&#26376;12&#26085; (&#26408;&#26332;&#26085;) 13&#26178;56&#20998;01&#31186; EEST";
    SimpleDateFormat parser = new SimpleDateFormat(pattern, Locale.JAPAN);
    Date d = parser.parse(date);
    System.out.println(d); // prints Thu Aug 12 13:56:01 EEST 2004
    Note that for the sake of portability the two first lines should be written asString pattern = "yyyy'\u5e74'M'\u6708'd'\u65e5' (EEEE) H'\u6642'mm'\u5206'ss'\u79d2' z";
    String date = "2004\u5e748\u670812\u65e5 (\u6728\u66dc\u65e5) 13\u664256\u520601\u79d2 EEST";This is to make it possible to compile your code on a platform that doesn't support Japanese script.
    Can i have your Yahoo / AOL ID?What makes you think I have one? :)

  • UNPARSABLE Date

    Hi i am using JDEV 11.1.1.3
    i have a rich input date in my form....
    i getting the value in my backing bean in string format
    String ProjStartDate = getId4().getValue().toString();
    getting value in this format
    "Sun Mar 06 00:00:00 IST 2011
    want 2 covert into java.util.date,i tried
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    java.util.Date ConvProjStartDate = dateFormatset.parse(ProjStartDate);
    i tried different format also....
    but i am getting error as UNPARSABLE DATE
    please suggest me a solution
    Edited by: Venkat L Naidu on Mar 22, 2011 11:32 PM

    try this:
    SimpleDateFormat formatter=
    new SimpleDateFormat("MM-dd-yyyy");
    String strDate = formatter.format(this.id1.getValue());

  • SimpleDateFormat parsing problem

    Hi All,
    I am creating a date "12142007 14:30" by parsing this string with the SimpleDateFormat object. However, when I convert it back to string and ask it to spit it out it gives me a completely different date of "Sun Jun 14 14:00:00 CDT 2009".
    Am I missing something here ? Not sure if i have to put something in the parse position. Any help would be appreciated.
    Thanks and Regards,
    *public void test2() throws Exception{*
    *          SimpleDateFormat sdf3 = new SimpleDateFormat("MMddyyyy HH:MM");*
    *          Date theDate = sdf3.parse("12142007 14:30");*
    *          System.out.println(theDate.toString());          *
    Edited by: mikematic_wha on Dec 14, 2007 9:42 AM

    PeterBro wrote:
    Try that again with the spelling corrected
    public void Calest()
              String tempDate = textField2.getText(); // ie 20080128
              SimpleDateFormat ofd = new SimpleDateFormat("yyyyMMdd");
         Date tod = odf.parse(tempDate); // this line errors
         }Peter,
    Why don't you return to the thread you started where you got a couple of answers and if you have a follow up question post it there.
    [http://forum.java.sun.com/thread.jspa?threadID=5290540]

  • Need info on SimpleDateFormat, Converting String to Date

    I'm a newbie and doing a conversion of a string to a date and it's adding a little less than 11 minutes. I know I'm missing something really simple but as I read it I ought to be able to convert "20030125 084539.637696" to Sat Jan 25 08:45:39 not 8:56! Also, for the time being I'm ignoring the zulu 'Z' because I can't find a pattern that'll take it.
    System.out.println("INFO:MetadataExtractorBean::filestarttime:" + filestarttime);
    filestarttime = filestarttime.replace("Z","");
    System.out.println("after filestarttime.replace(Z,null) filestarttime:" + filestarttime);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd hhmmss.SSSSSS");
    Date convertedDate = dateFormat.parse(filestarttime);
    System.out.println("after dateFormat.parse(filestarttime) convertedDate:" + convertedDate);
    INFO:MetadataExtractorBean::filestarttime:20030125 084539.637696Z
    after filestarttime.replace(Z,null) filestarttime:20030125 084539.637696
    after dateFormat.parse(filestarttime) convertedDate:Sat Jan 25 08:56:16 EST 2003
    Can someone help me with a) why it doesn't remain 8:45, and b) how to modify the pattern to be able to parse a zulu date. Thanks in advance.

    import java.text.*;
    public class ParsingExample {
        public static void main(String[] args) throws ParseException {
            String s = "20030125 084539.637";
            SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd HHmmss.SSS");
            System.out.println(f.parse(s));
    }Your round up was because ".637696" was taken to mean 637,696 milliseconds. SimpleDateFormat doesn't have a way to accept microseconds, and anyway, java.util.Date only has millisecond precision. I also changed "hh" to "HH" because I assume this is with a 24 hour clock.
    edit: you can tell your date format not to do these roll ups by setting:
    f.setLenient(false);

  • [svn:fx-trunk] 12077: Although Spark RichText does not support link formats , modifying compiled FXG to not generate ActionScript code that will cause compile time exceptions .

    Revision: 12077
    Revision: 12077
    Author:   [email protected]
    Date:     2009-11-20 18:16:32 -0800 (Fri, 20 Nov 2009)
    Log Message:
    Although Spark RichText does not support link formats, modifying compiled FXG to not generate ActionScript code that will cause compile time exceptions.
    Removing references to Flex Builder 3 in RPC.
    QE notes: N/A
    Doc notes: N/A
    Bugs:
    SDK-24305 - Link format property nodes cause errors on RichText in FXG 2.0
    SDK-24322 - A couple references to Flex Builder 3 in Flex 4 LangRef (and code comments)
    Reviewer: Deepa
    Tests run: checkintests
    Is noteworthy for integration: No
    Ticket Links:
        http://bugs.adobe.com/jira/browse/SDK-24305
        http://bugs.adobe.com/jira/browse/SDK-24322
    Modified Paths:
        flex/sdk/trunk/frameworks/projects/rpc/src/mx/rpc/xml/XMLDecoder.as
        flex/sdk/trunk/modules/compiler/src/java/flex2/compiler/fxg/FlexFXG2SWFTranscoder.java

  • How to parse thus XML data?

    Hi,experts:
    In below thread:
    Receiving .Net dataset with deployed proxies
    i asked how to create a external definition in IR base on above XML data.
    Now,i have created a XSD file base on the XML data.And importing it into IR successfully.
    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="zh-CN">
    <xs:complexType>
    <xs:choice maxOccurs="unbounded">
    <xs:element name="Status">
    <xs:complexType>
    <xs:attribute name="Result" type="xs:string"/>
    </xs:complexType>
    </xs:element>
    </xs:choice>
    </xs:complexType>
    </xs:element>
    </xs:schema>
    And now,the question is how to parse the XML data in diffgr:diffgram  segment.It seems a MS-XML syntax.Can it be parsed by XI?
    I tried,but the following error occurs:
    com.sap.aii.utilxi.misc.api.BaseRuntimeException: RuntimeException in Message-Mapping transformation: Cannot produce target element /ns0:ZMT_SD_ORDER01_RESULT. Check xml instance is valid for source xsd and target-field mapping fulfills requirements of target xsd at com.sap.aii.mappingtool.tf3.AMappingProgram.start

    Hi,
    The above Exception seems to be because of mapping error.
    Check your mapping for all the mandatory 1-1 mapping, also the 1-unbounded parent node mapping.
    Once when you import the XSD and activate your External Definition if it does without any error then there is no problem with your External Definition.
    If this error occurs only when you test your mapping then this is mapping error and not because of XSD.
    Please check the parent node mapping like 1-unbounded.
    Reward Points if useful
    Regards
    Ashmi.

  • Strange result of SimpleDateFormat.parse()

    Hi,
    I'm having trouble with the parse data function as it does not return the correct hour.
    Here is what I'm doing:
    Date nullDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse("1900-01-01T00:00:00-0000");I would now expect nullDate to be Mon Jan 01 00:00:00 CET 1900
    But instead, it is Mon Jan 01 00:09:21 CET 1900
    Personally I cannot think of a reason why the time is not 0.
    Any ideas?

    I don't see the problem you see. What version of Java on what OS and in what time zone?
    P.S. I would certainly set the time zone for the parser i.e.
           SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
            parser.setTimeZone(TimeZone.getTimeZone("UTC"));
            Date nullDate = parser.parse("1900-01-01T00:00:00-0000");
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
            System.out.println(formatter.format(nullDate));On my Ubuntu 10.04 using JDK 1.6.0_20 in London (currently GMT) time zone I get
    1900-01-01 00:00:00 GMT

  • Parsing today's date into a String

    How can I parse today's date into a String using this format: dd.mm.yyyy??? I taken a look at SimpleDateFormat, but I couldn't figure it out...
    Herman Svensen

    Hi Herman
    this is copied from the api docs:
    // Format the current time.
    SimpleDateFormat formatter
         = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss a zzz");
    Date currentTime_1 = new Date();
    String dateString = formatter.format(currentTime_1);
    http://java.sun.com/j2se/1.3/docs/api/java/text/SimpleDateFormat.html
    So what is the problem to figure out?

Maybe you are looking for