I am receiving email arrival Date like "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)", this needs to converted into this format "2020-02-11 16:05:00". Can anyone please help me to achieve this Date conversion?
Partially formed Input Date format like : EEE, d MMM yyyy HH:mm:ss
Can anyone give me exact date format for my input Date?
What i have tried:
try
{
String date_s = "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)";
SimpleDateFormat simpledateformat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
Date tempDate=simpledateformat.parse(date_s);
SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Output date is = "+outputDateFormat.format(tempDate));
} catch (Exception ex)
{
ex.printStackTrace();
}
Exception like below:
java.text.ParseException: Unparseable date: "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)"
at java.text.DateFormat.parse(DateFormat.java:366)
at JavaPackage.DateConvertion.main(DateConvertion.java:12)
Awaiting for your response.
Note: Just for Date Format Identification purpose , randomly given above converted Date.
The presence of spaces in your string looks funny. If the spaces always occur in those places, use the answer by Arvind Kumar Avinash. If there are variations in the occurrence of spaces, you can handle them by enclosing the spaces in square brackets in the format pattern string, thus:
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern(
"EEE,[ ]d MMM uuuu H[ ]:[ ]mm[ ]:[ ]ss xx[ ]'('z')'", Locale.ENGLISH);
String strDateTime = "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)";
OffsetDateTime odt = OffsetDateTime.parse(strDateTime, dtfInput);
System.out.println(odt);
Output:
2020-07-14T03:15:03Z
Square brackets in the format pattern string enclose optional parts, so the formatter above will parse strings that have or don’t have each of those spaces.
Oracle tutorial: Date Time explaining how to use java.time.