【问题标题】:Format JodaTime DateTime with preferred DateFormat使用首选日期格式格式化 Joda Time DateTime
【发布时间】:2017-01-11 01:02:30
【问题描述】:

我正在使用Joda Time,需要以用户的首选格式 (note that before Android M, the format could be changed) 显示日期。

可以使用 DateTimeFormatter 格式化 Joda DateTime,它是从具有所需日期格式的字符串创建的:

public String getFormattedDate(String datePattern) {
    if (mDate != null) {
        // get your local timezone
        DateTimeZone localTZ = DateTimeZone.getDefault();
        DateTime dt = mDate.toDateTime(localTZ);

        DateTimeFormatter fmt = DateTimeFormat.forPattern(datePattern);
        String formattedDate = dt.toString(fmt);
        return formattedDate;
    }
    return "";
}

但要获得用户的首选格式,您必须使用 Java DateFormat:

public static DateFormat getPreferredDateFormat(Context context) {
    final String format = Settings.System.getString(context.getContentResolver(), Settings.System.DATE_FORMAT);
    DateFormat dateFormat;
    if (android.text.TextUtils.isEmpty(format)) {
        dateFormat = android.text.format.DateFormat.getMediumDateFormat(context.getApplicationContext());
    } else {
        dateFormat = android.text.format.DateFormat.getDateFormat(context.getApplicationContext()); // Gets system date format
    }

    if (dateFormat == null)
        dateFormat = new SimpleDateFormat(format);

    return dateFormat;
}

Java DateFormat 没有一种方法可以给我一个带有日期格式的字符串。

那么有没有办法用 Java DateFormat 格式化 Joda DateTime?也许还指定我只想显示日期和月份(将是 dd/MM 或 MM/dd)?还是让 DateTimeFormatter 采用用户喜欢的格式?

【问题讨论】:

    标签: java android datetime jodatime date-formatting


    【解决方案1】:

    DateFormat 是一个抽象类,因此没有对格式模式的访问方法(因为每个具体实现都会处理自己的模式)。 然而,android.text.format.DateFormat.getDateFormat() 返回的实际上是一个SimpleDateFormat,它提供了模式。因此,您可以执行以下操作:

    SimpleDateFormat format=(SimpleDateFormat)DateFormat.getDateFormat(context.getApplicationContext());
    String pattern=format.toPattern();
    

    String pattern=format.toLocalizedPattern();
    

    这暂时有效,但请注意,它不是 100% 未来证明,因为 getDateFormat() 返回的实际类将来可能会发生变化。

    【讨论】:

      【解决方案2】:

      java.time

      Joda-Time 项目现在处于维护模式。团队建议改用java.time 课程。

      DateTimeFormatter 类可以在生成表示日期时间值的字符串时自动本地化。

      Instant instant = Instant.now();  // Current moment in UTC with a resolution of up to nanoseconds.
      ZoneId z = ZoneId.of( "America/Montreal" );  // Specify a time zone.
      ZonedDateTime zdt = instant.atZone( z );  // Adjust from UTC to a specific time zone. Same moment, different wall-clock time.
      

      要本地化,请指定:

      • FormatStyle 判断字符串应该是长还是缩写。
      • Locale 确定 (a) 用于翻译日期名称、月份名称等的人类语言,以及 (b) 决定缩写、大写、标点符号等问题的文化规范。

      例子:

      Locale l = Locale.CANADA_FRENCH ; 
      DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( l );
      String output = zdt.format( f );
      

      如果您只想处理月份和日期,MonthDay 类就是这样。

      MonthDay md = MonthDay.from( zdt );
      

      关于java.time

      java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧日期时间类,例如java.util.Date.Calendarjava.text.SimpleDateFormat

      Joda-Time 项目现在位于maintenance mode,建议迁移到 java.time。

      要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。

      大部分 java.time 功能在ThreeTen-Backport 中向后移植到Java 6 和7,并进一步适应ThreeTenABP 中的Android(参见How to use…)。

      ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如IntervalYearWeekYearQuarter 等等。

      【讨论】:

        【解决方案3】:
        private String getCurrentDate() {
            DateTime dateTimeUtc = new DateTime( DateTimeZone.UTC );
        
            DateTimeZone timeZone = DateTimeZone.forID( "Africa/Khartoum" );
            
            java.util.Locale locale = new Locale( "ar", "SD" ); // ( language code, country code );
            DateTimeFormatter formatter = DateTimeFormat.forStyle( "FF" ).withLocale( locale ).withZone( timeZone );
            String output = formatter.print( dateTimeUtc );
        
        
            return  output.substring(output.indexOf(output.charAt(0)),output.indexOf(':') - 2);
        }
        

        【讨论】:

        • 我使用此代码仅从较长格式中提取日期。基本上,获取从开始到第一个分号之前的字符的子字符串。所有这些都在 return 语句中。 :)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 2015-01-24
        • 2014-02-14
        • 1970-01-01
        • 1970-01-01
        • 2013-12-18
        • 1970-01-01
        相关资源
        最近更新 更多