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 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2006-2013 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
  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. seconds -= doDuration(buff, seconds, 1, "second");
  70. return buff.length() == 0 ? "0 seconds" : buff.toString();
  71. }
  72. }