fork download
  1. import java.time.LocalDateTime;
  2. import java.time.format.DateTimeFormatter;
  3. import java.time.temporal.ChronoUnit;
  4. import java.util.LinkedHashMap;
  5. import java.util.Locale;
  6. import java.util.Map;
  7.  
  8. public class Main {
  9. public static void main(String[] args) {
  10. // Test
  11. System.out.println(getDiffDateMap("2012-9-01 20:9:01", "2012-10-01 20:10:01"));
  12. }
  13.  
  14. public static Map<Integer, String> getDiffDateMap(String dateA, String dateB) {
  15. Map<Integer, String> out = new LinkedHashMap<Integer, String>();
  16. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u-M-d H:m:s", Locale.ENGLISH);
  17. LocalDateTime ldtA = LocalDateTime.parse(dateA, dtf);
  18. LocalDateTime ldtB = LocalDateTime.parse(dateB, dtf);
  19.  
  20. out.put(7, String.valueOf(ChronoUnit.MILLIS.between(ldtA, ldtB)));
  21. out.put(6, String.valueOf(ChronoUnit.SECONDS.between(ldtA, ldtB)));
  22. out.put(5, String.valueOf(ChronoUnit.MINUTES.between(ldtA, ldtB)));
  23. out.put(4, String.valueOf(ChronoUnit.HOURS.between(ldtA, ldtB)));
  24. out.put(3, String.valueOf(ChronoUnit.DAYS.between(ldtA, ldtB)));
  25. out.put(2, String.valueOf(ChronoUnit.WEEKS.between(ldtA, ldtB)));
  26. out.put(1, String.valueOf(ChronoUnit.MONTHS.between(ldtA, ldtB)));
  27. out.put(0, String.valueOf(ChronoUnit.YEARS.between(ldtA, ldtB)));
  28.  
  29. return out;
  30. }
  31. }
Success #stdin #stdout 0.09s 51384KB
stdin
Standard input is empty
stdout
{7=2592060000, 6=2592060, 5=43201, 4=720, 3=30, 2=4, 1=1, 0=0}