0

If I can do what I asked that works perfectly!

    <<LocalTime is provided as locTime>>
    Date flyDate = a.getDate();
    Date landingDate = b.getDate();

    flyDate.add(locTime);

Or I can learn what is the best practice.

I have bunch of Flight objects and currently they each hold a Date to determine flight's flying date/time and one for LocalTime for duration that says how many hours:minutes will the flight take.

I want to check if a flight is compatible with another one in terms of timing. Flight a's landing time should be before Flight b's flying time.

Thank you!

  • 2
    Don’t mix `Date` and the `java.time` API. Further, `LocalTime` is not a duration. [`Duration`](https://docs.oracle.com/javase/8/docs/api/?java/time/Duration.html) is a duration. – Holger Apr 23 '21 at 16:23
  • Oh! I probably did if not I probably will. What should I pay attention to? – Kayra Uckilinc Apr 23 '21 at 16:24
  • 3
    Decide, how you want to maintain the start and landing time, e.g. `LocalDateTime` or `ZonedDateTime`. Then, consider, e.g. `LocalDateTime start = LocalDateTime.now(); Duration flightTime = Duration.ofHours(1).plusMinutes(30); LocalDateTime end = start.plus(flightTime);`. Using `ZonedDateTime` works similar. – Holger Apr 23 '21 at 16:30

1 Answers1

1

You may be after something like the following:

    // Flying time for flight a
    ZonedDateTime aDepartureTime = ZonedDateTime.of(
            2021, 5, 1, 8, 0, 0, 0, ZoneId.of("Canada/Saskatchewan"));
    Duration aFlyingTime = Duration.ofHours(7).plusMinutes(45);
    ZonedDateTime aArrivalTime = aDepartureTime.plus(aFlyingTime)
            .withZoneSameInstant(ZoneId.of("America/Buenos_Aires"));
    
    // Flying time for flight b
    ZonedDateTime bDepartureTime = ZonedDateTime.of(
            2021, 5, 1, 18, 30, 0, 0, ZoneId.of("America/Buenos_Aires"));
    
    if (aArrivalTime.isBefore(bDepartureTime)) {
        System.out.format(
                "Flight b can be reached since arrival time %s is before departure time %s%n",
                aArrivalTime, bDepartureTime);
    } else {
        System.out.format(
                "Flight b cannot be reached since arrival time %s is not before departure time %s%n",
                aArrivalTime, bDepartureTime);
    }

Output is:

Flight b cannot be reached since arrival time 2021-05-01T18:45-03:00[America/Buenos_Aires] is not before departure time 2021-05-01T18:30-03:00[America/Buenos_Aires]

Use a ZonedDateTime for a date and time in a time zone. Use a Duration for — well, the class name says it.

Edit:

I am having trouble while adding all the Durations of a connected flight, it adds up to 00:00 at the end. I use

    Duration duration = Duration.ofHours(0);
    duration.plus(flight.getDuration());

What might be off?

A common oversight. For the second statement you need

    duration = duration.plus(flight.getDuration());

A Duration, like virtually all java.time classes, is immutable. So the plus method doesn’t add the other duration to the duration itself, but instead creates and returns a new Duration object holding the sum of the two durations.

Also how can I get hh:MM version of Duration?

See the link at the bottom.

A LocalTime is for a time of day. Don’t try to use it for a duration. A LocalDateTime is a date and time of day without time zone or offset from UTC. Since you don’t know which time zone it is in, you cannot use it for calculations that involve conversion to another time zone. I know that LocalDateTime is often used, but also very often in situations where it isn’t really the right class for the job. It hasn’t got very many good uses.

Links

How to format a duration in java? (e.g format H:MM:SS)

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I am having trouble while adding all the Durations of a connected flight, it adds up to 00:00 at the end. I use ``Duration duration = Duration.ofHours(0); duration.plus(flight.getDuration());`` What might be off? Also how can I get hh:MM version of Duration? – Kayra Uckilinc Apr 23 '21 at 18:42
  • Please see my edits (if you haven’t already) – Ole V.V. Apr 23 '21 at 18:51
  • My savior! Yes I felt so dumb when I thought it is a in-place method and didn't even think about it :( Formatting is working perfectly fine as well! – Kayra Uckilinc Apr 23 '21 at 18:52