Remember the date in Java parse error

Source: Internet
Author: User

String datestring = "2014101517";

New SimpleDateFormat ("YYYYMMDDHH"). Parse (datestring)

Does this code produce an error?

The answer is: No.

Here is the direct assignment to datestring a value, so parsing is not a problem.

But when the string is read from the file, be careful.

 Public classParseError {/**     * @paramargs*/     Public Static voidMain (string[] args) {String file= "D:/res/111.txt"; Try{String str= Myreader.readall (Newfileinputstream (file));            System.out.println (str); Date D=NewSimpleDateFormat ("YYYYMMDDHH"). Parse (str);        System.out.println (d); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } Catch(ParseException e) {e.printstacktrace (); }    }}

Like the previous piece of code, its output is this:

2014101517java.text.ParseException:Unparseable Date: "2014101517"at    Java.text.DateFormat.parse ( Dateformat.java:337) at    com.dhcc.jcron.ParseError.main (Parseerror.java:20)

I can't tear her sister.

In the end, it is found in debugging

This is clearly 10 characters how is Count 11?

Open Value again.

The No. 0 bit is an empty one, so the parsing goes wrong.

So what is the reason behind the problem?

Originally this file is format is UTF-8 format, UTF8 with BOM file, will put a 0xefbb in the file header.

But inputstreamreader in Java has read this BOM in.

Knowing this, I believe that the solution should be found soon, and this is not the case.

Remember the date in Java parse error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.