How to Reliably Parse the Output of Date.toString()
When attempting to parse the output of new Date().toString(), developers often encounter challenges due to the lack of locale-specific formatting. This can lead to inconsistent and unreliable results.
To resolve this issue, the solution lies in understanding the underlying format specified in the Date#toString() documentation. The format is as follows:
dow mon dd hh:mm:ss zzz yyyy
Where:
Translating this into a SimpleDateFormat pattern, we get:
EEE MMM dd HH:mm:ss zzz yyyy
For example, the German-formatted string "Sun Dec 12 13:45:12 CET 2010" can be parsed using the following SimpleDateFormat:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
By using this pattern, you can reliably parse the output of Date#toString() and convert it into a Date object, regardless of the system locale settings.
The above is the detailed content of How to Reliably Parse the Output of `Date.toString()` in Java?. For more information, please follow other related articles on the PHP Chinese website!