The linux server Gson conversion throws Caused by: java.text.ParseException: Unparseable date exception, the fastest solution

1. Problem occurs

linuxWhen deploying a backend service on    a server given by a customer a few days ago , this exception jarwas suddenly thrown . Caused by: java.text.ParseException: Unparseable dateAfter checking the code, we found that when we called the user center interface, we used the object conversion after getting the data gson, and we saw from the log that it was an error thrown during the date conversion.

2. Problem Analysis

   Because this problem did not occur when we were developing locally, we did not think of Gsonsetting the date format on the object at that time. But when deploying to linuxthe server, this error is reported, because it is a microservice project, and there are still many services, adding one by one is too troublesome, and, what I think of first is the server system problem, not the code problem, what problems can there be in the code? ? ? .
  Based on such doubts, I then inquired with the operation and maintenance boss, because it is a new server from the customer, I still doubt the default settings in their server, such as whether the server's time format, time zone, encoding, etc. are normal linux.
  In the end, I found something different.

3. Problem Solving

   When linuxI entered localethe command on the server, I found the situation as shown in the figure below:
insert image description here
   This means that the current server is encoded in English format. Anyway, everything else is normal, but this is wrong. Change it first, and then I feel that it is the problem. Then linuxdo the following operations on the server:
   enter cd ~or just enter cd, return to the home directory (some are not the root directory), and execute the following commands in sequence.

echo 'export LANG=zh_CN.UTF-8' >> .bash_profile

echo 'export LC_ALL="zh_CN.UTF-8"' >> .bash_profile

   Execute the above commands one by one, and then enter it localeto find that it has changed to the following.
insert image description here
   Because I don’t understand operation and maintenance, the operation and maintenance suggestion is to write it into the configuration file of linux, so as to prevent some servers from changing back after reconnecting. question.
   Finally, restart the service, try again, and find that gsonthe conversion is normal.

4. Afterthought

   Although the most correct way, most people may think that the time format should be defined when writing, such as this:

Gson gson= new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

   But writing this way, as I remember, it seems that gsonI only know the time in this format. Of course, the interface generally does not change the format, so it is correct to write it this way.
   In the end, I thought, because the English encoding is not the way our country defines time, what yyyy-MM-dd, everyone writes like this:
insert image description here
   because our interface is written in the Chinese encoding, so why do we need to change it to zh_CN.UTF-8encoding.
   Having said so much, in fact, you only need to execute the next two lines of commands on the server.

Guess you like

Origin blog.csdn.net/wohaqiyi/article/details/122033836