How to convert Milliseconds to “X mins, x seconds” in Java?

后端 未结 27 1807
夕颜
夕颜 2020-11-22 03:59

I want to record the time using System.currentTimeMillis() when a user begins something in my program. When he finishes, I will subtract the current Syste

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

    For small times, less than an hour, I prefer:

    long millis = ...
    
    System.out.printf("%1$TM:%1$TS", millis);
    // or
    String str = String.format("%1$TM:%1$TS", millis);
    

    for longer intervalls:

    private static final long HOUR = TimeUnit.HOURS.toMillis(1);
    ...
    if (millis < HOUR) {
        System.out.printf("%1$TM:%1$TS%n", millis);
    } else {
        System.out.printf("%d:%2$TM:%2$TS%n", millis / HOUR, millis % HOUR);
    }
    
    0 讨论(0)
  • 2020-11-22 04:22

    Use java.util.concurrent.TimeUnit, and use this simple method:

    private static long timeDiff(Date date, Date date2, TimeUnit unit) {
        long milliDiff=date2.getTime()-date.getTime();
        long unitDiff = unit.convert(milliDiff, TimeUnit.MILLISECONDS);
        return unitDiff; 
    }
    

    For example:

    SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd HH:mm:ss");  
    Date firstDate = sdf.parse("06/24/2017 04:30:00");
    Date secondDate = sdf.parse("07/24/2017 05:00:15");
    Date thirdDate = sdf.parse("06/24/2017 06:00:15");
    
    System.out.println("days difference: "+timeDiff(firstDate,secondDate,TimeUnit.DAYS));
    System.out.println("hours difference: "+timeDiff(firstDate,thirdDate,TimeUnit.HOURS));
    System.out.println("minutes difference: "+timeDiff(firstDate,thirdDate,TimeUnit.MINUTES));
    System.out.println("seconds difference: "+timeDiff(firstDate,thirdDate,TimeUnit.SECONDS));
    
    0 讨论(0)
  • 2020-11-22 04:23
        long startTime = System.currentTimeMillis();
        // do your work...
        long endTime=System.currentTimeMillis();
        long diff=endTime-startTime;       
        long hours=TimeUnit.MILLISECONDS.toHours(diff);
        diff=diff-(hours*60*60*1000);
        long min=TimeUnit.MILLISECONDS.toMinutes(diff);
        diff=diff-(min*60*1000);
        long seconds=TimeUnit.MILLISECONDS.toSeconds(diff);
        //hour, min and seconds variables contains the time elapsed on your work
    
    0 讨论(0)
  • 2020-11-22 04:26
    long time = 1536259;
    
    return (new SimpleDateFormat("mm:ss:SSS")).format(new Date(time));
    

    Prints:

    25:36:259

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

    Here is an answer based on Brent Nash answer, Hope that helps !

    public static String getDurationBreakdown(long millis)
    {
        String[] units = {" Days ", " Hours ", " Minutes ", " Seconds "};
        Long[] values = new Long[units.length];
        if(millis < 0)
        {
            throw new IllegalArgumentException("Duration must be greater than zero!");
        }
    
        values[0] = TimeUnit.MILLISECONDS.toDays(millis);
        millis -= TimeUnit.DAYS.toMillis(values[0]);
        values[1] = TimeUnit.MILLISECONDS.toHours(millis);
        millis -= TimeUnit.HOURS.toMillis(values[1]);
        values[2] = TimeUnit.MILLISECONDS.toMinutes(millis);
        millis -= TimeUnit.MINUTES.toMillis(values[2]);
        values[3] = TimeUnit.MILLISECONDS.toSeconds(millis);
    
        StringBuilder sb = new StringBuilder(64);
        boolean startPrinting = false;
        for(int i = 0; i < units.length; i++){
            if( !startPrinting && values[i] != 0)
                startPrinting = true;
            if(startPrinting){
                sb.append(values[i]);
                sb.append(units[i]);
            }
        }
    
        return(sb.toString());
    }
    
    0 讨论(0)
  • 2020-11-22 04:28

    Using the java.time package in Java 8:

    Instant start = Instant.now();
    Thread.sleep(63553);
    Instant end = Instant.now();
    System.out.println(Duration.between(start, end));
    

    Output is in ISO 8601 Duration format: PT1M3.553S (1 minute and 3.553 seconds).

    0 讨论(0)
提交回复
热议问题