Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. enum class RangeValueType{
  2. Int,
  3. Date,
  4. String
  5. }
  6.  
  7. fun parseValue(value: String): Pair<RangeValueType, Any> {
  8. val rangeDate: LocalDateTime
  9. val rangeInt: Int
  10. //try date first
  11. try {
  12. //ISO_LOCAL_DATE_TIME e.g. 2011-12-03T10:15:30 for Dec. 3rd, 2011 & 10:15 30 sec.
  13. //can't use basic date b/c e.g. 20111203 is indistinguishable from an int.
  14. rangeDate = LocalDateTime.parse(value)
  15. //it parsed to a date, so return the date
  16. return Pair(RangeValueType.Date, rangeDate)
  17. }catch (dtpe: DateTimeParseException) {
  18. //not a date
  19. }
  20. //try number next
  21. try{
  22. rangeInt = value.toInt()
  23. //it parsed to an int, so return the int
  24. return Pair(RangeValueType.Int, rangeInt)
  25. }catch (nfe: NumberFormatException)
  26. {
  27. //not an int
  28. }
  29. //not date or int, must be a string
  30. return Pair(RangeValueType.String, value)
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement