I\'m trying to parse a String to a Date Object, as part of a loading functionality for my program, as seen below:
String dateString = \"Wed Jan 18 23:22:18 E
The hour should be HH
, not hh
h Hour in am/pm (1-12)
And year should be yyyy
not YYYY
Y Week year
ALWAYS consult the JavaDocs first
String dateString = "Wed Jan 18 23:22:18 EST 2017";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
try {
Date scheduleItemDate = sdf.parse(dateString);
System.out.println(dateString);
System.out.println(scheduleItemDate.toString());
System.out.println(sdf.format(scheduleItemDate));
} catch (ParseException e) {
System.out.println("Exception");
}
Which now prints...
Wed Jan 18 23:22:18 EST 2017
Wed Jan 18 23:22:18 EST 2017
Wed Jan 18 23:22:18 EST 2017