fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.ArrayList;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. private static final String[] intervalsKeys = {
  12. "years",
  13. "months",
  14. "weeks",
  15. "days",
  16. "hours",
  17. "minutes",
  18. "seconds"
  19. };
  20.  
  21. private static final int[] intervalsValues = {
  22. 60 * 60 * 24 * 365,
  23. 60 * 60 * 24 * 30,
  24. 60 * 60 * 24 * 7,
  25. 60 * 60 * 24,
  26. 60 * 60,
  27. 60,
  28. 1
  29. };
  30.  
  31. public static String getPeriodFormatted(int seconds) {
  32. return getPeriodFormatted(seconds, 2);
  33. }
  34.  
  35. public static String getPeriodFormatted(int seconds, int granularity){
  36. ArrayList<String> result = new ArrayList<>();
  37. for(int i = 0; i < intervalsKeys.length; i++){
  38. int value = seconds / intervalsValues[i];
  39. if(value > 0){
  40. seconds -= value * intervalsValues[i];
  41. result.add(intervalsKeys[i] + " " + value);
  42. }
  43. }
  44.  
  45. String f = "";
  46. for(int i = 0; i < granularity; i++){
  47. f += result.get(i);
  48. if(i - 1 < granularity)
  49. f += ", ";
  50. }
  51. return f;
  52. }
  53.  
  54. public static void main (String[] args) throws java.lang.Exception
  55. {
  56. // your code goes here
  57. System.out.println ("El" + getPeriodFormatted(2000000, 2));
  58. }
  59. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
Elweeks 3, days 2,