How do I parse a string like "Fri Feb 14 09:58:21 EST 2020"
into a Date in Java ?
The closest example I can find is :
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
String dateInString = "Friday, Jun 7, 2013 12:10:56 PM";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
}
catch (ParseException e) {
e.printStackTrace();
}
But it doesn't have the " EST " part, what's the right way to do it ?
Your pattern is not correct, it contain many problems :
EEEE
replace with EEE
,
remove this because your string not have comma,
same thing for the second commayyyy
and put in the enda
remove as it is not used at all, and instead use z
to match ETC
If you are using java-time you can parse your date using this pattern EEE MMM dd HH:mm:ss z yyyy
:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"EEE MMM dd HH:mm:ss z yyyy",
Locale.US);
ZonedDateTime zdt = ZonedDateTime.parse(str, formatter);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments