You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DateUtils.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util;
  23. /**
  24. * Utility methods associated with dates.
  25. */
  26. public final class DateUtils {
  27. /** Private contructor to stop instantiation of class. */
  28. private DateUtils() {
  29. //Shouldn't be used
  30. }
  31. /**
  32. * Tests for and adds one component of the duration format.
  33. *
  34. * @param builder The string builder to append text to
  35. * @param current The number of seconds in the duration
  36. * @param duration The number of seconds in this component
  37. * @param name The name of this component
  38. * @return The number of seconds used by this component
  39. */
  40. private static int doDuration(final StringBuilder builder, final int current,
  41. final int duration, final String name) {
  42. int res = 0;
  43. if (current >= duration) {
  44. final int units = current / duration;
  45. res = units * duration;
  46. if (builder.length() > 0) {
  47. builder.append(", ");
  48. }
  49. builder.append(units);
  50. builder.append(' ');
  51. builder.append(name);
  52. builder.append(units == 1 ? "" : 's');
  53. }
  54. return res;
  55. }
  56. /**
  57. * Formats the specified number of seconds as a string containing the
  58. * number of days, hours, minutes and seconds.
  59. *
  60. * @param duration The duration in seconds to be formatted
  61. * @return A textual version of the duration in words (e.g. '3 days, 1 minute, 4 seconds').
  62. */
  63. public static String formatDuration(final int duration) {
  64. final StringBuilder buff = new StringBuilder();
  65. int seconds = duration;
  66. seconds -= doDuration(buff, seconds, 60 * 60 * 24, "day");
  67. seconds -= doDuration(buff, seconds, 60 * 60, "hour");
  68. seconds -= doDuration(buff, seconds, 60, "minute");
  69. doDuration(buff, seconds, 1, "second");
  70. return buff.length() == 0 ? "0 seconds" : buff.toString();
  71. }
  72. /**
  73. * Appends the specified number as a 0-padded 2 digit time.
  74. *
  75. * @param builder The builder to append the number to.
  76. * @param number The number to be appended.
  77. * @return The given builder, as a convenience.
  78. */
  79. private static StringBuilder appendTime(final StringBuilder builder, final int number) {
  80. if (number < 10) {
  81. builder.append('0');
  82. }
  83. builder.append(number);
  84. return builder;
  85. }
  86. /**
  87. * Formats the specified number of seconds as a string containing the
  88. * number of hours, minutes and seconds.
  89. *
  90. * @param duration The duration in seconds to be formatted
  91. * @return A textual version of the duration as a time (e.g. '03:02:12').
  92. */
  93. public static String formatDurationAsTime(final int duration) {
  94. final StringBuilder result = new StringBuilder();
  95. final int hours = duration / 3600;
  96. final int minutes = duration / 60 % 60;
  97. final int seconds = duration % 60;
  98. if (hours > 0) {
  99. appendTime(result, hours).append(':');
  100. }
  101. appendTime(result, minutes).append(':');
  102. appendTime(result, seconds);
  103. return result.toString();
  104. }
  105. }