2017-06-02 1 views
0

私は時間の関数をTimeUnitで使用しています。しかし、私は数日で成功しています...私は月と年までそれを望みます。私の機能は以下のようなものです....誰も私にどのように私は年まで増加することができます示唆することができますか?Time Ago関数(月と年のAndroidを使用)

private String displayQuoteTime(long seconds) { 

    int day = (int) TimeUnit.SECONDS.toDays(seconds); 
    long hours = TimeUnit.SECONDS.toHours(seconds) 

      - TimeUnit.DAYS.toHours(day); 
    long minute = TimeUnit.SECONDS.toMinutes(seconds) 
      - TimeUnit.DAYS.toMinutes(day) 
      - TimeUnit.HOURS.toMinutes(hours); 
    long second = TimeUnit.SECONDS.toSeconds(seconds) 
      - TimeUnit.DAYS.toSeconds(day) 
      - TimeUnit.HOURS.toSeconds(hours) 
      - TimeUnit.MINUTES.toSeconds(minute); 
    String postTime = ""; 
    if (day > 0) { 
     if (day == 1) { 
      postTime = day + " day"; 
     } else { 
      postTime = day + " days"; 
     } 
    } else if (hours > 0) { 
     if (TextUtils.isEmpty(postTime)) { 
      if (hours == 1) { 
       postTime = hours + " hour"; 
      } else { 
       postTime = hours + " hours"; 
      } 

     } else { 
      postTime = postTime + " " + hours + " hours"; 
     } 

    } else if (minute > 0) { 
     if (TextUtils.isEmpty(postTime)) { 
      if (minute == 1) { 
       postTime = minute + " min"; 
      } else { 
       postTime = minute + " mins"; 
      } 
     } else 
      postTime = postTime + " " + minute + " mins"; 
    } else { 
     if (TextUtils.isEmpty(postTime)) 
      postTime = "second"; 
    } 
    postTime = postTime + " ago"; 

    System.out.println("Day " + day + " Hour " + hours + " Minute " 
      + minute + " Seconds " + second); 
    return postTime; 
} 

私はまだ学んでいます....誰かが私の問題を解決できるかどうか教えてください。ありがとう

+0

@ OleV.V。それは重複していません....あなたはそこまでしか見ることができません....月など... –

+0

あなたは答えをお読みですか? 4つの答えのうちの3つが月と年に接触します。 –

答えて

0
private static String displayQuoteTime(long seconds) { 

    int day = (int) TimeUnit.SECONDS.toDays(seconds); 
    long hours = TimeUnit.SECONDS.toHours(seconds) 

      - TimeUnit.DAYS.toHours(day); 
    int months = day/30; 
    int years = months/12; 
    long minute = TimeUnit.SECONDS.toMinutes(seconds) 
      - TimeUnit.DAYS.toMinutes(day) 
      - TimeUnit.HOURS.toMinutes(hours); 
    long second = TimeUnit.SECONDS.toSeconds(seconds) 
      - TimeUnit.DAYS.toSeconds(day) 
      - TimeUnit.HOURS.toSeconds(hours) 
      - TimeUnit.MINUTES.toSeconds(minute); 
    String postTime = ""; 
    if (years > 0) { 
     if (TextUtils.isEmpty(postTime)) { 
      if (years == 1) { 
       postTime = years + " year"; 
      } else { 
       postTime = years + " years"; 
      } 

     } else { 
      postTime = postTime + " " + years + " years"; 
     } 
    } 
     else if (months > 0) { 
     if (TextUtils.isEmpty(postTime)) { 
      if (months == 1) { 
       postTime = months + " month"; 
      } else { 
       postTime = months + " months"; 
      } 
     } else 
      postTime = postTime + " " + months + " months"; 
    } else if (day > 0) { 
     if (day == 1) { 
      postTime = day + " day"; 
     } else { 
      postTime = day + " days"; 
     } 
    } else if (hours > 0) { 
     if (TextUtils.isEmpty(postTime)) { 
      if (hours == 1) { 
       postTime = hours + " hour"; 
      } else { 
       postTime = hours + " hours"; 
      } 

     } else { 
      postTime = postTime + " " + hours + " hours"; 
     } 

    } else if (minute > 0) { 
     if (TextUtils.isEmpty(postTime)) { 
      if (minute == 1) { 
       postTime = minute + " min"; 
      } else { 
       postTime = minute + " mins"; 
      } 
     } else 
      postTime = postTime + " " + minute + " mins"; 
    }else { 
     if (TextUtils.isEmpty(postTime)) 
      postTime = "second"; 
    } 
    postTime = postTime + " ago"; 

    System.out.println(" years " + years + " months " + months + " Day " + day + " Hour " + hours + " Minute " 
      + minute + " Seconds " + second); 
    return postTime; 
} 
+0

月に30日かかりますか?本当に正確ではない/完璧ですが、十分に良いかもしれませんが、OPは決定する必要があります。また、私は受け入れ目盛りを見ている間、一般的にコードだけの回答はほとんど役に立たない、いくつかの説明を追加してください。 –