1

I'm getting a date as input in the format of 31-AUG-2018. I would like to convert it into AUGUST31,2018 using java

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ArunBharath
  • 143
  • 2
  • 17
  • I guess you just tried doing it after googling for that. Perhaps you can provide some code that does not work for you. – Frito Oct 24 '18 at 15:39
  • 2
    Possible duplicate of [How can I change the date format in Java?](https://stackoverflow.com/questions/3469507/how-can-i-change-the-date-format-in-java) – Joe C Oct 24 '18 at 15:40
  • 1
    @JoeC Not a duplicate as this Question here involves the issue of localized Month name, with further complication of being all-uppercase. Likely this *is* a duplicate but not of [that one](https://stackoverflow.com/questions/3469507/how-can-i-change-the-date-format-in-java). – Basil Bourque Oct 24 '18 at 15:55
  • What have you tried so far? – TheJavaGuy-Ivan Milosavljević Oct 24 '18 at 16:01

1 Answers1

1

Parsing text.

DateTimeFormatter inputFormatter = 
        new DateTimeFormatterBuilder()
        .parseCaseInsensitive()
        .appendPattern("dd-MMM-uuuu")
        .toFormatter(Locale.ENGLISH)
;

DateTimeFormatter outputformatter =
        DateTimeFormatter.ofPattern("MMMMdd,uuuu", Locale.ENGLISH)
;

String input = "31-AUG-2018";
LocalDate date = LocalDate.parse(input, inputFormatter);

Generating text.

String output = date.format(outputformatter).toUpperCase(Locale.ENGLISH);

System.out.println("Converted to: " + output);

This snippet outputs:

Converted to: AUGUST31,2018

The all caps in both input and output require a bit of special treatment. For the input I am using a DateTimeFormatterBuilder and its parseCaseInsensitive method. This gives a formatter that will parse the month abbreviation in uppercase or lowercase or any mix of them. For the output I saw no better option than calling toUpperCase on the result.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161