You are using the built-in date format for your locale, which is a good idea. It’s simple, you are exploiting that someone knows what that format looks like, and your code lends itself well to internationalization. When you do that, you can choose how long or short of a format you want. You probably did something equivalent to the following:
ZoneId zone = ZoneId.of("Asia/Karachi");
Locale pakistan = Locale.forLanguageTag("en-PK");
DateTimeFormatter mediumFormatter = DateTimeFormatter
.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(pakistan);
LocalDate today = LocalDate.now(zone);
System.out.println(today.format(mediumFormatter));
15-Feb-2019
In my snippet I have specified a medium format. I think that your best bet is to use the short format instead:
DateTimeFormatter shortFormatter = DateTimeFormatter
.ofLocalizedDate(FormatStyle.SHORT).withLocale(pakistan);
System.out.println(today.format(shortFormatter));
15/02/2019
This uses slashes instead of hyphens. I would trust that this is how people in your culture generally will expect to see a date when written in a short format. And you’re saved of your string manipulation or other hand formatting.
In my snippets I am using java.time, the modern Java date and time API. Calendar
and DateFormat
are long outdated, the latter in particular notoriously troublesome. The modern API is so much nicer to work with.
Disclaimer: I have run the snippets on my Java 10. Output on Android may vary. I wouldn’t be too worried. In all cases the built-in localized formats have been chosen with care.
Question: Can I use java.time on Android?
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