you have problem because the format of the date is not supported, I invite you to read this article https://www.claudebueno.com/programmation/comment-gerer-la-date-et-lheure-avec-kotlin.htm but in your case if you want that the code runs, change the format of date, like this:
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.LocalDate
fun main() {
//example
val current = LocalDateTime.now()
val formatter = DateTimeFormatter.BASIC_ISO_DATE
val formatted = current.format(formatter)
println("Current Date is: $formatted")
//your code
val dates = /*"20 Aug 2012"*/ "20120820"
val datess = LocalDate.parse(dates, DateTimeFormatter.BASIC_ISO_DATE)
println(datess)
}
Answer from zerbene on Stack Overflowyou have problem because the format of the date is not supported, I invite you to read this article https://www.claudebueno.com/programmation/comment-gerer-la-date-et-lheure-avec-kotlin.htm but in your case if you want that the code runs, change the format of date, like this:
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.LocalDate
fun main() {
//example
val current = LocalDateTime.now()
val formatter = DateTimeFormatter.BASIC_ISO_DATE
val formatted = current.format(formatter)
println("Current Date is: $formatted")
//your code
val dates = /*"20 Aug 2012"*/ "20120820"
val datess = LocalDate.parse(dates, DateTimeFormatter.BASIC_ISO_DATE)
println(datess)
}
tl;dr ⇒ You are using the wrong pattern for parsing
Your date
Stringis of the formatdd MMM uuuu(a non ISO format) but you are trying to parse it with aDateTimeFormatter.ISO_LOCAL_DATEYour datetime
Stringis of the formatMM/dd/uuuu hh:mm:ss a(non ISO) but you are trying to parse it with aDateTimeFormatter.ISO_LOCAL_DATE, which is at least doubly wrong because that formatter tries to parse an ISO date. YourStringis non ISO and contains more information (time of day) than this formatter is able to parse.
There are several built-in DateTimeFormatters, like the one you are currently using, but you need to use a correct one or if there is none, create one that covers your String(s) yourself (either by DateTimeFormatter.ofPattern(...) or by using a DateTimeFormatterBuilder).
Here's a small example for your String examples:
fun main(args: Array<String>) {
// your example Strings
val dateFirst = "20 Aug 2012"
val dateSecond = "12/16/2020 12:00:00 AM"
// you need two different formatters here, your Strings differ in format and content
val firstFormatter = DateTimeFormatter.ofPattern("dd MMM uuuu", Locale.ENGLISH)
val secondFormatter = DateTimeFormatter.ofPattern("MM/dd/uuuu hh:mm:ss a", Locale.ENGLISH)
// then use those according to what you want to parse
val localDate = LocalDate.parse(dateFirst, firstFormatter)
val localDateTime = LocalDateTime.parse(dateSecond, secondFormatter)
// use the built-in formatters for output
println(localDate.format(DateTimeFormatter.ISO_LOCAL_DATE))
println(localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
}
Output (in ISO):
2012-08-20
2020-12-16T00:00:00
I think this will answer your question:
val stringDate = expiration_button.text.toString()
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm");
val dt = LocalDate.parse(stringDate, formatter);
Edit 1:
It's probably crashing because you are using a 12hr Hour, instead of a 24hr pattern.
Changing the hour to 24hr pattern by using a capital H should fix it:
val dateTime = LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));
Use below to convert the time from String to LocalDateTime, but make sure you are getting the time in String form.
String str = "2016-03-04 11:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
Btw, If your String contains seconds as well like "2016-03-04 11:30: 40", then you can change your date time format to yyyy-MM-dd HH:mm:ss" as shown below:
String str = "2016-03-04 11:30: 40";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
You can define optional parts in the pattern of a DateTimeFormatter. In your case, you'd want:
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd['T'HH:mm]")
Where the ['T'HH:mm] defines the optional time part. You can then use this formatter when parsing your strings into LocalDate instances. There are a few ways this can be done, but the easiest and most readable is to use the LocalDate#parse(CharSequence,DateTimeFormatter) method.
Runnable example:
import java.time.LocalDate
import java.time.format.DateTimeFormatter
fun main() {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd['T'HH:mm]")
val dateOnlyString = "2021-06-08"
val dateAndTimeString = "2021-06-08T15:00"
val parsedDateOnly = LocalDate.parse(dateOnlyString, formatter)
val parsedDateAndTime = LocalDate.parse(dateAndTimeString, formatter)
println("parse(\"$dateOnlyString\") --> $parsedDateOnly")
println("parse(\"$dateAndTimeString\") --> $parsedDateAndTime")
}
Output:
parse("2021-06-08") --> 2021-06-08
parse("2021-06-08T15:00") --> 2021-06-08
One more option:
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd['T'HH:mm]")
val dt = formatter.parseBest(str, LocalDateTime::from, LocalDate::from)
Here dt will be instance of LocalDateTime or LocalDate (depending on the str input string).
Note, that you cannot parse input with format "yyyy-MM-dd" into LocalDateTime, even if you use optional parts in the DateTimeFormatter pattern.