简体   繁体   中英

java android DateFormat ignores modified locale settings

In my app for displaying the date I use the following code:

public static String format (Date date) {
    DateFormat formater = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);
    return formater.format(date);
}

In my Android device as default regional date format I have "Regional (31.10.2013)" and then my app works as expected.

However I can change in my Android settings date format to ex. "2013.10.31" and then unfortunately my app seems to ignore those settings and still display date in default format "31.10.2013" . Why? How to format date to take into consideration those Android settings?

This works correctly as you are using DateFormat.getDateTimeInstance .
According to the API doc -

Gets the date/time formatter with the given date and time formatting styles for the default locale .

For your case, it's better to use SimpleDateFormat and specify the format yourself. For example:

private String printStandardDate(Date date) {
    return new SimpleDateFormat("dd/MM/yy").format(date);
}

You can check this so - Using DateFormat.getDateTimeInstance().format(date);

UPDATE: can you please check this-

public static String format (Date date) {
  DateFormat dateFormat = 
         android.text.format.DateFormat.getDateFormat(getApplicationContext());
    return dateFormat.format(date);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM