Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

datetime - Kotlin DateTimeParseException

Getting date from https://api.spacexdata.com/v3/launches This date have format: 2006-03-25T10:30:00+12:00. I want convert it to "dd, mm, yyyy", but always getting error: "java.time.format.DateTimeParseException: Text '2006-03-25T10:30:00+12:00' could not be parsed, unparsed text found at index 10"

my code:

val formatter = DateTimeFormatter.ofPattern("dd, mm, yyyy", Locale.US)
val myDate = LocalDate.parse(launchDate, formatter)

var launchDateConverted: String=  myDate.toString()

i getting data at String, then i convert it to date for formatting, and after i converting date back to string thats to display at UI. i used different methods, but cannot find the correct way. My current locale is "RU".

question from:https://stackoverflow.com/questions/65908827/kotlin-datetimeparseexception

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Your formatter does not match the input format. Basically you need two formatters: one for input and one for output.
  2. The format "dd, mm, yyyy" is wrong: mm stands for minute of hour, not for month. You should use "dd, MM, yyyy".
val launchDate = "2006-03-25T10:30:00+12:00"

val inputFormatter = DateTimeFormatter.ISO_DATE_TIME
val myDate = LocalDate.parse(launchDate, inputFormatter)

val outputFormatter = DateTimeFormatter.ofPattern("dd, MM, yyyy", Locale.US)
println(outputFormatter.format(myDate))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...