SimpleDateFormat can parse 2009102x

Hello,
If you look to the piece of code below, it seems my users can enter 2009102w.
However, when one tries to enter 20091w20, it throws an error.
I don't understand why an incorrect character in my date can get my date parsed?!
Is this a bug or is there a reason?
          try {
                  DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
                  //this is ok
                  //Date date = (Date)formatter.parse("20091020");
                  //this is also ok !!!!!
                  //Date date = (Date)formatter.parse("2009102w");
                  //this is not ok
                  //Date date = (Date)formatter.parse("20091w20");
                  System.out.println(date);
              } catch (ParseException e) {
                   System.out.println("exception");
              }Thanks

The date formatter doesn't feel the need to parse all the characters you give it. If it reads an acceptable date it will ignore any garbage following. The Formatter objects in java.text seem to have been designed to parse a string with multiple elements, dates, numbers, etc., each taking it's own chunk. You'll find the same with the NumberFormat objects.
Also, by default, the parsing mode on the data formatter is set to "lenient". If you want to tighten it up call setLenient(false) on the formatter. This is mostly about things like if you put 31/02/2009 then with lenient on you'll get 03/03/2009, but may affect the acceptance of a single digit day.
P.S. if you really want to be strict about validating the format then check with a regexp first.
Edited by: malcolmmc on Sep 8, 2009 8:59 AM

Similar Messages

  • Can you recommend some tools that can parse XQuery?

    I need a tool that can parse an xquery and manipulate the internal blocks of it. Can somebody please help me? Thanks!

    Back in the day I used to use this: http://www.analogx.com/contents/download/audio/tagm.htm (freeware).
    Depending on how large your collection is, the program might run slower, still, it was a good program for what it does.

  • Is there a problem on the pattern of SimpleDateFormat to parse a String ?

    Hello,
    I tried to parse a String to a Date using a SimpleDateFormat.
    I must be able to have on the pattern something like : "yyyyMMdd_HH00".
    To get a String from a Date with SimpleDateFormat.format(Date date) it works well, but when a try to parse a String into a Date with SimpleDateFormat.parse(String date) I get a : java.text.ParseException: Unparseable date.
    I try it on a JVM 1.4 and 1.5 for the same result.
    An exemple of code :
              String pattern = "yyyyMMdd_HH00";
              SimpleDateFormat currentFormat = new SimpleDateFormat(pattern);
              Date date = currentFormat.parse("20091223_1000");It seems that adding the '00' after 'HH' raise the exception...
    Has the 'HH00' a particular meaning like on Oracle 'HH24' ?
    Is there a way to add number after the 'HH' ?
    Thanks
    Edit :
    Oh yeah, I forgot, I tryied to use the pattern "yyyyMMdd_HH'00'" using '' to escape the 00 without better result...
    Edited by: Jeremy.Antonucci on 23 déc. 2009 15:25

    The context of this pattern :
    To get a file on a http server, I must build an Url String with a filename using the current date.
    I want to be able to change easily the pattern on a file parameters.
    The date must be ending with 00 for the minutes.
    I may have use a pattern with 'HHmm' and suppress the minutes but if it is change on the futur the code will not work...
    As I can't say if in the futur the url will change for the date pattern or not, I decide to use a pattern like "yyyyMMdd_HH00" that works fine for everything else than parsing a string.
    I create a String date based on the current date on UTC using this pattern and keep it to use it to compose an Url String.
    After I need to know how much time I must sleep before the next execution (on each Hour), so I use this date +1 hour to see if the currentDate during this calculation is higher than this date+1... (I'm surely not clear, but it is not the real problem just for curious ^^)
    A dirty solution is to avoid this problem is to catch this case and do substitution on a temporary pattern of the 'HH00' with 'HHmm'. It's really dirty because nothing says that later a 'HH00' mean always 'HHmm' but I have nothing better...
              String pattern2 = pattern;
              if (pattern.matches(".*HH00$")) {
                   pattern2=pattern.substring(0, (pattern.length()-2))+"mm";
              }Where can I do this bug report ?
    Do you see a good solution ?

  • How can (parse) i use XML file with missing EndTag

    hi,
    i have an application which writes an "XML file".
    another application should read that XML file, build an DOM and
    access the nodes with xpath.
    my problem. if the first application is not finished there are tags
    missing. e.g. </xml>. but the seconds application cannot wait until the first application finishes it task.
    if i now read the XML file the parser cannot load it because the end tags are missing.
    my question:
    how can i deactivate the check or how can i read the XML file and access it via XPath (my application is using at the moment XPath to access the nodes and i dont want to change that)
    as parser i am using XERCES
    alex

    As far as I know, you can't do this - xml must be well formed (this is sort of a bedrock of xml). There may be some work around's, but I'm not aware of any - and they would most likely be hacks.

  • Maximum XML file size that can parsed with c++ XML parser

    Hi!
    what is the maximum file size that can be parsed using the xml parser(version 1) for c++ on linux .
    i'm getting an error(error no 231) when i try to parse an XML file of 3MB on Red Hat Linux 6.1
    Regards
    anjana

    moving to xml db forum

  • Request - Perl script that can parse an ACE config from a VIP

    Has anyone ever written a Perl script that can take the VIP from an ACE config and parse it out into the component parts of the config, ACLs, NATs, Class-map, policy-map, etc. ?
    This seems like something someone must have written already.
    Thanks in advance!

    Any reason you dont want to use XML instead of doing just PERL ?  Its lot easier to do with XML scripts as ACE has a XML interface to query whatever is needed. So that said, you can use a perl to interface ACE via XML. Here's a simple Perl that uses LibCurl:
    #!/usr/bin/perl
    use WWW::Curl::Easy;
    my $numArgs = $#ARGV + 1;
    if ($numArgs<4)
    die("Usage: shusers.pl ip_address username password command\n");
    my $ip = @ARGV[0];
    my $uname = @ARGV[1];
    my $pwd = @ARGV[2];
    my $cmd = @ARGV[3];
    my $curl = new WWW::Curl::Easy;
    my $posturl = "http://$ip/bin/xml_agent/";
    my $xml_cmd = "xml_cmd=<$cmd/>";
    $curl->setopt(CURLOPT_HEADER, 0);
    $curl->setopt(CURLOPT_FRESH_CONNECT, 1);
    $curl->setopt(CURLOPT_URL, $posturl);
    $curl->setopt(CURLOPT_RETURNTRANSFER,1);
    $curl->setopt(CURLOPT_USERPWD,"$uname:$pwd");
    $curl->setopt(CURLOPT_POST,1);
    $curl->setopt(CURLOPT_POSTFIELDS, $xml_cmd);
    $curl->perform;
    my $info = $curl->getinfo(CURLINFO_RESPONSE_CODE);
    print $info;
    Hope this helps.
    Cheers
    V.K

  • Anyone know how I can parse this string?

    Hello everyone!
    I'm using the Scanner class (which is like the StringTokenizer class but suppose to be more powerful).
    Anyways, I have the following String:
    OP '1.3.12.2.1004.212'.'1.3.12.2.1004.195'
    I want to parse the String so I have the following:
    '1.3.12.2.1004.212'
    '1.3.12.2.1004.195'
    so basically just parse it by the middle dot, I tried useDelimeter function and it doesn't parse it up at all. You would think it would have parsed every occurance of . but that isn't the case.
    Here is my code:
    else if(line.contains("OP"))
                             System.out.println("HIT THE SUBFIELD PART lulz");
                             String subClassName = "";
                             String subFieldName = "";
                             scan = new Scanner(line);
                             scan.useDelimiter(".");
                             System.out.println("LINE: " + line);
                             while(scan.hasNext())
                                  System.out.println("Token: " + scan.next());
                             }The output is the following:
    HIT THE SUBFIELD PART LOL
    LINE: OP '1.3.12.2.1004.212'.'iPAddress'
    Token:
    Token:
    Token:
    Token:
    Token:
    Token:
    Token:
    Any ideas what I can do to parse it up the way I mentioned above? thanks!

    hm..it just occured to me I could do the following
    else if(line.contains("OP"))
                             System.out.println("HIT THE SUBFIELD PART lulz");
                             String subClassName = "";
                             String subFieldName = "";
                             scan = new Scanner(line);
                             scan.skip("OP");
                             scan.useDelimiter("'.'");
                             System.out.println("LINE: " + line);
                             while(scan.hasNext())
                                  System.out.println("LOOK HERE: " + scan.next());
                             }I get the following output:
    LINE: OP '1.3.12.2.1004.212'.'iPAddress'
    LOOK HERE: '1.3.12.2.1004.212
    LOOK HERE: iPAddress'
    So I guess I can just manually manipulate that last token and readd the ' character!
    I think thats what I will do but if you can find a bettter solution let me know! thanks! :D

  • App needs to run on multiple DBs; can parsing schema/ ref owner be dynamic?

    I have an app that needs to run on multiple databases. Is there a way to create something like an app-level item -- something that only needs to be set once -- that can be referenced to specify the parsing schema, and all of the instances of "reference table owner" used to specify the source for DML? Or (I've not tried this yet) can I just NOT set the value of reference table owner? In my case, the reference table owner for all tables is the same as the parsing schema.
    Thanks,
    Carol

    hi Varad -- I should have been more specific. The application does not need to access multiple databases; rather, it will be installed on multiple databases, operating only on the DB on which it's installed. (Same functionality on each DB, same DB schemas in each, different data in each.)
    So, I'm wondering if there is a way to have the parsing schema somehow be set automatically based on information in the database, or if I'll have to create a slightly different version of the app, w/ different parsing schemas, for each database.
    It was recommended to me (perhaps by you, I can't recall) that the parsing schema be the same as the owner of the tables that the app accesses; the table owner is different in each of these databases, so that is the source of this problem (if indeed it is a problem).
    Thoughts?
    Thanks,
    Carol

  • How can  parse this xml tag " Cell ID="0" Type="String" Afghanistan cell "

    Hi,
    I have to parse this type of xml file.
    so how can i parse this type of xml file like
    <Row ID="1">
    <Cell ID="0" Type="String" >Afghanistan</Cell>
    <Cell ID="1" Type="Number" >93</Cell>
    <Cell ID="2" Type="Number" >0</Cell>
    <Cell ID="3" Type="Number" >1</Cell>
    </Row>

    You haven't given us enough information. Is this for iPhone or Mac OS X? What language are you using? etc.

  • Comparing two dates and adding days to date

    Hi,
    I want to compare two dates(like 2006/10/21 and 2006/11/2),how can i compare these two dates,like which is greater.And if to the second date,i want to add some number of days like 10 days,how can i add so that the date becomes 2006/11/12).
    Please reply soon.Thanks

    No,all other queries are different,as they have the
    date in this format
    '2006-03-06 " but i have the date in this format
    "2006-03-06 10:26:46.0",
    i.e the time is also assciated with it.
    If I have only the date(in string format or date
    date format 2000/12/1),then I can easily do it,but
    I have the time also.If I have the date only,then I
    can split the date into three strings,and after
    converting into integer I can pass it to the
    constructor,but what about the time?SimpleDateFormat can parse "2006-03-06 10:26:46.0" date too:
               SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SS");
               Date date = dateFormat.parse("2006-03-06 10:26:46.0");
               System.out.println("date = "  +date);

  • SimpleDateFormat parses wrong date successfully (lenient = false). Why?

    Hi
    I've got a problem validating date by SimpleDateFormat. Format "yyyy" successfully parses string like "2009-78" into date 01.01.2009. Can you please help me with it?
    Here is my code:
    SimpleDateFormat format = new SimpleDateFormat("yyyy");
    format.setLenient(false);
    String dateStr = "2009-78";
    Date date;
    try {
        date = format.parse(dateStr);
    } catch (ParseException e) {
        date = null;
        e.printStackTrace();
    System.out.println(String.format("String '%s' parsed to date %s", dateStr, date));Output:
    String '2009-78' parsed to date Thu Jan 01 00:00:00 MSK 2009I need an exception to be thrown in such situation. How can I check where the string represents a correct date?
    Thanks for your help.
    Evgeny
    Edited by: su.eug on Apr 13, 2009 12:56 AM

    Read the comments in the API:
    [http://java.sun.com/javase/6/docs/api/java/text/DateFormat.html#parse(java.lang.String)|http://java.sun.com/javase/6/docs/api/java/text/DateFormat.html#parse(java.lang.String)]
    You could try the other overload of parse, if you want to test whether the whole String was used:
    [http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html#parse(java.lang.String, java.text.ParsePosition)|http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html#parse(java.lang.String,%20java.text.ParsePosition)]
    (Sorry--the second link doesn't go to the right place. There is a version of "parse" with two parameters, which you can use to determine if the whole String was used.)

  • SimpleDateFormat parsing errors

    Would someone please change the code so that SimpleDateFormat will parse a string that is shorter than the format. For example:
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    format.setLenient(true);
    format.parse("2003-04"); <-- throws an exception even though it's valid
    Please insert at line 1089
                   if (start >= textLength && this.isLenient()) {
                        break;
                   }

    But I will need to know the version number and date from the file.
    Otherwise, if we aren't looking at the same file then I won't know
    that the line numbers are the same. It will be best if you post
    the first three lines including this information:
    * @(#)SimpleDateFormat.java     x.xx xx/xx/xx
    * That way I can make sure the code you sent goes in the right spot.
    Otherwise it will be my third strike and I will surely be fired.
    Also, I think it's too late to make it into the next release so
    you'll have to wait a while. Until then, I suggest that it's
    possible for you to write your own implementation.

  • How can I parse the document in WebI using sdk?

    I wanna to parse the document in WebIntelligence using sdk. My question is :
    1) By which sdk, I can parse the document.  'Report Application Server SDK' ?
    2) I wanna to parse the 'Self-Defined SQL' and 'Query' components of the document. Can the sdk support this request ?
    My enviroment is  BO XI Release 2.
    Thanks all.

    Hi shao,
    1) By which sdk, I can parse the document. 'Report Application Server SDK' ?
    'Report Application Server SDK' is For Crystal reports so for WebIntelligence or DesktopIntelligence Report it is  "Report Engine SDK".
    Apart from this if you want to do more on these reports "BusinessObjects Enterprise SDK" can be used.
    You can get more information on below link for XI R2.
    http://devlibrary.businessobjects.com/BusinessObjectsXIR2SP2/en/devsuite.htm
    For question 2,
    I am not sure about it but Report Engine SDK provides classes and interface of Data Providers.
    i.e. Building and Editing Data Providers   and  Working with Recordsets.
    Also you can have look on
    Report Engine SDK's
    Interface "Query"
    Hope these helps you.
    Thanks,
    Praveen.

  • Can't parse WSDL when useProxy=true

    Hello everyone.
    I'm using Flex since 1.5 and now try to migrate Flex2.0.
    Most of our application need to access data via WebService,
    and we use <mx:WebService> tag to access them.
    In our Flex1.5 environment, we also use Proxy service of Flex
    (with specifying useProxy=true), and it works fine.
    But in Flex2.0, our mxml application can't parse the same
    WSDL when useProxy=true!
    On the other hand, our application can parse correctly when
    not using proxy service. (useProxy=false and set crossdomain.xml on
    the root of datasource Web server.)
    I'was so confusing that I captured HTTP traffic between
    client application and server( both FlexDataService and Data source
    Web Server), with ServiceCapture.
    When useProxy=true, at first, client application sent request
    to Flex Data Service to get not only calling the SOAP operations
    but also calling WSDL itself. After that, Flex Data Service
    returned the response message as WSDL data, and I found the WSDL
    data was broken.
    When useProxy=false, of cource, the WSDL was passed
    datasource Web Server correctly and application could parse it
    without problem.
    These parse error depends on the WebService.
    I wonder this is some bug of Flex Data Service or not.......

    Hello, everyone.
    After that, I tried to change the encoding of WSDL, which was
    broken when useProxy=true, from UTF-16 to UTF-8.
    And I could get correct WSDL data from Flex Data Service's
    proxy.....
    It seems when the encoding of WSDL is UTF-16, the parse is
    failed, but I'm not sure this problem is happened commonly and this
    is some of the bug of Flex Data Services.

  • How can I parser a String with javax.xml.parsers.DocumentBuilder?

    Normally the DocumentBuilder can parse files, InputSource,InputStream or uri. but what if I want to parse an editing String the user created in an EditPane or JTextComponent?

    new InputSource(new StringReader(theString))

Maybe you are looking for