2017-01-03 2 views
0

これは私がgetTimeBetween関数を呼び出す方法です:2つのZonedDateTimesの時間差を取得し、「4時間、1分、40秒前」のようにきれいに印刷しますか?

getTimeBetween(ZonedDateTime.now().minusHours(4).minusMinutes(1).minusSeconds(40), ZonedDateTime.now()); 

をそして、私はこの出力を期待する:

4 hours, 1 minute, 40 seconds ago 

これは私のgetTimeBetween機能です:

private String getTimeBetween(ZonedDateTime zonedDateTime1, ZonedDateTime zonedDateTime2) { 
    Duration timeDifference = Duration.between(zonedDateTime1, zonedDateTime2); 
    if (timeDifference.getSeconds() == 0) return "now"; 
    String timeDifferenceAsPrettyString = ""; 
    Boolean putComma = false; 
    if (timeDifference.toDays() > 0) { 
     if (timeDifference.toDays() == 1) timeDifferenceAsPrettyString += timeDifference.toDays() + " day"; 
     else timeDifferenceAsPrettyString += timeDifference.toDays() + " days"; 
     putComma = true; 
    } 
    if (timeDifference.toHours() > 0) { 
     if (putComma) timeDifferenceAsPrettyString += ", "; 
     if (timeDifference.toHours() == 1) timeDifferenceAsPrettyString += timeDifference.toHours() + " hour"; 
     else timeDifferenceAsPrettyString += timeDifference.toHours() % 24 + " hours"; 
     putComma = true; 
    } 
    if (timeDifference.toMinutes() > 0) { 
     if (putComma) timeDifferenceAsPrettyString += ", "; 
     if (timeDifference.toMinutes() == 1) timeDifferenceAsPrettyString += timeDifference.toMinutes() + " minute"; 
     else timeDifferenceAsPrettyString += timeDifference.toMinutes() % 60 + " minutes"; 
     putComma = true; 
    } 
    if (timeDifference.getSeconds() > 0) { 
     if (putComma) timeDifferenceAsPrettyString += ", "; 
     if (timeDifference.getSeconds() == 1) timeDifferenceAsPrettyString += timeDifference.getSeconds() + " second"; 
     else timeDifferenceAsPrettyString += timeDifference.getSeconds() % 60 + " seconds"; 
    } 
    timeDifferenceAsPrettyString += " ago"; 
    return timeDifferenceAsPrettyString; 
} 

この機能は期待通りに動作しますが、ありますこれは本当にこれのようにする必要がありますか?おそらくこれを達成するためのより良い方法がありますか?

私はJava 8を使用しています。

+0

より良い方法:1) 'StringBuilder'を使用してください。 2)ヘルパーメソッドに対する共通コード、すなわち4つの「if」ステートメントをリファクタリングする。 – Andreas

答えて

1

どうやってですか?

static String getTimeBetween(ZonedDateTime from, ZonedDateTime to) { 
    StringBuilder builder = new StringBuilder(); 
    long epochA = from.toEpochSecond(), epochB = to.toEpochSecond(); 
    long secs = Math.abs(epochB - epochA); 
    if (secs == 0) return "now"; 
    Map<String, Integer> units = new LinkedHashMap<>(); 
    units.put("day", 86400); 
    units.put("hour", 3600); 
    units.put("minute", 60); 
    units.put("second", 1); 
    boolean separator = false; 
    for (Map.Entry<String, Integer> unit : units.entrySet()) { 
     if (secs >= unit.getValue()) { 
      long count = secs/unit.getValue(); 
      if (separator) builder.append(", "); 
      builder.append(count).append(' ').append(unit.getKey()); 
      if (count != 1) builder.append('s'); 
      secs %= unit.getValue(); 
      separator = true; 
     } 
    } 
    return builder.append(epochA > epochB ? " ago" : " in the future").toString(); 
} 

おそらく、代わりにそれをすべてのメソッド呼び出しをインスタンス化のLinkedHashMapを格納することができますが、これは動作するはずです。

+0

ありがとうございます。非常に役に立ちます。 BTW私は "前"と "将来"は交換されるべきだと思います。 – Defozo

+0

@Defozoこれは意図されたものです。 'from'日付が' to'日付の前にある場合、 'to'日付は将来でなければなりません。私はあなたがパラメータを交換することができると思います:P – Moira

関連する問題