Java.text.ParseException: Unbeatable Date: Thu Jan 19, 2012 8:00 p.m.

I would like to analyze the date. My string date is "Thu January 19, 2012 20:00." And my code to parse:

format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm aaa"); this.settDate(new Timestamp((format.parse(sDate)).getTime())); 

However, this will not work. How can I analyze this date?

Full method:

 public void saveTask(int iDevice, String description, String sDate) throws ParseException { format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm aaa"); this.setiDeviceId(iDevice); this.setsDescription(description); this.settDate(new Timestamp((format.parse(sDate)).getTime())); DatabaseManager.save(this); } 

And an exception:

 java.text.ParseException: Unparseable date: "Thu Jan 19 2012 01:00 AM" 

Debug image:

enter image description here

Thanks!

+6
source share
4 answers

Try it under the code ... Tested and works

  String dateStr = "Thu Jan 19 2012 01:00 PM"; DateFormat readFormat = new SimpleDateFormat( "EEE MMM dd yyyy hh:mm aaa"); DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = readFormat.parse( dateStr ); } catch ( ParseException e ) { e.printStackTrace(); } String formattedDate = ""; if( date != null ) { formattedDate = writeFormat.format( date ); } System.out.println(formattedDate); 

Conclusion 2012-01-19 13:00:00

Hurrah!!! Nice to help !!!

+15
source

Try installing Locale in this case, for example, US:

 SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm aaa",Locale.US); format.parse("Thu Jan 19 2012 08:00 PM"); 
+5
source

What is your default locale? Since the date string is in English, try to parse it in the US locale:

 DateFormat format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm a", Locale.US); 
+1
source

Make sure that this call has a valid value. Place a breakpoint directly on this line and double-check the actual value.

As you can see here http://ideone.com/MBzYn code works fine.

0
source

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


All Articles