首页 文章

使用首选DateFormat格式化JodaTime DateTime

提问于
浏览
5

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

可以使用DateTimeFormatter格式化Joda DateTime,该DateTimeFormatter是从具有所需日期格式的String创建的:

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没有一个方法可以给我一个带有日期格式的String .

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

2 回答

  • 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()返回的实际类可能在将来发生变化 .

  • 3

    java.time

    Joda-Time项目现在处于维护模式 . 该团队建议搬到java.time class .

    生成表示日期时间值的String时,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确定字符串应该是long还是缩写 .

    • 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 等 .

相关问题