Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. public static HashMap<String, Integer> getDifferences(Date begin, Date end) {
  2. HashMap<String, Integer> output = new HashMap<>();
  3. long diff = end.getTime() - begin.getTime();
  4.  
  5. output.put("s", (int) diff / 1000 % 60);
  6. output.put("m", (int) diff / (60 * 1000) % 60);
  7. output.put("h", (int) diff / (60 * 60 * 1000) % 24);
  8. output.put("d", (int) diff / (24 * 60 * 60 * 1000));
  9. return output;
  10. }
  11.  
  12. public static String clockFormat(HashMap<String, Integer> input) {
  13. String output = "";
  14. ArrayList<String> units = new ArrayList<>();
  15. for (String key : input.keySet()) {
  16. if (input.get(key) > 10) {
  17. units.add("0" + input.get(key));
  18. } else {
  19. units.add(input.get(key) + "");
  20. }
  21. }
  22. for (String unit : units) {
  23. if (!output.equals("")) {
  24. output = output + ":" + unit;
  25. } else {
  26. output = unit;
  27. }
  28. }
  29. return output;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement