0

Now display the date as follows:

    val calendar = Calendar.getInstance()
    val currentYear = calendar.get(Calendar.YEAR)
    val currentMonth = calendar.get(Calendar.MONTH)
    val currentDay = calendar.get(Calendar.DAY_OF_MONTH)

    pickDate.setOnClickListener {
        val datePickDialog = DatePickerDialog(
            activity,
            DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
                val dateFormat = SimpleDateFormat("d MMMM yyyy")
                calendar.set(year, month, dayOfMonth)
                val dateString = dateFormat.format(calendar.time)
                currentDateView.text = dateString
            },
            currentYear,
            currentMonth,
            currentDay
        )
        datePickDialog.show()
    }

It is possible to do as it is easier?

Morozov
  • 4,968
  • 6
  • 39
  • 70
  • Consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [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. Sep 19 '19 at 09:25
  • Possible duplicate of [Printing out datetime in a specific format in Java?](https://stackoverflow.com/questions/40715424/printing-out-datetime-in-a-specific-format-in-java) – Ole V.V. Sep 19 '19 at 09:27
  • @OleV.V. I don't see a code sample there are some examples of how to implement this library? – Morozov Sep 19 '19 at 09:41
  • Hey you just changed your question? Didn't my answer helped you ? Now my answer doesn't make sense :/ – Somesh Kumar Sep 19 '19 at 09:44
  • 1
    The first snippet in t[he accepted answer](https://stackoverflow.com/a/40715452/5772882) to the suggested original question uses java.time. The code using the backport in ThreeTenABP would be the same. Also see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). Hope it helps? – Ole V.V. Sep 19 '19 at 09:45
  • @SomeshKumar I see your point and understand your frustration. Still, as I have perceived the situation, the OP has not changed the question into a new and different one. Rather the original question wasn’t clear. While we may argue that you answered the question as it stood, I don’t think you answered the question that the OP *had intended to ask*. Which is why the OP edited the question and made it clearer. Such editing is pretty common on Stack Overflow. And yes, questioners should take to care to make the question as clear and unambiguous as possible the first time. We’re all just human. – Ole V.V. Sep 19 '19 at 10:19

1 Answers1

1

It cannot be done a lot easier. Two suggestions for simplifications:

  1. Use java.time, the modern Java date and time API. In this case we substitute two old classes, Calendar and Date, with just one new class, LocalDate. So it saves us a conversion. Also the classes you used are long outdated, and generally java.time is much nicer to work with.
  2. Use the built-in localized date format for the user’s locale rather than writing a format pattern string. The latter tends to be error-prone, and using the built-in format lends itself much better to internationalization.

The code below is not tested, there’s probably a typo or two, but you should get the idea.

    val date = LocalDate.now(ZoneId.of("Africa/Abidjan"))
    val currentYear = date.year
    val currentMonth = date.monthValue
    val currentDay = date.dayOfMonth

    val dateFormatter = DateTimeFormatter.ofLocalizedDate(TextStyle.MEDIUM)

    pickDate.setOnClickListener {
        val datePickDialog = DatePickerDialog(
            activity,
            DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
                val selectedDate = LocalDate.of(year, month + 1, dayOfMonth)
                val dateString = selectedDate.format(dateFormatter)
                currentDateView.text = dateString
            },
            currentYear,
            currentMonth - 1,
            currentDay
        )
        datePickDialog.show()
    }

Insert your desired time zone where I put Africa/Abidjan. Use ZoneId.systemDefault for the default time zone (this is what the code in your question used). I have also taken the formatter out of the event listener. There’s no need to construct a new formatter each time.

Question: Can I use java.time on Android? I in project use min API 21

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • LocalDate from java time use min Api 26, but i in project use min 21 Mb as variant i can use `org.joda.time.LocalDate`? – Morozov Sep 20 '19 at 07:49
  • Yes, you can use Joda-Time instead, there will probably be minor changes to the code. But if you’re fine with an external dependency, I see no reason why you should prefer Joda-TIme, in maintenance mode, over its successor, java.time, backported and adapted for Android in ThreeTenABP, which too you can use on API levels lower than 26. You may want to read the section *Question: Can I use java.time on Android?* once more. – Ole V.V. Sep 20 '19 at 07:55
  • 1
    Thx, but I'm not sure that this solution is more easier, as it was necessary to add additional dependencies and create a class application but perhaps in the future it will really help to avoid many problems. – Morozov Sep 20 '19 at 08:19