【问题标题】:Joda & Android: How to format a date for display in the user's locale?Joda & Android:如何格式化日期以在用户的​​语言环境中显示?
【发布时间】:2015-05-09 05:33:40
【问题描述】:

我正在使用 Joda 库。 我的要求是显示的日期字符串适合用户的语言环境,即在德国设备 12.12.2014 和美国设备 2014/12/12 上。我发现我可以使用 LocalDate 的 toString() 方法。

LocalDate localDate = LocalDate.now();
localDate.toString("yyyy/MM/dd", Locale.getDefault());

如果我理解正确,我需要提供一个模式,但恕我直言,这违背了指定语言环境的目的。

有人能告诉我吗?

【问题讨论】:

  • 此页面的未来访问者:现在,Home Page of Joda-Time 有此通知:请注意,从 Java SE 8 开始,要求用户迁移到 java .time (JSR-310) - JDK 的核心部分,它取代了这个项目。您可以使用java.time API 检查this answer

标签: android date locale jodatime datetime-format


【解决方案1】:

我的解决方案,它将格式化适合设备区域设置的显示日期:

        DateTimeFormatter fmt = DateTimeFormat.mediumDate();
        String str = fmt.print(localDate);
        getGeburtsdatumEditText().setText(str);

【讨论】:

    【解决方案2】:

    我将此代码用于生成小时和分钟的类似情况:

    public String timeGenerateString( int iHour, int iMinute) {
      if( (iHour   < 0) || (iHour   > 24))  return "";
      if( (iMinute < 0) || (iMinute > 59))  return "";
    
      if( android.text.format.DateFormat.is24HourFormat( getApplicationContext()) == true)
        return  "" + String.format("%02d", iHour) + ":" + String.format("%02d", iMinute);
       else
        if( iHour >= 13)
          return  "" + String.format("%02d", (iHour - 12)) + ":" + String.format("%02d", iMinute) + " PM";
         else
          return  "" + String.format("%02d", iHour) + ":" + String.format("%02d", iMinute) + " AM";
    }
    

    也许这通常适合你。

    【讨论】:

      猜你喜欢
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 2015-05-31
      • 2010-09-10
      • 2020-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多