When SimpleDateFormat is garbled like ٢٠١٨١٠٠٤٠٨٣١٣٣٦٥٧

Conclusion

Always specify Locale for SimpleDateFormat!

Cause

If Locale is not specified, SimpleDateFormat may be converted depending on the locale of the terminal. If it is a Middle Eastern country, it may have adopted a system called "Indian numerals" [^ 1]. Then, "20181004083133657" will be converted to "٢٠١٨١٠٠٤٠٨٣١٣٣٦٥٧".

Solution

Always specify Locale.US!

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS", Locale.US);

By the way, if you set the locale to new Locale ("ar", "EG") , this phenomenon can be reproduced on the Android device, but for some reason it could not be reproduced on macOS.

[^ 1]: [Indian Numerals-Wikipedia](https://ja.wikipedia.org/wiki/%E3%82%A4%E3%83%B3%E3%83%89%E6%95%B0%E5 % AD% 97)

Recommended Posts

When SimpleDateFormat is garbled like ٢٠١٨١٠٠٤٠٨٣١٣٣٦٥٧
SimpleDateFormat is thread unsafe
E-mail attachment name is garbled
[Windows] Java code is garbled
fields_for is used like this
When the character string passed to C ++ by JNA is garbled
When I edit the properties file, it looks like garbled characters
When JDBC connection pooling is created
Escape processing when searching for LIKE
Check when moss with SimpleDateFormat parse