【问题标题】:Show date based on the locale settings根据区域设置显示日期
【发布时间】:2016-04-27 00:00:42
【问题描述】:

我正在开发一个能够获取帖子时间信息的应用程序。

我想要以毫秒为单位显示时间以使其用户友好,但我需要注意在 android 设备的设置中定义的日期格式。

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

当我能够确定格式时,我只需要获取日期和月份。一年对我来说毫无用处。

我已经完成了下面的代码,但我不喜欢我使用子字符串的事实,因为如果 01/02 变成 1/2 它不起作用

DateFormat.getDateInstance(DateFormat.SHORT).format(new Date(tweetsCreatedTime)).substring(0,5)

tweetsCreatedTime 以毫秒为单位,定义为 long

最好不要使用本地,而是获取设置并确保即使本地显示 EN 或 US,用户也不会更改其显示方式。

谢谢

【问题讨论】:

    标签: android date locale


    【解决方案1】:

    我用DateFormat:

    java.text.DateFormat dateFormat = DateFormat.getTimeFormat(context);
    String dateString = dateFormat.format(date);
    

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

    【讨论】:

      【解决方案2】:

      java.time

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

      有关详细信息,请参阅几乎相同的问题 How can I format Date with Locale in Android 中的 my Answer

      简短的示例代码,将给定输入的 long 数字变量的误称 tweetsCreatedTime 更改为 tweetMillisecondsSinceEpoch。请注意,虽然您的输入以毫秒为单位,但 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

      【讨论】:

        【解决方案3】:

        您好,使用 SimpleDateFormater 它允许使用您自己的格式和任何日期,无论设备设置如何,这是我一直使用的自定义方法,即使您可以设置本地您希望它显示日期

         /**
         * 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 定义本地 ...

        【讨论】:

        • 谢谢,但您强制采用 SimpleDateFormat 格式。有没有办法从设备的设置中获取这种格式
        • 我认为您可以从设备获得的是使用了本地的巫婆,并且可以使用 Locale.getDefault(); 找到该值;但是日期的显示方式取决于您,如果语言环境是法语使用法语格式显示它或者语言环境是英语(很可能;)使用英语格式显示日期
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多