Java DATE Parsing

I have a dumb problem with java.util.Date .

I have this line of code, but I donโ€™t understand why this date cannot be matched with this format.

 public class TestTime { public static void main(String[] args) { final String DATE_FORMAT = "EEE MMM dd HH:mm:ss zzz yyyy"; String date = "Sat Dec 31 10:00:00 CET 2011"; SimpleDateFormat dFormat = new SimpleDateFormat(DATE_FORMAT); Date lDate = null; try { lDate = dFormat.parse(date); } catch (ParseException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } 
+4
source share
3 answers

If your system uses a language other than English, you need to use this constructor:

 SimpleDateFormat(DATE_FORMAT,Locale.ENGLISH); 

If this is not a problem, you should format the date using the same formatter and compare the result with your input string.

+10
source

I see nothing wrong with that. It runs for me without errors and returns:

 Sat Dec 31 09:00:00 GMT 2011 
+5
source

This seems to be a locale related issue.

If I install French, the template does not work. If I set Locale to US in the SimpleDateFormat constructor, it works.

SimpleDateFormat dFormat = new SimpleDateFormat (DATE_FORMAT, Locale.US);

+1
source

Source: https://habr.com/ru/post/1385562/


All Articles