Android:如何获取基于设备时区的默认日期格式 [英] Android: how to get the default date format based on device timezone
问题描述
SimpleDateFormat()
给出日期根据用户区域设置的格式。我正在寻找一种情况,我需要将提供的日期转换为基于时区的日期格式。
The SimpleDateFormat()
gives the date format as per the user locale. I'm in search of a scenario where I need to convert a supplied Date into timezone based date format.
例如,某个用户将其语言环境设置为中文,而在德国时区则应结果格式为 dd.mm.yyyy
Say e.g., A user set her locale as 'Chinese' and in German timezone should result in the date format as dd.mm.yyyy
推荐答案
您可以做到如下:
-
使用地理位置API ,例如通过从URL http: //ip-api.com/json 即可获取国家/地区代码。
Use a Geolocation API to get the country code e.g by parsing the JSON from the URL, http://ip-api.com/json you can get the country code.
通过循环Locale.getAvailableLocales()
,假设使用循环变量 locale
并返回 locale
locale.getCountryCode()。equals(从json获取国家代码)
Loop through Locale.getAvailableLocales()
, let's say using the loop variable, locale
and return the locale
for which locale.getCountryCode().equals(the-country-code-obtained-from-the-json)
使用获得的 Locale
,获取如下所示的日期模式:
With the obtained Locale
, get the date pattern as shown below:
String datePattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, null,
IsoChronology.INSTANCE, locale);
这篇关于Android:如何获取基于设备时区的默认日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!