Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 根据区域设置显示日期_Android_Date_Locale - Fatal编程技术网

Android 根据区域设置显示日期

Android 根据区域设置显示日期,android,date,locale,Android,Date,Locale,我正在开发一个应用程序,它能够获得一篇文章的时间信息 我想要的是以毫秒为单位显示进入日期的时间,以方便用户使用,但我需要注意android设备设置中定义的日期格式 如果是dd/mm/yyyy或mm/dd/yyyy或yyyy/dd/mm 当我能够确定格式时,我只需要得到日期和月份。一年对我来说是无用的 我已经完成了下面的代码,但我不喜欢使用substring,因为如果01/02变为1/2,它将不起作用 DateFormat.getDateInstance(DateFormat.SHORT).for

我正在开发一个应用程序,它能够获得一篇文章的时间信息

我想要的是以毫秒为单位显示进入日期的时间,以方便用户使用,但我需要注意android设备设置中定义的日期格式

如果是dd/mm/yyyy或mm/dd/yyyy或yyyy/dd/mm

当我能够确定格式时,我只需要得到日期和月份。一年对我来说是无用的

我已经完成了下面的代码,但我不喜欢使用substring,因为如果01/02变为1/2,它将不起作用

DateFormat.getDateInstance(DateFormat.SHORT).format(new Date(tweetsCreatedTime)).substring(0,5)
tweetsCreatedTime以毫秒为单位,定义为长

与其使用local,不如获取设置,并确保即使local显示EN或US,用户也不会更改其显示方式


感谢您使用SimpleDataFormat,它允许您在任何日期使用自己的格式,而不考虑设备设置。这是一种我一直使用的自定义方法,即使您可以设置希望它显示日期的本地格式

 /**
 * Get localized date string (Using given locale)
 *
 * @param dateString Date string
 * @param locale     Desired locale
 * @return Formatted localized date string
 */
public static String formatLocalized(String dateString, Locale locale) {
    Date date = formatDate(dateString, locale);
    SimpleDateFormat iso8601Format = new SimpleDateFormat("d MMM yyyy", locale);
    iso8601Format.setTimeZone(TimeZone.getTimeZone("UTC"));
    return iso8601Format.format(date);

}
使用Locale.ENGLISH | Locale.FRENCH定义本地语言

java.time 您应该使用java.time包中的现代日期类

有关详细信息,请参见一个几乎相同的问题

简短的示例代码,将给定输入的
long
number变量的命名错误
tweetsCreatedTime
更改为
tweetmillissecondssinceepoch
。请注意,虽然您的输入以毫秒为单位,但java.time类实际上能够以纳秒为单位获得更高的分辨率

Instant instant = Instant.ofEpochMilli( tweetMillisecondsSinceEpoch );  // Number of milliseconds since the POSIX epoch of first moment of 1970 in UTC. 
ZoneId zoneId = ZoneId.of( "Pacific/Auckland" );  // Arbitrary choice of time zone. Crucial it determining the date, as date varies with a new day dawning earlier to the east.
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );  // Apply a time zone.
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM );  // Let java.time translate human language and determine cultural norms in formatting.
formatter = formatter.withLocale( Locale.CANADA_FRENCH );  // Arbitrarily choosing Québec as the Locale. Note that Locale has *nothing* to do with time zone. You could use Chinese locale for a time zone in Finland. 
String output = zdt.format( formatter );  // Generate String representation of our date-time value.
2015-05-23

我使用:


请记住,它还返回名为的类,但它是一个不同的类

谢谢,但是您强制使用SimpleDataFormat中的格式。有没有一种方法可以从设备的设置中获取此格式?我认为您可以从设备中获得的是使用local,并且可以使用Locale.getDefault()找到该值;但日期的显示方式取决于您,如果区域设置为法语,则使用法语格式显示日期,或者如果区域设置为英语(很可能;),则使用英语格式显示日期
java.text.DateFormat dateFormat = DateFormat.getTimeFormat(context);
String dateString = dateFormat.format(date);