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?
CodePudding user response:
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.
CodePudding user response:
Try this code
val sdf = SimpleDateFormat("EEE, MMMM dd, yyyy")
val current = sdf.format(Date())
textView.text = "$current"
CodePudding user response:
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"
CodePudding user response:
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
- Oracle tutorial: Date Time explaining how to use java.time.
- Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - Java 8 APIs available through desugaring
- ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.