2017-01-06 4 views
2

私はDateUtils.getRelativeTimeSpanString()ポストが行われたときに表示するために使用しています。 1分前、私は上の(短縮と前なし)1メートル又は1 Hなどになりたい:それは返し時間の前に表示Twitterのように

ストリングは、そのようなものです。

「時間」を「h」または「分」と「m」で置き換えることはできますが、言語が英語と異なる場合は機能しません。これは現在Twitterが使用しているものです。 英語/キリル文字 Twitterで表示する方法。

enter image description here

enter image description here

UPDATE:私はここに(再びJodaTimeパッケージを使用して)クリーナーソリューションが追加されますが、私は@Bradleyウィルソンから答えを受け入れます。残りの回答も同様の結果に変更できるので投票に値する。すべて、ありがとう:

DateTime postMaded = new DateTime(your previous date); 
    DateTime nowUpdate = new DateTime(); 

    Period period = new Period(postMaded, nowUpdate); 

    PeriodFormatter formatter; 

    Locale current = ConverterMethods.getCurrentLocale(); 

    if (current.getLanguage().contentEquals(any Cyrillic language)) { 

     if (period.getYears() != 0) { 
      formatter = new PeriodFormatterBuilder().appendYears().appendSuffix(" г.").printZeroNever().toFormatter(); 
     } else if (period.getMonths() != 0) { 
      formatter = new PeriodFormatterBuilder().appendMonths().appendSuffix(" м").printZeroNever().toFormatter(); 
     } else if (period.getWeeks() != 0) { 
      formatter = new PeriodFormatterBuilder().appendWeeks().appendSuffix(" седм.").printZeroNever().toFormatter(); 
     } else if (period.getDays() != 0) { 
      formatter = new PeriodFormatterBuilder().appendDays().appendSuffix(" д").printZeroNever().toFormatter(); 
     } else if (period.getHours() != 0) { 
      formatter = new PeriodFormatterBuilder().appendHours().appendSuffix(" ч").printZeroNever().toFormatter(); 
     } else if (period.getMinutes() != 0) { 
      formatter = new PeriodFormatterBuilder().appendMinutes().appendSuffix(" мин").printZeroNever().toFormatter(); 
     } else { 
      formatter = new PeriodFormatterBuilder().appendSeconds().appendSuffix(" с").printZeroNever().toFormatter(); 
     } 

    } else { 

     if (period.getYears() != 0) { 
      formatter = new PeriodFormatterBuilder().appendYears().appendSuffix(" y").printZeroNever().toFormatter(); 
     } else if (period.getMonths() != 0) { 
      formatter = new PeriodFormatterBuilder().appendMonths().appendSuffix(" mon").printZeroNever().toFormatter(); 
     } else if (period.getWeeks() != 0) { 
      formatter = new PeriodFormatterBuilder().appendWeeks().appendSuffix(" w").printZeroNever().toFormatter(); 
     } else if (period.getDays() != 0) { 
      formatter = new PeriodFormatterBuilder().appendDays().appendSuffix(" d").printZeroNever().toFormatter(); 
     } else if (period.getHours() != 0) { 
      formatter = new PeriodFormatterBuilder().appendHours().appendSuffix(" h").printZeroNever().toFormatter(); 
     } else if (period.getMinutes() != 0) { 
      formatter = new PeriodFormatterBuilder().appendMinutes().appendSuffix(" m").printZeroNever().toFormatter(); 
     } else { 
      formatter = new PeriodFormatterBuilder().appendSeconds().appendSuffix(" s").printZeroNever().toFormatter(); 
     } 
    } 
+1

Joda-Time(https://github.com/dlew/joda-time-android)には、そのようなことをするための素晴らしいオプションがいくつかあります。 –

+0

'Period'、' PeriodFormatter'、 'PeriodFormatterBuilder'クラスを含むパッケージを指定すると役に立ちます – RustamG

+0

私はそれを追加しました。それを指摘してくれてありがとう。 – charbinary

答えて

1

あなたがこれを達成するためにJodaTimeを使用できることを行うには、この機能を使用することができます。

はそうのようなアプリのレベルBuild.Gradleファイルへの依存関係を追加します。 :

compile 'net.danlew:android.joda:2.9.4.1' 

次に簡単に実装できますこの機能。

private String time = "Just Now"; 

private String how_long_ago(String created_at) { 
     DateTime sinceGraduation = new DateTime(created_at, GregorianChronology.getInstance()); 
     DateTime currentDate = new DateTime(); //current date 

     Months diffInMonths = Months.monthsBetween(sinceGraduation, currentDate); 
     Days diffInDays = Days.daysBetween(sinceGraduation, currentDate); 
     Hours diffInHours = Hours.hoursBetween(sinceGraduation, currentDate); 
     Minutes diffInMinutes = Minutes.minutesBetween(sinceGraduation, currentDate); 
     Seconds seconds = Seconds.secondsBetween(sinceGraduation, currentDate); 

     Log.d("since grad", "before if " + sinceGraduation); 
     if (diffInDays.isGreaterThan(Days.days(31))) { 
      time = diffInMonths.getMonths() + " months ago"; 
      if (diffInMonths.getMonths() == 1) { 
       time = diffInMonths.getMonths() + " month ago"; 
      } else { 
       time = diffInMonths.getMonths() + " months ago"; 
      } 
      return time; 
     } else if (diffInHours.isGreaterThan(Hours.hours(24))) { 
      if (diffInDays.getDays() == 1) { 
       time = diffInDays.getDays() + " day ago"; 
      } else { 
       time = diffInDays.getDays() + " days ago"; 
      } 
      return time; 
     } else if (diffInMinutes.isGreaterThan(Minutes.minutes(60))) { 
      if (diffInHours.getHours() == 1) { 
       time = diffInHours.getHours() + " hour ago"; 
      } else { 
       time = diffInHours.getHours() + " hours ago"; 
      } 
      return time; 
     } else if (seconds.isGreaterThan(Seconds.seconds(60))) { 
      if (diffInMinutes.getMinutes() == 1) { 
       time = diffInMinutes.getMinutes() + " minute ago"; 
      } else { 
       time = diffInMinutes.getMinutes() + " minutes ago"; 
      } 
      return time; 
     } else if (seconds.isLessThan(Seconds.seconds(60))) { 
      return time; 
     } 
     Log.d("since grad", "" + sinceGraduation); 
     return time; 
    } 

ただ必要な「1メートル」

を達成するためにtimeためString出力を変更する:あなたは、あなたのリソースを文字列を変更するためにローカライズを使用することができます。

詳細はAndroid Localization Tutorialを参照してください。

2

あなたは

public static String getTimeAgo(long time) { 
    if (time < 1000000000000L) { 
    // if timestamp given in seconds, convert to millis 
    time *= 1000; 
    } 

    long now = System.currentTimeMillis(); 
    if (time > now || time <= 0) { 
    return null; 
    } 

    // TODO: localize 
    final long diff = now - time; 
    if (diff < MINUTE_MILLIS) { 
    return "just now"; 
    } else if (diff < 2 * MINUTE_MILLIS) { 
    return "a minute ago"; 
    } else if (diff < 50 * MINUTE_MILLIS) { 
    return diff/MINUTE_MILLIS + " min ago"; 
    } else if (diff < 90 * MINUTE_MILLIS) { 
    return "an hour ago"; 
    } else if (diff < 24 * HOUR_MILLIS) { 
    return diff/HOUR_MILLIS + " hours ago"; 
    } else if (diff < 48 * HOUR_MILLIS) { 
    return "yesterday"; 
    } else { 
    return diff/DAY_MILLIS + " days ago"; 
    } 
} 
+1

いいえ、言語が英語でないと、あなたのケースでは入力されないので、私はできません。たとえば、キリル文字の場合、分/分は "минута/мин"です。 – charbinary

+0

ローカリゼーション用の文字列リソースを使用することができます。 –

4

PrettyTimeライブラリをご覧ください。

それは使用することが非常に簡単です:

import org.ocpsoft.prettytime.PrettyTime

PrettyTime p = new PrettyTime(); 
System.out.println(p.format(new Date())); 
// prints "moments ago" 

OUはまた、国際化されたメッセージのロケールに渡すことができます。

PrettyTime p = new PrettyTime(new Locale("fr")); 
System.out.println(p.format(new Date())); 
// prints "à l'instant" 

コメントで述べたように、Androidはこの機能を備えていますandroid.text.format.DateUtilsクラスに組み込まれています。あなたは、あなたの質問に指定されているように動作します私のライブラリTime4Aを使用して

+0

DateUtils.getRelativeTimeSpanString()を使用しています。しかし、すべての言語で出力をカスタマイズしたいと思います。画像を見る – charbinary

+0

まずこのライブラリ 'PrettyTime'を試してみてください。 –

+0

1つの問題:私は、省略形を実現する方法が見当たりません。例として、この[リソース](https://github.com/ocpsoft/prettytime/blob/master/core/src/main/java/org)も参照してください。 /ocpsoft/prettytime/i18n/Resources_ja.java)。 OPは、構成可能なテキスト幅の強い希望を持っているようです。私の答えも見てください。 –

1

:(前-語で)

印刷相対時間前に、言葉もなく

PlainTimestamp now = PlainTimestamp.nowInSystemTime(); 
    PlainTimestamp earlier = now.minus(1, ClockUnit.MINUTES); 
    Moment past = earlier.inStdTimezone(); 
    Locale russian = new Locale("ru"); 

    PrettyTime.of(Locale.ENGLISH).printRelativeInStdTimezone(past); 
    // output: 1 minute ago 

    PrettyTime.of(Locale.ENGLISH) 
     .withShortStyle().printRelativeInStdTimezone(past); 
    // output: 1 min. ago 

    PrettyTime.of(russian).printRelativeInStdTimezone(past); 
    // output: 1 минуту назад 

    PrettyTime.of(russian).withShortStyle().printRelativeInStdTimezone(past); 
    // output: 1 мин. назад 

印刷フラット回

IsoUnit days = CalendarUnit.DAYS; 
    IsoUnit hours = ClockUnit.HOURS; 
    IsoUnit minutes = ClockUnit.MINUTES; 
    net.time4j.Duration<IsoUnit> d = 
     Duration.in(Timezone.ofSystem(), days, hours, minutes) 
      .between(earlier, now); 

    PrettyTime.of(Locale.ENGLISH).print(d, TextWidth.WIDE); 
    // output: 1 minute 

    PrettyTime.of(Locale.ENGLISH).print(d, TextWidth.ABBREVIATED); 
    // output: 1 min 

    PrettyTime.of(Locale.ENGLISH).print(d, TextWidth.NARROW); 
    // output: 1m 

    PrettyTime.of(russian).print(d, TextWidth.WIDE); 
    // output: 1 минута 

    PrettyTime.of(russian).print(d, TextWidth.ABBREVIATED); 
    // output: 1 мин 

    PrettyTime.of(russian).print(d, TextWidth.NARROW); 
    // output: 1 мин 

最新のライブラリバージョンでは、 CLDR-repository v30.0.2のデータ(Unicode-consortiumから、実際には80言語)。必要に応じて、Time4Aと一緒に配布する独自の資産を定義して(デフォルトの資産をオーバーライドして)テキストリソースを変更することもできます。

関連する問題