Java Duration to toDuration(Long unixTime)

Here you can find the source of toDuration(Long unixTime)

Description

to Duration

License

Open Source License

Declaration

public static String toDuration(Long unixTime) 

Method Source Code


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

import java.time.Duration;
import java.time.Instant;

import java.time.LocalDateTime;

import java.time.ZoneId;

import java.time.temporal.ChronoUnit;

public class Main {
    public static String toDuration(Long unixTime) {
        LocalDateTime start = LocalDateTime.ofInstant(Instant.ofEpochSecond(unixTime), ZoneId.systemDefault());
        LocalDateTime end = LocalDateTime.ofInstant(Instant.ofEpochSecond(0), ZoneId.systemDefault());

        Duration duration = Duration.between(end, start);
        long hours = duration.toHours();
        long minutes = duration.minusHours(hours).toMinutes();

        return String.format("%02d:%02d", hours, minutes);
    }//from w  w  w  .j  a  v  a  2 s  .  c  o m

    public static String toDuration(LocalDateTime start, LocalDateTime stop) {
        long hours = ChronoUnit.HOURS.between(start, stop);
        long minutes = ChronoUnit.MINUTES.between(start, stop) % 60;

        return String.format("%02d:%02d", hours, minutes);
    }
}

Related

  1. durationToFloat(Duration duration)
  2. durationToString(Duration d)
  3. toDuration(String durationString)
  4. toMicro(Duration duration)
  5. toSeconds(final Duration time)
  6. toTicks(Duration duration)