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

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