Android: How To Get Default Date/time Format?
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 Androi
Solution 1:
from the above input, improved the answer in kotlin with backward compatibility.
privatefungetDefaultDatePattern(): String {
returnif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
DateTimeFormatterBuilder.getLocalizedDateTimePattern(
FormatStyle.SHORT,
null,
IsoChronology.INSTANCE,
Locale.getDefault()
)
} else {
SimpleDateFormat().toPattern()
}
}
Solution 2:
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;
publicclassMain {
publicstaticvoidmain(String[] args) {
for (Locale locale : DateFormat.getAvailableLocales()) {
StringdatePattern= 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-25Locale:nn,Date Format:dd.MM.y,Today:25.08.2020Locale:ar,Date Format:d/M/y,Today:25/8/2020.........Locale:ccp,Date Format:d/M/yy,Today:25/8/20Locale:br,Date Format:dd/MM/y,Today:25/08/2020
Post a Comment for "Android: How To Get Default Date/time Format?"