5

First: I found a lot of solutions but none of them is working for my issue.

Issue: I want to convert a string date in the format of yyyy-MM-dd to an actaul date, I can do it with the following method that I created but the problem is its only compatible with API Level 26 and above, and my minimum support Level is 19.

    //string to date convert
    @RequiresApi(Build.VERSION_CODES.O)
    fun convertStringDate(dateString: String): LocalDate {
        val format = DateTimeFormatter.ISO_DATE
        val tmpDate = LocalDate.parse("2019-12-10", format)
        var parsedDate: LocalDate = tmpDate
        try {
            parsedDate = LocalDate.parse(dateString, format)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return parsedDate
    }

Question: How can I convert a string in the format yyyy-MM-dd to actual date, so that it is compatible with API Level 19 and above ?

Answer Found: Add desugring in your module level build.gradle file as

implementation coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5")

and also enable

coreLibraryDesugaringEnabled true

in compiler options in the same file

Haris
  • 372
  • 3
  • 16
  • https://stackoverflow.com/questions/8573250/android-how-can-i-convert-string-to-date ? – Dan Baruch Jun 30 '21 at 08:35
  • what do you mean by actual date? Calendar or DateTime or LocalDate? – Eishon Jun 30 '21 at 08:35
  • 1
    LocalDate, as defined in the return type of my method in the question. – Haris Jun 30 '21 at 08:38
  • 1
    Either use [Android API Desugaring](https://developer.android.com/studio/write/java8-support) or import a compatibility library, like [ThreeTenAbp](https://github.com/JakeWharton/ThreeTenABP) – deHaar Jun 30 '21 at 09:01
  • Related: Both [Android - Date in API Level 21 \[closed\]](https://stackoverflow.com/questions/60687806/android-date-in-api-level-21) and [cannot resolve symbol 'java.time.LocalDate' error in android studio](https://stackoverflow.com/questions/28745205/cannot-resolve-symbol-java-time-localdate-error-in-android-studio) (search for yet more). – Ole V.V. Jun 30 '21 at 10:30

3 Answers3

3

You could start using desugaring so you can use java 8 methods in lower min apis not only do you get LocalDate you get lots of other stuff too. https://developer.android.com/studio/write/java8-support#library-desugaring

Per.J
  • 1,229
  • 8
  • 12
1

Try this,it will work no need to use any library

fun getStringToDate() {
    val dtStart = "2021-06-30"
    val format = SimpleDateFormat("yyyy-MM-dd")

    val date = format.parse(dtStart)
    Log.e("date", "date: $date")
}
rNkL
  • 376
  • 3
  • 11
  • 1
    Consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jun 30 '21 at 10:31
0

below code will work irrespective of versions

public static String getDateFormatyyyyMMMddToyyyyMMdd(String string) {
    SimpleDateFormat inputSDF = new SimpleDateFormat("yyyy-MMM-dd", Locale.getDefault());
    SimpleDateFormat outputSDF = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    Date date = null;
    try {
        //here you get Date object from string 
        date = inputSDF.parse(string);
    } catch (ParseException e) {
        return string;
    }
    //after changing date format again you can change to string with changed format
    return outputSDF.format(date);
}
Mohd Qasim
  • 896
  • 9
  • 20
  • Consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jun 30 '21 at 10:31