@Test public void testMoreThanADay() { long millis = TimeUnit.DAYS.toMillis(365); Assert.assertEquals("365 days", PrettyFormatter.formatMillisToYearsDaysHours(millis)); millis = TimeUnit.DAYS.toMillis(366); Assert.assertEquals("1 year, 1 day", PrettyFormatter.formatMillisToYearsDaysHours(millis)); millis = TimeUnit.DAYS.toMillis(366) + TimeUnit.HOURS.toMillis(5); Assert.assertEquals("1 year, 1 day, 5 hours", PrettyFormatter.formatMillisToYearsDaysHours(millis)); }
public static String formatMillisToYearsDaysHours(long millis) { long days = TimeUnit.MILLISECONDS.toDays(millis); millis -= TimeUnit.DAYS.toMillis(days); final long hours = TimeUnit.MILLISECONDS.toHours(millis); millis -= TimeUnit.HOURS.toMillis(hours); long years = 0; while(days>365) { days -= 365; years++; } List<String> parts = new ArrayList<>(); parts.add((years>0)?(years>1)?years+" years":years+" year":null); parts.add((days>0)?(days>1)?days+" days":days+" day":null); parts.add((hours>0)?(hours>1)?hours+" hours":hours+" hour":null); final String formatted = parts.stream().filter(s -> s!=null).collect(Collectors.joining(", ")); if(formatted.length() == 0) { if(millis > 0) { final long minutes = TimeUnit.MILLISECONDS.toMinutes(millis); millis -= TimeUnit.MINUTES.toMillis(minutes); final long seconds = TimeUnit.MILLISECONDS.toSeconds(millis); final String minutesPart = (minutes>0)?(minutes>1)?minutes+" minutes":minutes+" minute":""; final String secondsPart = (seconds>0)?(seconds>1)?" and "+seconds+" seconds":" and "+seconds+" second":""; return minutesPart + secondsPart; } return "-"; } return formatted; }
PrettyFormatter { public static String formatMillisToYearsDaysHours(long millis) { long days = TimeUnit.MILLISECONDS.toDays(millis); millis -= TimeUnit.DAYS.toMillis(days); final long hours = TimeUnit.MILLISECONDS.toHours(millis); millis -= TimeUnit.HOURS.toMillis(hours); long years = 0; while(days>365) { days -= 365; years++; } List<String> parts = new ArrayList<>(); parts.add((years>0)?(years>1)?years+" years":years+" year":null); parts.add((days>0)?(days>1)?days+" days":days+" day":null); parts.add((hours>0)?(hours>1)?hours+" hours":hours+" hour":null); final String formatted = parts.stream().filter(s -> s!=null).collect(Collectors.joining(", ")); if(formatted.length() == 0) { if(millis > 0) { final long minutes = TimeUnit.MILLISECONDS.toMinutes(millis); millis -= TimeUnit.MINUTES.toMillis(minutes); final long seconds = TimeUnit.MILLISECONDS.toSeconds(millis); final String minutesPart = (minutes>0)?(minutes>1)?minutes+" minutes":minutes+" minute":""; final String secondsPart = (seconds>0)?(seconds>1)?" and "+seconds+" seconds":" and "+seconds+" second":""; return minutesPart + secondsPart; } return "-"; } return formatted; } }
PrettyFormatter { public static String formatMillisToYearsDaysHours(long millis) { long days = TimeUnit.MILLISECONDS.toDays(millis); millis -= TimeUnit.DAYS.toMillis(days); final long hours = TimeUnit.MILLISECONDS.toHours(millis); millis -= TimeUnit.HOURS.toMillis(hours); long years = 0; while(days>365) { days -= 365; years++; } List<String> parts = new ArrayList<>(); parts.add((years>0)?(years>1)?years+" years":years+" year":null); parts.add((days>0)?(days>1)?days+" days":days+" day":null); parts.add((hours>0)?(hours>1)?hours+" hours":hours+" hour":null); final String formatted = parts.stream().filter(s -> s!=null).collect(Collectors.joining(", ")); if(formatted.length() == 0) { if(millis > 0) { final long minutes = TimeUnit.MILLISECONDS.toMinutes(millis); millis -= TimeUnit.MINUTES.toMillis(minutes); final long seconds = TimeUnit.MILLISECONDS.toSeconds(millis); final String minutesPart = (minutes>0)?(minutes>1)?minutes+" minutes":minutes+" minute":""; final String secondsPart = (seconds>0)?(seconds>1)?" and "+seconds+" seconds":" and "+seconds+" second":""; return minutesPart + secondsPart; } return "-"; } return formatted; }  }
PrettyFormatter { public static String formatMillisToYearsDaysHours(long millis) { long days = TimeUnit.MILLISECONDS.toDays(millis); millis -= TimeUnit.DAYS.toMillis(days); final long hours = TimeUnit.MILLISECONDS.toHours(millis); millis -= TimeUnit.HOURS.toMillis(hours); long years = 0; while(days>365) { days -= 365; years++; } List<String> parts = new ArrayList<>(); parts.add((years>0)?(years>1)?years+" years":years+" year":null); parts.add((days>0)?(days>1)?days+" days":days+" day":null); parts.add((hours>0)?(hours>1)?hours+" hours":hours+" hour":null); final String formatted = parts.stream().filter(s -> s!=null).collect(Collectors.joining(", ")); if(formatted.length() == 0) { if(millis > 0) { final long minutes = TimeUnit.MILLISECONDS.toMinutes(millis); millis -= TimeUnit.MINUTES.toMillis(minutes); final long seconds = TimeUnit.MILLISECONDS.toSeconds(millis); final String minutesPart = (minutes>0)?(minutes>1)?minutes+" minutes":minutes+" minute":""; final String secondsPart = (seconds>0)?(seconds>1)?" and "+seconds+" seconds":" and "+seconds+" second":""; return minutesPart + secondsPart; } return "-"; } return formatted; }  static String formatMillisToYearsDaysHours(long millis); }
PrettyFormatter { public static String formatMillisToYearsDaysHours(long millis) { long days = TimeUnit.MILLISECONDS.toDays(millis); millis -= TimeUnit.DAYS.toMillis(days); final long hours = TimeUnit.MILLISECONDS.toHours(millis); millis -= TimeUnit.HOURS.toMillis(hours); long years = 0; while(days>365) { days -= 365; years++; } List<String> parts = new ArrayList<>(); parts.add((years>0)?(years>1)?years+" years":years+" year":null); parts.add((days>0)?(days>1)?days+" days":days+" day":null); parts.add((hours>0)?(hours>1)?hours+" hours":hours+" hour":null); final String formatted = parts.stream().filter(s -> s!=null).collect(Collectors.joining(", ")); if(formatted.length() == 0) { if(millis > 0) { final long minutes = TimeUnit.MILLISECONDS.toMinutes(millis); millis -= TimeUnit.MINUTES.toMillis(minutes); final long seconds = TimeUnit.MILLISECONDS.toSeconds(millis); final String minutesPart = (minutes>0)?(minutes>1)?minutes+" minutes":minutes+" minute":""; final String secondsPart = (seconds>0)?(seconds>1)?" and "+seconds+" seconds":" and "+seconds+" second":""; return minutesPart + secondsPart; } return "-"; } return formatted; }  static String formatMillisToYearsDaysHours(long millis);  }