0

Each timezone has it's own date/time formats. Say for e.g.,

UK >> dd/MM/yyyy    
US >> M/d/yyyy     

for more, https://gist.github.com/mlconnor/1887156

I'm sure Android might've a way to retrive the default date format. Can any guide what would be the simplest way to fetch?

Faisal
  • 1,332
  • 2
  • 13
  • 29

2 Answers2

4

DateTimeFormatterBuilder#getLocalizedDateTimePattern

import java.text.DateFormat;
import java.time.LocalDate;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        for (Locale locale : DateFormat.getAvailableLocales()) {
            String datePattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, null,
                    IsoChronology.INSTANCE, locale);
            System.out.println("Locale: " + locale.getLanguage() + ", Date Format: " + datePattern + ", Today: "
                    + LocalDate.now().format(DateTimeFormatter.ofPattern(datePattern, locale)));
        }
    }
}

Output:

Locale: , Date Format: y-MM-dd, Today: 2020-08-25
Locale: nn, Date Format: dd.MM.y, Today: 25.08.2020
Locale: ar, Date Format: d‏/M‏/y, Today: 25‏/8‏/2020
...
...
...
Locale: ccp, Date Format: d/M/yy, Today: 25/8/20
Locale: br, Date Format: dd/MM/y, Today: 25/08/2020
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
4

from the above input, improved the answer in kotlin with backward compatibility.

 private fun getDefaultDatePattern(): String {

    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        DateTimeFormatterBuilder.getLocalizedDateTimePattern(
            FormatStyle.SHORT,
            null,
            IsoChronology.INSTANCE,
            Locale.getDefault()
        )
    } else {
        SimpleDateFormat().toPattern()
    }
}
Faisal
  • 1,332
  • 2
  • 13
  • 29