Skip to content

Instantly share code, notes, and snippets.

@mattmook
Created September 21, 2021 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattmook/147ce7609c6697d0f3a333b9d1c01469 to your computer and use it in GitHub Desktop.
Save mattmook/147ce7609c6697d0f3a333b9d1c01469 to your computer and use it in GitHub Desktop.
private val ddMMMMyyyyRegex = "([0-9]{1,2})\\s+([A-Za-z]{3,4})\\s+([0-9]{4})".toRegex()
private val monthMapping = mapOf(
"Jan" to Month.JANUARY,
...
"Sep" to Month.SEPTEMBER,
"Sept" to Month.SEPTEMBER,
...
"Dec" to Month.DECEMBER
)
fun String.toLocalDate(): LocalDate? {
return ddMMMMyyyyRegex.matchEntire(this)?.groupValues?.let {
val (_, day, month, year) = it
LocalDate.of(year.toInt(), monthMapping[month], day.toInt())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment