2017-06-02 58 views
0

我使用TimeUnit在我的Android應用程序之一中使用了TimeUnit。但是我成功了幾天...我想要它直到月和年。我的功能如下....任何人都可以建議我如何增加到一年?具有月份和年份的時間功能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

你讀過答案了嗎?四個答案中的三個答案涉及幾個月和幾年。 –

回答

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將不得不決定。另外,雖然我看到了接受刻度標記,但通常只有代碼答案很少有用,請添加一些解釋。 –