Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference of using TemporalAmount or TemporalUnit in Java 8?

I write some piece of code in Java 8 which use time arithmetic. I realize that I can implement in differentways. Lets look at simple code below. Of course it is the same result but I confused which way is mostly applied or most efficient to make arithmetic operations in Java 8 ?

LocalTime time = LocalTime.now();
// 1st way
LocalTime plusOp = time.plus(Duration.ofMinutes(10L));
// 2nd way
LocalTime plusOp2 = time.plus(10L, ChronoUnit.MINUTES);
System.out.println(plusOp);
System.out.println(plusOp2);
// 3. way simply
 time.plusMinutes(10L);

Thanks in advance.

like image 372
Ugur Artun Avatar asked Mar 10 '15 11:03

Ugur Artun


People also ask

What is TemporalAmount in Java?

public interface TemporalAmount. Framework-level interface defining an amount of time, such as "6 hours", "8 days" or "2 years and 3 months". This is the base interface type for amounts of time. An amount is distinct from a date or time-of-day in that it is not tied to any specific point on the time-line.

Can Java duration be negative?

A Duration represents a directed distance between two points on the time-line and can therefore be positive, zero or negative.

What is Java duration return?

A Duration represents a directed distance between two points on the time-line. A negative duration is expressed by the negative sign of the seconds part. A duration of -1 nanosecond is stored as -1 seconds plus 999,999,999 nanoseconds.

How do you add instant days?

Instant plus() method in Java An immutable copy of a instant where a time unit is added to it can be obtained using the plus() method in the Instant class in Java. This method requires two parameters i.e. time to be added to the instant and the unit in which it is to be added.


1 Answers

Duration can only handle fixed-length periods, such as "hours", "minutes", "seconds", "days" (where it assumes exactly 24 hours per day). You can't use "months" with Duration, because a month varies in length.

Period - the other common TemporalAmount implementation - represents years, months and days separately.

Personally I would recommend:

  • When you know the unit beforehand, use the appropriate plusXxx method, e.g. time.plusMinutes(10). That's about as easy to read as it gets.
  • When you're trying to represent "logical" calendar amounts, use Period
  • When you're trying to represent "fixed length" amounts, use Duration

Here's an example of where Period and Duration can differ:

import java.time.*;

public class Test {
    public static void main(String[] args) {
        ZoneId zone = ZoneId.of("Europe/London");
        // At 2015-03-29T01:00:00Z, Europe/London goes from UTC+0 to UTC+1
        LocalDate transitionDate = LocalDate.of(2015, 3, 29);
        ZonedDateTime start = ZonedDateTime.of(transitionDate, LocalTime.MIDNIGHT, zone);
        ZonedDateTime endWithDuration = start.plus(Duration.ofDays(1));
        ZonedDateTime endWithPeriod = start.plus(Period.ofDays(1));
        System.out.println(endWithDuration); // 2015-03-30T01:00+01:00[Europe/London]
        System.out.println(endWithPeriod);   // 2015-03-30T00:00+01:00[Europe/London]
    }
}

I wouldn't worry about the efficiency until you really need to - at which point you should have a benchmark so you can test different options.

like image 59
Jon Skeet Avatar answered Oct 11 '22 13:10

Jon Skeet