Java Duration Calculate serializeDuration(Duration duration)

Here you can find the source of serializeDuration(Duration duration)

Description

serialize Duration

License

Open Source License

Declaration

public static String serializeDuration(Duration duration) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.time.Duration;

public class Main {
    public static String serializeDuration(Duration duration) {
        if (duration == null || duration.isNegative() || duration.isZero()) {
            return "";
        }// w  w  w  . j a v  a  2  s  .  c  o m
        Duration remainingTime = duration;
        StringBuffer sb = new StringBuffer("P");
        long days = remainingTime.toDays();
        if (days > 0) {
            sb.append(days).append("D");
            remainingTime = duration.minusDays(days);
        }
        if (!remainingTime.isZero()) {
            sb.append("T");
            long hours = remainingTime.toHours();
            if (hours > 0) {
                sb.append(hours).append("H");
                remainingTime = duration.minusHours(hours);
            }

            long minutes = remainingTime.toMinutes();
            if (minutes > 0) {
                sb.append(minutes).append("M");
                remainingTime = duration.minusMinutes(minutes);
            }

            long seconds = remainingTime.getSeconds();
            if (seconds > 0) {
                sb.append(seconds).append("S");
            }
        }

        return sb.toString();
    }
}

Related

  1. min(Duration a, Duration b)
  2. minutesToDuration(long minutes)
  3. multiply(Duration duration, double factor)
  4. runAfter(Duration duration, Runnable toRun)
  5. seconds(Duration duration)
  6. sleep(Duration duration)
  7. sleep(final Duration duration)
  8. within( CompletableFuture future, Duration duration)