Interval time subtraction in Java

I tried to find the time difference (HH: mm: ss.SSS) in Java, where each time can be more than 24 hours. SimpleDateFormatdoes not support time exceeding 24 hours.

For instance,

Time A = 36: 00: 00.00
Time B = 23: 00: 00.00

I would like to receive a response from 13: 00: 00.00. (13 hours).

Does anyone know if there are any Java libraries that can do subtraction. I would also like to know if adding time in the Java library is possible.

+5
source share
5 answers

You do not need a third-party library

, -, Date DateTime Timestamp, , -, interval, JDK >= 1.5 java.util.concurrent.TimeUnit - .

, String, , , .

java.util.concurrent.TimeUnit - , 1.5. , java.util.concurrent, , , .

import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main
{
    private static long parseInterval(final String s)
    {
        final Pattern p = Pattern.compile("^(\\d{2}):(\\d{2}):(\\d{2})\\.(\\d{3})$");
        final Matcher m = p.matcher(s);
        if (m.matches())
        {
            final long hr = Long.parseLong(m.group(1)) * TimeUnit.HOURS.toMillis(1);
            final long min = Long.parseLong(m.group(2)) * TimeUnit.MINUTES.toMillis(1);
            final long sec = Long.parseLong(m.group(3)) * TimeUnit.SECONDS.toMillis(1);
            final long ms = Long.parseLong(m.group(4));
            return hr + min + sec + ms;
        }
        else
        {
            throw new IllegalArgumentException(s + " is not a supported interval format!");
        }
    }

    private static String formatInterval(final long l)
    {
        final long hr = TimeUnit.MILLISECONDS.toHours(l);
        final long min = TimeUnit.MILLISECONDS.toMinutes(l - TimeUnit.HOURS.toMillis(hr));
        final long sec = TimeUnit.MILLISECONDS.toSeconds(l - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min));
        final long ms = TimeUnit.MILLISECONDS.toMillis(l - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min) - TimeUnit.SECONDS.toMillis(sec));
        return String.format("%02d:%02d:%02d.%03d", hr, min, sec, ms);
    }

    public static void main(final String[] args)
    {
        final String s1 = "36:00:00.000";
        final String s2 = "23:00:00.000";

        final long i1 = parseInterval(s1);
        final long i2 = parseInterval(s2);

        System.out.println(formatInterval(i1 - i2));
    }
}

13:00:00.000

, , milliseconds, 3 .

+11

JodaTime. , , java.util java.util.concurrent apis:

public static int getHoursBetween(final String date1, final String date2){
    final DateTimeFormatter fmt =
        DateTimeFormat
            .forPattern("HH:mm:ss.SS")
            .withChronology(
                LenientChronology.getInstance(
                    GregorianChronology.getInstance()));
    return Hours.hoursBetween(
        fmt.parseDateTime(date1),
        fmt.parseDateTime(date2)
    ).getHours();
}

(LenientChronology , , 38: 00: 00.00)

+3

Joda-Time, Java, .

+1

, , .

0

Here's an example of how to get the difference between two dates using Joda-Time (as mentioned by Jim Harrison earlier. This is truly one of the best time libraries available.

  public static void main(String[] args) {
    DateTime timeA = new DateTime(2011, 6, 13, 12, 0, 0, 0);
    DateTime timeB = new DateTime(2011, 6, 12, 23, 0, 0, 0);

    Period period = new Period(timeB, timeA);
    System.out.println(period.getHours());
  }
0
source

All Articles