-2

How can I convert a String like this "Thu Apr 16 16:45:48 'CST' 2015" in a Date but in the current Date format, like this "EEE MMM dd HH:mm:ss z yyyy"

Razib
  • 10,965
  • 11
  • 53
  • 80

1 Answers1

4
String dateString = "Thu Apr 16 16:45:45 'CST' 2015";
DateFormat dateF = new SimpleDateFormat("EEE MMM DD HH:mm:ss z yyyy");
Date theDate = dateF.parse(dateString);

Edit: There should not be any ' surrounding the CST. You should also add another parameter to the SimpleDateFormat constructor: Locale.your_time_region. Also make sure you handle the ParseException thrown by the parse function.

Slow Trout
  • 492
  • 3
  • 13
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jun 06 '17 at 08:35