Advertisement
Guest User

Untitled

a guest
Oct 16th, 2015
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2. public class TimeCalc
  3. {
  4. public static void main(String[] args)
  5. {
  6. int seconds = Integer.parseInt(JOptionPane.showInputDialog(
  7. "Enter any number of seconds greater than zero:"));
  8. String outputMessage;
  9. if (seconds <= 0)
  10. outputMessage = "Invalid input received!";
  11. else
  12. {
  13. outputMessage = String.format("%d seconds is equal to:\n", seconds);
  14. final int SECONDS_PER_DAY = 86400;
  15. int days = 0;
  16. if (seconds >= SECONDS_PER_DAY)
  17. {
  18. days = seconds/SECONDS_PER_DAY;
  19. seconds = seconds - days * SECONDS_PER_DAY;
  20. }
  21. final int SECONDS_PER_HOUR = 3600;
  22. int hours = 0;
  23. if (seconds >= SECONDS_PER_HOUR)
  24. {
  25. hours = seconds/SECONDS_PER_HOUR;
  26. seconds = seconds - hours * SECONDS_PER_HOUR;
  27. }
  28. final int SECONDS_PER_MINUTE = 60;
  29. int minutes = 0;
  30. if (seconds >= SECONDS_PER_MINUTE)
  31. {
  32. minutes = seconds/SECONDS_PER_MINUTE;
  33. seconds = seconds - minutes * SECONDS_PER_MINUTE;
  34. }
  35. if (days > 0)
  36. outputMessage += String.format(" %,d days\n", days);
  37. if (hours > 0)
  38. outputMessage += String.format(" %d hours\n", hours);
  39. if (minutes > 0)
  40. outputMessage += String.format(" %d minutes\n", minutes);
  41. if (seconds > 0)
  42. outputMessage += String.format(" %d seconds", seconds);
  43. }
  44. JOptionPane.showMessageDialog(null, outputMessage);
  45. System.exit(0);
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement