Skip to content

Instantly share code, notes, and snippets.

@mochadwi
Last active June 23, 2021 16:09
Show Gist options
  • Save mochadwi/ba5ea0dae2ea8b2688cab5e91e1a8f6d to your computer and use it in GitHub Desktop.
Save mochadwi/ba5ea0dae2ea8b2688cab5e91e1a8f6d to your computer and use it in GitHub Desktop.
sealed class ParsedDate {
data class Success(val date: Date) : ParsedDate()
data class Failure(val errorOffset: Int) : ParsedDate()
}
fun DateFormat.tryParse(text: String): ParsedDate =
try {
ParsedDate.Success(parse(text))
} catch (e: ParseException) {
ParsedDate.Failure(e.errorOffset)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment