SHARE
    TWEET
    ferrybig

    TimeConvertor

    Dec 5th, 2014
    749
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    Java 2.72 KB | None | 0 0
    1. public class TimeConvertor {
    2. private TimeConvertor() {}
    3. private enum TimeUnits {
    4. MILISECONDS("miliseconds"),
    5. SECONDS("seconds", 1000, MILISECONDS),
    6. MINUTES("minutes", 60, SECONDS),
    7. HOURS("hours", 60, MINUTES),
    8. DAYS("days", 24, HOURS),
    9. WEEKS("weeks", 7, DAYS),
    10. MONTHS("months", 4, WEEKS),
    11. YEARS("years", 365, DAYS);
    12. private TimeUnits(String friendlyName, long commonDivider, TimeUnits parent) {
    13. this(friendlyName, commonDivider * parent.getCommonDivider());
    14. }
    15. private TimeUnits(String friendlyName, long commonDivider) {
    16. this.friendlyName = friendlyName;
    17. this.commonDivider = commonDivider;
    18. }
    19. private TimeUnits(String friendlyName) {
    20. this(friendlyName, 1);
    21. }
    22. private final String friendlyName;
    23. private final long commonDivider;
    24. public long getCommonDivider() {
    25. return commonDivider;
    26. }
    27. public String getFriendlyName() {
    28. return friendlyName;
    29. }
    30. }
    31. public static String convertTime(long time, int maxStringComponents, String delimiter) {
    32. StringBuilder builder = new StringBuilder();
    33. final TimeUnits[] values = TimeUnits.values();
    34. long tempTime= 0;
    35. long tempTimeBuilding;
    36. for (int i = 0; i < maxStringComponents; i++) {
    37. TimeUnits lowestUnit = null;
    38. for (TimeUnits unit : values) {
    39. tempTimeBuilding = time / unit.getCommonDivider();
    40. if (tempTimeBuilding > 0) {
    41. tempTime = tempTimeBuilding;
    42. lowestUnit = unit;
    43. } else {
    44. break;
    45. }
    46. }
    47. if(lowestUnit == null) {
    48. break;
    49. }
    50. if (i != 0) {
    51. builder.append(delimiter);
    52. }
    53. time -= tempTime * lowestUnit.getCommonDivider();
    54. builder.append(tempTime).append(' ').append(lowestUnit.getFriendlyName());
    55. }
    56. return builder.toString();
    57. }
    58. public static void main(String ... args) {
    59. System.out.println(convertTime(1, 3, ", "));
    60. System.out.println(convertTime(10, 3, ", "));
    61. System.out.println(convertTime(100, 3, ", "));
    62. System.out.println(convertTime(1000, 3, ", "));
    63. System.out.println(convertTime(10000, 3, ", "));
    64. System.out.println(convertTime(100000, 3, ", "));
    65. System.out.println(convertTime(1000000, 3, ", "));
    66. System.out.println(convertTime(345127364, 3, ", "));
    67. System.out.println(convertTime(345127364, 30, ", "));
    68. }
    69. }
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /