A pit of Android and Java APIs: SimpleDateFormat

Source: Internet
Author: User

At work today, an unexpected exception was encountered:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/41/1F/wKiom1PQzc7xIN37AALO7_svFHI872.jpg "title=" Problem stack " alt= "Wkiom1pqzc7xin37aalo7_svfhi872.jpg"/>

The code for the problem is this (the contextual information has been removed):

LOG.I (Log_tag, New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss", Locale.china)
. Format (System.currenttimemillis ()));

Check repeatedly, feel no problem, then create a new Java Project, directly output the same code:

public class main{
public static void Main (string[] args) {
System.out.println (New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss", Locale.china)
. Format (System.currenttimemillis ()));
}
}

The resulting result is this:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/41/1F/wKiom1PQzw3z8mgdAADNqOEXNz0576.jpg "title=" Java Project "alt=" Wkiom1pqzw3z8mgdaadnqoexnz0576.jpg "/>

feel strange, then consult the Android API, in Java.text.SimpleDateFormat In the description table, unlike Java SE, in the description of "year", there is only "Y", there is no "y". When you replace "Y" in your code with lowercase, the problem is resolved.

This is the different pit of the API. I've seen some of them before, but today this is a bit of an accident that I haven't noticed before. Summary: If you have a strange problem with Java standard code in the future, it's a good idea to look at the Android API to make sure that your calls are correct and valid.

This article is from the "Flying Cat" blog, be sure to keep this source http://flyingcat2013.blog.51cto.com/7061638/1529773

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.