Guest User

Untitled

a guest
Dec 11th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. java TimeCalculator 443432s 34234m 34323m 32344234s 8h 23d 12h
  2.  
  3. 450 days 22 hours 18 minutes 6 seconds
  4.  
  5. public class TimeCalculator {
  6. private static final int MINUTE_IN_SECONDS = 60;
  7. private static final int HOUR_IN_SECONDS = 3600;
  8. private static final int DAY_IN_SECONDS = 86400;
  9.  
  10. private static int seconds = 0;
  11.  
  12. public static void main(String[] args) {
  13. if (args.length == 0 || args[0].contains("help")) {
  14. showHelp();
  15. return;
  16. }
  17.  
  18. for (String arg : args) {
  19. timeStringToSeconds(arg);
  20. }
  21.  
  22. printNewTimeString();
  23. }
  24.  
  25. private static void timeStringToSeconds(String timeString) {
  26. if (timeString.contains("s")) {
  27. timeString = timeString.replace("s", "");
  28. seconds += Integer.parseInt(timeString);
  29. } else if (timeString.contains("m")) {
  30. timeString = timeString.replace("m", "");
  31. seconds += Integer.parseInt(timeString) * MINUTE_IN_SECONDS;
  32. } else if (timeString.contains("h")) {
  33. timeString = timeString.replace("h", "");
  34. seconds += Integer.parseInt(timeString) * HOUR_IN_SECONDS;
  35. } else if (timeString.contains("d")) {
  36. timeString = timeString.replace("d", "");
  37. seconds += Integer.parseInt(timeString) * DAY_IN_SECONDS;
  38. }
  39. }
  40.  
  41. private static void printNewTimeString() {
  42. String newTimeString = new String();
  43. // calculate days
  44. int days = 0;
  45. while (seconds > DAY_IN_SECONDS) {
  46. days++;
  47. seconds -= DAY_IN_SECONDS;
  48. }
  49. if (days > 0) {
  50. newTimeString += days + " days ";
  51. }
  52.  
  53. // calculate hours
  54. int hours = 0;
  55. while(seconds > HOUR_IN_SECONDS) {
  56. hours++;
  57. seconds -= HOUR_IN_SECONDS;
  58. }
  59. if (hours > 0) {
  60. newTimeString += hours + " hours ";
  61. }
  62.  
  63. // calculate minutes
  64. int minutes = 0;
  65. while (seconds > MINUTE_IN_SECONDS) {
  66. minutes++;
  67. seconds -= MINUTE_IN_SECONDS;
  68. }
  69. if (minutes > 0) {
  70. newTimeString += minutes + " minutes ";
  71. }
  72.  
  73. // calculate seconds
  74. if (seconds > 0) {
  75. newTimeString += seconds + " seconds ";
  76. }
  77.  
  78. System.out.println(newTimeString);
  79. }
  80.  
  81. private static void showHelp() {
  82. System.out.println("This Program converts time strings to an ordered string that makes the time");
  83. System.out.println("information better understandablen");
  84. System.out.println("You can enter something like this: ");
  85. System.out.println("java TimeCalculator 534s 400d 32453s 234h");
  86. System.out.println("The output will be this: ");
  87. System.out.println("410 days 3 hours 9 minutes 47 seconds");
  88. System.out.println("Available characters: d, h, m, s");
  89. }
  90. }
Add Comment
Please, Sign In to add comment