Generic way to parse dates in Java

后端 未结 7 859
醉酒成梦
醉酒成梦 2021-01-19 00:03

What is the best way to parse dates in Java ? Is there any built in way to parse strings such as \"18 Jul 2011\" , \"Jul 18, 2011\", \"18-07-2011\", \"2011-07-18\" without k

7条回答
  •  失恋的感觉
    2021-01-19 00:41

    You can use DateFormat.parse(...) and use a locale for the dateformat, but that still might not handle all those cases. It's also not that easy: consider the following two possible dates (which are the same): 18/07/2011 and 07/18/2011 - how should that be parsed without knowing whether it is dd/mm/yyyy or mm/dd/yyyy (e.g. in Saudi Arabia)?

提交回复
热议问题