-2

I'm trying to display the local date (ie Tuesday, September 14, 2021) in a textview and I'm having a hard time finding a way to do it. Any tips or ideas?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Kieran W
  • 11
  • 2
  • Welcome to Stack Overflow. *I'm having a hard time finding a way to do it.* What have you tried in order to find a way? In what way did it fall short? Showing us the effort that you have made will make many users here prepared to do a greater effort on their part to help you. It will probably also show us which part of your task you’re having trouble with, so we can focus our answers there and thereby help you better and more precisely. – Ole V.V. Sep 17 '21 at 09:09

4 Answers4

2

You can use SimpleDateFormat class to format dates. To acheive the format you specified in question, use this:

textView.text = SimpleDateFormat("EEEE, MMMM dd, yyyy").format(Date())

To understand the meaning of different date and time patterns, checkout this link.

Arpit Shukla
  • 9,612
  • 1
  • 14
  • 40
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. We have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android look into [desugaring](https://developer.android.com/studio/write/java8-support-table). – Ole V.V. Sep 15 '21 at 07:25
1

java.time and a built-in localized format

Consider using java.time, the modern Java date and time API, for your date work. Please excuse my Java syntax. Declare a formatter:

private static final DateTimeFormatter DATE_FORMATTER
        = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(Locale.US);

Use like this:

    LocalDate today = LocalDate.now(ZoneId.systemDefault());
    String text = today.format(DATE_FORMATTER);
    System.out.println(text);

When running today, output was:

Wednesday, September 15, 2021

Now assign this text into your TextView as described in the other answers.

Not only will you want to avoid the troublesome SimpleDateFormat class, you will also want to avoid writing your own format pattern string since this is error-prone. And your wish goes nicely hand in hand with the wish of your users to see the date printed in an appropriate format for their locale. In the above code I used Local.US for demonstration. In real code you will want to leave out that bit. Then the formatter will take on the default locale of the device, and users in all locales will be happy. Simply like this:

private static final DateTimeFormatter DATE_FORMATTER
        = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both 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 non-Android 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 either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I highly recommend using this solution instead of using the broken `java.util` Date-Time API. The `java.util` Date-Time API and their formatting API, `SimpleDateFormat` are outdated and error-prone. It is recommended to stop using them completely and switch to the [modern Date-Time API](https://www.oracle.com/technical-resources/articles/java/jf14-Date-Time.html). – Arvind Kumar Avinash Sep 19 '21 at 09:45
0

Try this code

val sdf = SimpleDateFormat("EEE, MMMM dd, yyyy")
val current = sdf.format(Date())

textView.text = "$current"
Ahmed M. Abed
  • 599
  • 3
  • 9
  • 22
0

Thank You everyone! Both of these solutions worked:

Solution 1:

    val dateDisplay: TextView = findViewById(R.id.date)
    dateDisplay.text = SimpleDateFormat("EEEE, MMMM dd, yyyy").format(Date())

Solution 2:

        val sdf = SimpleDateFormat("EEE, MMMM dd, yyyy")
        val current = sdf.format(Date())
        val dateDisplay: TextView = findViewById(R.id.date)
        dateDisplay.text = "$current"
Ali
  • 1,080
  • 16
  • 22
Kieran W
  • 11
  • 2