1

Im trying to parse date 31-MAR-22 16:49 with this method, but it doesn't works

it throws java.time.format.DateTimeParseException: Text '30-MAR-22 16:49' could not be parsed at index 3

I passed hard coded date "30-Mar-22 16:49" instead of "30-MAR-22 16:49" diff is "Mar" and it worked. I have to know which pattern is used to convert MAR

 getConvertedDates(date: String?): String {
    val format = "MMM d"
    val formatter = DateTimeFormatter.ofPattern("dd-MMM-yy HH:mm")
    val parsedDate = formatter.parse(date)
    val displayFormatter = DateTimeFormatter.ofPattern(format)
    return displayFormatter.format(parsedDate).toString()
}
misterios
  • 300
  • 1
  • 2
  • 15
  • 1
    `Y` is day of week which is dangerous (it almost does what you want, but not quite). You want `u`. However, I doubt that's what you meant by 'doesn't works'. You need to supply a bit more detail. Do you get compiler errors? runtime errors? A result you did not expect or want? Paste the errors, or if no errors, the result you got vs. the result you want. – rzwitserloot Apr 01 '22 at 03:06
  • Im getting null, I have date in this format -> 31-MAR-22 16:49 and i want to convert it like that -> Mar 31 – misterios Apr 01 '22 at 03:11
  • `"dd-MMM-yy HH:mm"` - the `y` has to be lower case. – Dawood ibn Kareem Apr 01 '22 at 03:15
  • java.time.format.DateTimeParseException: Text '30-MAR-22 16:49' could not be parsed at index 3 – misterios Apr 01 '22 at 03:19
  • I think problem is -> AR . no ? MMM = Mar are not ? – misterios Apr 01 '22 at 03:20
  • not works, same exception – misterios Apr 01 '22 at 03:27
  • I passed hard coded date "30-Mar-22 16:49" instead of "30-MAR-22 16:49" diff is "Mar" and it worked. I have to know which pattern is used to convert MAR – misterios Apr 01 '22 at 03:30
  • 2
    Ah, of course, sorry, had not noticed your month is in all upper case. You need `new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("dd-MMM-uu HH:mm").toFormatter(Locale.ENGLISH)`. Yes, it’s a bit complicated to specify that case should be ignored in parsing. – Ole V.V. Apr 01 '22 at 03:36
  • 1
    [Full working code online here](https://ideone.com/MbsTT9) – Ole V.V. Apr 01 '22 at 03:40
  • thanks very much, it worked and I'll use it , but I think I cant pass review with that :D lets see, thank u one more time – misterios Apr 01 '22 at 03:41

0 Answers0