0

I am using dd-MMM-yyyy format in String format in kotlin in android.
It returns "01-Sept-2022" as a result.
I want "01-Sep-2022" as result.
This issue shows up in android 12 & 13.

@JvmStatic
fun getCurrentDate(format: String): String {
    val sdf = SimpleDateFormat(format)
    val currentDate = sdf.format(Date())
    return currentDate
}

How can I get my desired return value in all versions?

deHaar
  • 17,687
  • 10
  • 38
  • 51
Anil Ranga
  • 13
  • 3
  • show us the code of how you are formatting the date – Nitish Sep 02 '22 at 05:09
  • @JvmStatic fun getCurrentDate(format: String): String { val sdf = SimpleDateFormat(format) val currentDate = sdf.format(Date()) return currentDate } – Anil Ranga Sep 02 '22 at 05:11
  • 2
    Which `Locale` is your `Locale.getDefault()`? You can try out different `Locale`s in [`SimpleDateFormate(String, Locale)`](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html#SimpleDateFormat(java.lang.String,%20java.util.Locale)), maybe one is reliably formatting as desired. The pattern should already be correct. – deHaar Sep 02 '22 at 06:29
  • Consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. Use [desugaring](https://developer.android.com/studio/write/java8-support-table) in order to use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). It is so much nicer to work with. – Ole V.V. Sep 02 '22 at 18:47
  • Not being an Android developer I don’t know where Android locale data such as abbreviations used in different languages come from. They are likely to reflect what users want, so I wouldn’t worry much. However, if you want to guard against changes in the next version I believe the good solution is using a `DateTimeFormatterBuilder` and its `appendText` method for specifying the exact abbreviations you want. See [here](https://stackoverflow.com/a/52374919/5772882) and [here](https://stackoverflow.com/a/57183455/5772882). – Ole V.V. Sep 02 '22 at 18:54

1 Answers1

0

According to @deHaar SimpleDateFormat returns the formatted string depending on the Locale. Take a look at all locales and you will find the correct form you want. Below I added a snippet where you can easily find it. If you develop this app for the app store or for other public users you shouldn't change the Locale in SimpleDateFormat because the user in this region normally knows this format. The Locale.getDefault is also influenced by the Android system settings.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val out = findViewById<TextView>(R.id.out)
    out.text = getCurrentDate("dd-MMM-yyyy")
}

fun getCurrentDate(format: String): String {
    var result = ""

    val locales = listOf<Locale>(Locale.CANADA, Locale.CANADA_FRENCH, Locale.CHINA, Locale.CHINESE,
        Locale.ENGLISH, Locale.FRANCE, Locale.FRENCH, Locale.GERMAN, Locale.GERMANY, Locale.ITALIAN,
        Locale.ITALY, Locale.JAPAN, Locale.JAPANESE, Locale.KOREA, Locale.KOREAN, Locale.PRC,
        Locale.ROOT, Locale.SIMPLIFIED_CHINESE, Locale.TAIWAN, Locale.TRADITIONAL_CHINESE, Locale.UK, Locale.US)

    locales.forEach {
        val sdf = SimpleDateFormat(format, it)
        result += sdf.format(Date()) + "\n"
    }
    return result
}

returns

04-Sep.-2022
04-sept.-2022
04-9月-2022
04-9月-2022
04-Sep-2022
04-sept.-2022
04-sept.-2022
04-Sept.-2022
04-Sept.-2022
04-set-2022
04-set-2022
04-9月-2022
04-9月-2022
04-9월-2022
04-9월-2022
04-9月-2022
04-Sep-2022
04-9月-2022
04-9月-2022
04-9月-2022
04-Sep-2022
04-Sep-2022
jox
  • 2,218
  • 22
  • 32
Tobias Lukoschek
  • 355
  • 1
  • 3
  • 20