How can I convert a Java string to a date in this format - "31 Jan 2022". I've tried the code below.
Date sDate;
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
sDate = format.parse("20/12/2002");
return sDate.toString();
It's giving an output in this format "Fri Jan 30 00:00:00" which is not what I want
"31 Jan 2022"is not in the format"dd/MM/yyyy". Are you sure that your input is what you say it is?DateandSimpleDateFormat, as they are obsolete classes. They're also troublesome. You are better off using classes from thejava.timepackage. In your case, where you only seem to need a date without time and timezone, you may want to useLocalDate.String❷ Pass this string as an argument toDateTimeFormatter::ofPattern❸ Use your string and theDateTimeFormatterinstance as arguments toLocalDate::parse.