Java LocalTime reportTimeDiff(LocalTime start, LocalTime end)

Here you can find the source of reportTimeDiff(LocalTime start, LocalTime end)

Description

This method will print (to System.out) the difference in time between a starting and an ending time

License

Open Source License

Parameter

Parameter Description
start staring LocalTime
end ending LocalTime

Declaration

public static void reportTimeDiff(LocalTime start, LocalTime end) 

Method Source Code

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

import java.time.LocalTime;

import static java.lang.Math.pow;

public class Main {
    /**//from  w  ww. j  a  va 2s  .  c  om
     * This method will print (to System.out) the difference
     * in time between a starting and an ending time
     *
     * @param start staring LocalTime
     * @param end   ending LocalTime
     */
    public static void reportTimeDiff(LocalTime start, LocalTime end) {

        end = end.minusHours(start.getHour());
        end = end.minusMinutes(start.getMinute());
        end = end.minusSeconds(start.getSecond());
        end = end.minusNanos(start.getNano());
        System.out.print("Computed in ");
        if (end.getHour() != 0) {
            System.out.print(end.getHour() + " Hours, ");
        }
        if (end.getMinute() != 0) {
            System.out.print(end.getMinute() + " Minutes, ");
        }
        if (end.getSecond() != 0 || end.getNano() != 0) {
            int nanos = (int) (end.getNano() / pow(10, 4));
            System.out.print(end.getSecond() + "." + nanos + " Seconds");
        } else {
            System.out.print("less than 0.001 second");
        }
    }
}

Related

  1. getTimeDiff(LocalTime start, LocalTime end)
  2. isBetween(LocalTime lt, LocalTime startTime, LocalTime endTime)
  3. isLocalTimeInRange(LocalTime value, LocalTime optionalMinimum, LocalTime optionalMaximum, boolean inclusiveOfEndpoints)
  4. parseLocalTime(String string)
  5. repDiff(LocalTime start, LocalTime end, byte type)
  6. toLocalTime(Date date)