2016-09-02 6 views
4

私はJoda Timeを使用しており、ユーザーの希望の形式(note that before Android M, the format could be changed)で日付を表示する必要があります。 Aジョダ日時は希望日付形式の文字列から作成されてDateTimeFormatter、使用してフォーマットすることができ書式JodaTime DateTime優先DateFormatを使用

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

DateFormatは抽象クラスであり、フォーマットパターンへのアクセスメソッドはありません(それぞれの具体的な実装では独自のパターンが処理されるため)。 しかし、どのandroid.text.format.DateFormat.getDateFormat()が返すものは実際にはパターンを提供するSimpleDateFormatです。したがって、あなたのような何かを行うことができます。これは、当分の間動作しますが、それはてgetDateFormat(によって返される実際のクラスとして、100%の将来性はないことに注意してください

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

または

String pattern=format.toLocalizedPattern(); 

)をしてもよいです将来の変化。

0

java.time

Joda-Timeプロジェクトはメンテナンスモードになりました。チームはjava.timeクラスに移動するようアドバイスします。

DateTimeFormatterクラスは、日付/時刻値を表す文字列を生成するときに自動的にローカライズされます。

  • FormatStyle文字列があるべき長いまたは省略決定するために:、ローカライズ指定するには

    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. 
    

  • Locale(a)日の名前、月の名前などを翻訳するための人間言語、および(b)略語、大文字、句読点などの問題を決定する文化的な基準を決定する。

例:あなただけの月と日のヶ月で作業したい場合は

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

MonthDayクラスだけでは行きます。 java.timeについて

MonthDay md = MonthDay.from(zdt); 

java.timeフレームワークは、Java 8に組み込まれており、後にされています。これらのクラスは、java.util.Date.Calendar、& java.text.SimpleDateFormatなどの面倒な古い日時クラスに取って代わります。

Joda-Timeプロジェクトは、今maintenance modeで、java.timeへの移行をアドバイスします。

詳しくはOracle Tutorialをご覧ください。そして、多くの例と説明のためにStack Overflowを検索してください。

java.time機能ThreeTen-BackportでJava 6 & 7に戻って、移植、さらにThreeTenABPAndroidに適合されているの多く(How to use…を参照)。

ThreeTen-Extraプロジェクトでは、追加のクラスでjava.timeを拡張します。このプロジェクトは、将来のjava.timeへの追加の可能性を証明する土台です。ここでは、IntervalYearWeekYearQuarterなどの便利なクラスがあります。

関連する問題