2012-04-14 8 views
-1

メッセージが投稿された後に経過した時間を表示する必要があります。私は入力としてpublisheddateを取る方法を書いており、経過時間を返します。時間の長さを分、時間、日数にするかどうかを判断する

d1.gettime-pubdate.gettime=time in milliseconds. 

私があれば条件

if time==something concat days ago, 
elseif hours ago 
elseif min ago 
elseif seconds ago 
elseif justnow. 

条件があれば、何を含める必要が書いていますか?例えば。日はif(time>=1440)でなければなりません(私は分単位で日付を取る場合、time/1000*60*60で行うことができます)。数分、何時間、何をすべきですか?& c。?

ここに示したアドバイスに基づいて、私は何を書きましたか?改善提案?

public static String TimeElapsed(final Date date) 

{ 

     Date Interval=new Date(); 
     DateTime Interval1 = new DateTime();//Converting java time to joda time 
     Interval1 = new DateTime(date); 
     DateTime Interval2 = new DateTime(); 
     Interval2=new DateTime(Interval); 

     //Now we have two joda time instances 

     Duration dd=new Duration(Interval1,Interval2); 
     //get duration between  given   and current time 
     Duration rd1=new Duration(1000);//seconds 
     Duration rd2=new Duration(60000);//minutes 
     Duration rd7=new Duration(60000*2);//2minutes 
     Duration rd3=new Duration(3600000);//hours 
     Duration rd6=new Duration(3600000*2);//2hours 
     Duration rd4=new Duration(86400000);//days 
     Duration rd5=new Duration(86400000*2);//2days 
     if(dd.isShorterThan(rd1)){ 
      String k="just now"; 
      return k; 
     } 
     else if(dd.isEqual(rd1)){ 
      String k="just now"; 
      return k; 

     } 
     else if(dd.isLongerThan(rd1)&& dd.isShorterThan(rd2)) 
     { 
      String k=dd.getStandardSeconds()+""+" seconds ago"; 
      return k; 
     } 
     else if(dd.isEqual(rd2) || dd.isShorterThan(rd7)){ 
      String k=dd.getStandardMinutes()+""+" minute ago"; 
      return k; 
     } 
     else if(dd.isLongerThan(rd2) && dd.isShorterThan(rd3)){ 
      String k=dd.getStandardMinutes()+""+" minutes ago"; 
      return k; 
     } 
     else if(dd.isEqual(rd3) || dd.isShorterThan(rd6)) 
     { 
      String k=dd.getStandardHours()+""+" hour ago"; 
      return k; 
     } 
     else if(dd.isLongerThan(rd3) && dd.isShorterThan(rd4)){ 
      String k=dd.getStandardHours()+""+"hours ago"; 
      return k; 
     } 
     else if(dd.isEqual(rd4) || dd.isShorterThan(rd5)) { 
      String k=dd.getStandardDays()+""+" day ago"; 
      return k; 
     } 

     else if(dd.isLongerThan(rd4)){ 
      String k=dd.getStandardDays()+""+" days ago"; 
      return k; 
     } 
     else{ 
      String k=""; 
      return k; 
     } 
    } 

答えて

1

あなたはJoda TimeのPeriod.normalizedStandardメソッドのようなものを探していると思います。

参照: Period to string

2

私はJoda-Timeを使用してのwrschneider99の提案に同意するが、IMOあなたは固定数である経過期間の話をしていると仮定しDurationではなくIntervalを、使用する必要があります数ヶ月のようなより人間中心の何かではなく、ミリ秒単位で表現されます。私は「単なるテストのための」実装および「システム時刻に基づいて、」実装でClockインターフェースのいくつかの並べ替えを使用して、あなたのようにそれを注入したい

// Assuming this really is a constant... 
private static final Duration POST_DURATION = Duration.standardDays(2); 

// Then elsewhere... 
Instant expiry = post.getTime().plus(POST_DURATION); 
// See below 
Instant now = clock.now(); 

if (now.isAfter(expiry)) { 
    ... 
} 

注:

私のような何かを示唆していますテスト容易性のために、他の依存関係もあります。詳細については、Kevin Bourrillion's blog post about pure functionsを参照してください。

関連する問題