How to format a duration in java? (e.g format H:MM:SS)

前端 未结 19 1486
情话喂你
情话喂你 2020-11-22 03:32

I\'d like to format a duration in seconds using a pattern like H:MM:SS. The current utilities in java are designed to format a time but not a duration.

相关标签:
19条回答
  • 2020-11-22 04:19

    This answer only uses Duration methods and works with Java 8 :

    public static String format(Duration d) {
        long days = d.toDays();
        d = d.minusDays(days);
        long hours = d.toHours();
        d = d.minusHours(hours);
        long minutes = d.toMinutes();
        d = d.minusMinutes(minutes);
        long seconds = d.getSeconds() ;
        return 
                (days ==  0?"":days+" jours,")+ 
                (hours == 0?"":hours+" heures,")+ 
                (minutes ==  0?"":minutes+" minutes,")+ 
                (seconds == 0?"":seconds+" secondes,");
    }
    
    0 讨论(0)
  • 2020-11-22 04:19

    I'm not sure that is you want, but check this Android helper class

    import android.text.format.DateUtils
    

    For example: DateUtils.formatElapsedTime()

    android date duration elapsedtime

    0 讨论(0)
  • 2020-11-22 04:21

    If you don't want to drag in libraries, it's simple enough to do yourself using a Formatter, or related shortcut eg. given integer number of seconds s:

      String.format("%d:%02d:%02d", s / 3600, (s % 3600) / 60, (s % 60));
    
    0 讨论(0)
  • 2020-11-22 04:21
    long duration = 4 * 60 * 60 * 1000;
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS", Locale.getDefault());
    log.info("Duration: " + sdf.format(new Date(duration - TimeZone.getDefault().getRawOffset())));
    
    0 讨论(0)
  • 2020-11-22 04:22

    This is a working option.

    public static String showDuration(LocalTime otherTime){          
        DateTimeFormatter df = DateTimeFormatter.ISO_LOCAL_TIME;
        LocalTime now = LocalTime.now();
        System.out.println("now: " + now);
        System.out.println("otherTime: " + otherTime);
        System.out.println("otherTime: " + otherTime.format(df));
    
        Duration span = Duration.between(otherTime, now);
        LocalTime fTime = LocalTime.ofNanoOfDay(span.toNanos());
        String output = fTime.format(df);
    
        System.out.println(output);
        return output;
    }
    

    Call the method with

    System.out.println(showDuration(LocalTime.of(9, 30, 0, 0)));
    

    Produces something like:

    otherTime: 09:30
    otherTime: 09:30:00
    11:31:27.463
    11:31:27.463
    
    0 讨论(0)
  • 2020-11-22 04:24

    In Scala, building up on YourBestBet's solution but simplified:

    def prettyDuration(seconds: Long): List[String] = seconds match {
      case t if t < 60      => List(s"${t} seconds")
      case t if t < 3600    => s"${t / 60} minutes" :: prettyDuration(t % 60)
      case t if t < 3600*24 => s"${t / 3600} hours" :: prettyDuration(t % 3600)
      case t                => s"${t / (3600*24)} days" :: prettyDuration(t % (3600*24))
    }
    
    val dur = prettyDuration(12345).mkString(", ") // => 3 hours, 25 minutes, 45 seconds
    
    0 讨论(0)
提交回复
热议问题