Gson Date Conversion Issues

Source: Internet
Author: User
Tags dateformat i18n locale

Ext.: http://blog.csdn.net/liao_leo/article/details/44593095

Today encountered a very strange problem, Gson parse date string, local execution can, server execution on the error.


This problem is generally related to the environment, to Gson source code to see the Datetypedapter this class,

Private final DateFormat Enusformat
= Dateformat.getdatetimeinstance (Dateformat.default, Dateformat.default, locale.us);
Private final DateFormat LocalFormat
= Dateformat.getdatetimeinstance (Dateformat.default, Dateformat.default);
Private final DateFormat Iso8601format = Buildiso8601format ();

private static DateFormat Buildiso8601format () {
DateFormat Iso8601format = new SimpleDateFormat ("Yyyy-mm-dd ' T ' HH:mm:ss ' Z '", locale.us);
Iso8601format.settimezone (Timezone.gettimezone ("UTC"));
return iso8601format;
}

It has several DataFormat classes of parse date strings, one for each attempt, and an error if unsuccessful.


2015-03-24 14:54:14 Our date format is this, the local to go no problem, the server went on the error. LocalFormat and Enusformat are related to the local language, and some of its formatting classes are in this Localdata.jar, and Java gets the native language and then loads different classes to get the formatting information. My local is ZH_CN, the server is en_US, Local is able to convert 2015-03-24 14:54:14 this, because its format is exactly yyyy-mm-dd this, but the DataFormat on the server Partern is en_US, Unable to parse 2015-03-24 14:54:14, the error was made.

Workaround, modify the Linux

1. Modify/etc/sysconfig/i18n
English version of the system:
Lang= "en_US. UTF-8 "
Sysfont= "Latarcyrheb-sun16"

Chinese Version system:
Lang= "ZH_CN. UTF-8 "
Sysfont= "Latarcyrheb-sun16"


2.source/etc/sysconfig/i18n

3. Restart the service

Gson Date Conversion Issues

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.