0

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

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 1
    but prefer the [answer](https://stackoverflow.com/a/20821770/16320675) to that question, using the newer `java.time.LocalDate` – user16320675 May 25 '22 at 14:47

1 Answers1

1

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.

WJS
  • 36,363
  • 4
  • 24
  • 39