How do i convert the below date:
Apr 29, 2022 to 04/29/20222
So basically my input date in format Mon dd, yyyy should be converted to mm/dd/yyyy
How do i convert the below date:
Apr 29, 2022 to 04/29/20222
So basically my input date in format Mon dd, yyyy should be converted to mm/dd/yyyy
Use DateTimeFormatter and LocalDate
String date = "Apr 29, 2022";
DateTimeFormatter fromFormat = DateTimeFormatter.ofPattern("MMM dd, yyyy")
.withLocale(Locale.US);
DateTimeFormatter toFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate localDate = LocalDate.parse(date,fromFormat);
System.out.println(localDate.format(toFormat));
prints
04/29/2022
For other useful date/time processing classes check out the java.time package.