Android根据系统设置获得对应格式的当前日期字符串

来源:互联网 发布:阿迪达斯清风系列 知乎 编辑:程序博客网 时间:2024/05/22 06:22

由于最近一个App需要多语言,这样就会涉及到不同国家的用户对日期格式的需求不一样,为了让用户使用更舒服,尽量按照用户系统设置的日期格式来在App中显示。
直接上代码

public static String formatDateStampString() {        long when = System.currentTimeMillis();        int format_flags = DateUtils.FORMAT_NO_NOON_MIDNIGHT                | DateUtils.FORMAT_ABBREV_ALL;                | DateUtils.FORMAT_SHOW_DATE                 | DateUtils.FORMAT_SHOW_WEEKDAY;        return DateUtils.formatDateTime(AppData.getContext(), when, format_flags);    }

输出结果:

英文:Wed, Apr 12中文:4月12日周三

关于format_flags的具体说明大概有一些这些:
FORMAT_SHOW_TIME
FORMAT_SHOW_WEEKDAY
FORMAT_SHOW_YEAR
FORMAT_SHOW_DATE
FORMAT_NO_MONTH_DAY
FORMAT_12HOUR
FORMAT_24HOUR
FORMAT_CAP_AMPM
FORMAT_NO_NOON
FORMAT_CAP_NOON
FORMAT_NO_MIDNIGHT
FORMAT_CAP_MIDNIGHT
FORMAT_UTC
FORMAT_ABBREV_TIME
FORMAT_ABBREV_WEEKDAY
FORMAT_ABBREV_MONTH
FORMAT_ABBREV_ALL
FORMAT_NUMERIC_DATE

官方的英文解释如下:(自行翻译吧)
If FORMAT_SHOW_TIME is set, the time is shown as part of the date range. If the start and end time are the same, then just the start time is shown.

If FORMAT_SHOW_WEEKDAY is set, then the weekday is shown.

If FORMAT_SHOW_YEAR is set, then the year is always shown. If FORMAT_SHOW_YEAR is not set, then the year is shown only if it is different from the current year, or if the start and end dates fall on different years.

Normally the date is shown unless the start and end day are the same. If FORMAT_SHOW_DATE is set, then the date is always shown, even for same day ranges.

If FORMAT_NO_MONTH_DAY is set, then if the date is shown, just the month name will be shown, not the day of the month. For example, “January, 2008” instead of “January 6 - 12, 2008”.

If FORMAT_CAP_AMPM is set and 12-hour time is used, then the “AM” and “PM” are capitalized. You should not use this flag because in some locales these terms cannot be capitalized, and in many others it doesn’t make sense to do so even though it is possible.

If FORMAT_NO_NOON is set and 12-hour time is used, then “12pm” is shown instead of “noon”.

If FORMAT_CAP_NOON is set and 12-hour time is used, then “Noon” is shown instead of “noon”. You should probably not use this flag because in many locales it will not make sense to capitalize the term.

If FORMAT_NO_MIDNIGHT is set and 12-hour time is used, then “12am” is shown instead of “midnight”.

If FORMAT_CAP_MIDNIGHT is set and 12-hour time is used, then “Midnight” is shown instead of “midnight”. You should probably not use this flag because in many locales it will not make sense to capitalize the term.

If FORMAT_12HOUR is set and the time is shown, then the time is shown in the 12-hour time format. You should not normally set this. Instead, let the time format be chosen automatically according to the system settings. If both FORMAT_12HOUR and FORMAT_24HOUR are set, then FORMAT_24HOUR takes precedence.

If FORMAT_24HOUR is set and the time is shown, then the time is shown in the 24-hour time format. You should not normally set this. Instead, let the time format be chosen automatically according to the system settings. If both FORMAT_12HOUR and FORMAT_24HOUR are set, then FORMAT_24HOUR takes precedence.

If FORMAT_UTC is set, then the UTC time zone is used for the start and end milliseconds unless a time zone is specified. If a time zone is specified it will be used regardless of the FORMAT_UTC flag.

If FORMAT_ABBREV_TIME is set and 12-hour time format is used, then the start and end times (if shown) are abbreviated by not showing the minutes if they are zero. For example, instead of “3:00pm” the time would be abbreviated to “3pm”.

If FORMAT_ABBREV_WEEKDAY is set, then the weekday (if shown) is abbreviated to a 3-letter string.

If FORMAT_ABBREV_MONTH is set, then the month (if shown) is abbreviated to a 3-letter string.

If FORMAT_ABBREV_ALL is set, then the weekday and the month (if shown) are abbreviated to 3-letter strings.

If FORMAT_NUMERIC_DATE is set, then the date is shown in numeric format instead of using the name of the month. For example, “12/31/2008” instead of “December 31, 2008”.

0 0
原创粉丝点击