Java IRC bot
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.md87.charliebravo;
  6. /**
  7. *
  8. * @author chris
  9. */
  10. public class Formatter {
  11. /**
  12. * Tests for and adds one component of the duration format.
  13. *
  14. * @param builder The string builder to append text to
  15. * @param current The number of seconds in the duration
  16. * @param duration The number of seconds in this component
  17. * @param name The name of this component
  18. * @return The number of seconds used by this component
  19. */
  20. private static int doDuration(final StringBuilder builder, final int current,
  21. final int duration, final String name) {
  22. int res = 0;
  23. if (current >= duration) {
  24. final int units = current / duration;
  25. res = units * duration;
  26. if (builder.length() > 0) {
  27. builder.append(", ");
  28. }
  29. builder.append(units);
  30. builder.append(' ');
  31. builder.append(name + (units != 1 ? 's' : ""));
  32. }
  33. return res;
  34. }
  35. /**
  36. * Formats the specified number of seconds as a string containing the
  37. * number of days, hours, minutes and seconds.
  38. *
  39. * @param duration The duration in seconds to be formatted
  40. * @return A textual version of the duration
  41. */
  42. public static String formatDuration(final int duration) {
  43. final StringBuilder buff = new StringBuilder();
  44. int seconds = duration;
  45. seconds -= doDuration(buff, seconds, 60*60*24, "day");
  46. seconds -= doDuration(buff, seconds, 60*60, "hour");
  47. seconds -= doDuration(buff, seconds, 60, "minute");
  48. seconds -= doDuration(buff, seconds, 1, "second");
  49. return buff.toString();
  50. }
  51. }