Question

I have an application that uses SimpleDateFormat to parse dates. I suddenly had the urge to change my computer's display time format to a Chinese format and then left it at that to do something else.

When I went back to run my application several minutes later, I was getting "Unparseable date" errors for some reason. This never happened before; I never introduced any new code, so I was wondering what went wrong.

I then traced my actions and remembered I changed the format of the time display, so I changed it back and the application runs as usual.

Now, looking into the docs, I see that the library is locale-sensitive. Fortunately, this application is only for personal use where I have full control over my computer's settings. However, if I were to deploy an application for public consumption, I have no control over their environment, and so if I ever had a client that wasn't using US time format, they will be unhappy.

  1. Is this an issue in all datetime-parsing libraries?
  2. Are there "locale insensitive" libraries that will transparently handle locale detection for you? If so, what should I use?
  3. Should I avoid SimpleDateFormat completely because of this behaviour?
Was it helpful?

Solution

The issue is that format parsing depends on locale. To be safe you have to explicitly send expected locale when creating instance of SimpleDateFormat. Otherwise it takes your defult system locale that can be confusing.

This happened in your case. Your program worked well. Then you changed "time format of your computer", that is changed your locale. Then your program stopped working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top