1

I have departure time - 02:00, arrival - 06:15, and on the way - "on the way 4h 15m". I want to calculate whether the travel time is counted correctly

HTML
    <div class="wl-offer_edge-from">02:00</div>
    <div class="wl-offer_duration">on the way 4h 15m</div></div>
    <div class="wl-offer_edge-back">06:15</div>

My code JAVA+Selenide

But for some reason the test falls on "LocalTime timeDuration"

public void checkFlightDurationTime() {
    SelenideElement card = $$(".wl").get(0);
    LocalTime timeDeparture = LocalTime.parse(card.find(".wl-offer_edge-from").text(), 
            DateTimeFormatter.ofPattern("HH:mm"));
    LocalTime timeArrival = LocalTime.parse(card.find(".wl-offer_edge-back").text(), 
            DateTimeFormatter.ofPattern("HH:mm"));
    LocalTime timeDuration = LocalTime.parse($(".wl-offer_duration").text()
            .replace("on the way ", "").replace("h ", ":").replace("m", ""));
    PeriodFormatter hoursMinutesFormatter =
            new PeriodFormatterBuilder().appendHours().appendSeparator(":")
                    .appendMinutes().toFormatter();
    Period timeFrom = hoursMinutesFormatter
            .parseMutablePeriod(String.valueOf(timeDeparture)).toPeriod();
    Period timeOfDurationOriginal = hoursMinutesFormatter
            .parseMutablePeriod(String.valueOf(timeDuration)).toPeriod();
    Period timeBack = hoursMinutesFormatter.parseMutablePeriod(String.valueOf(timeArrival)).toPeriod();
    Period timeOfDuration = Period.parse(timeBack.minus(timeFrom).toString(hoursMinutesFormatter));
    if(timeOfDurationOriginal.equals(timeOfDuration)){
        System.out.println("It's OK");
    }}
EYE
  • 13
  • 2
  • 4
    Use a `java.time.Duration` instead of a `java.time.Period` (which is basically handling differences in days, months and years instead of hours, minutes and further down the units). – deHaar Oct 12 '22 at 13:30

1 Answers1

2
  1. You do not need a custom DateTimeFormatter to parse the time 02:00 or 06:15 as they are already in the default pattern used by LocalTime. Note that the modern date-time API is based on ISO 8601.
  2. You can use java.time.Duration for your requirement. It was introduced with Java-8. If you are using Java-9, you can leverage some convenience methods that were introduced with this release.

Demo:

import java.time.Duration;
import java.time.LocalTime;

public class Main {
    public static void main(String[] args) {
        LocalTime departure = LocalTime.parse("02:00");
        LocalTime arrival = LocalTime.parse("06:15");
        Duration duration = Duration.between(departure, arrival);

        // Custom format
        // ################################## Java-8 ##################################
        String formatted = String.format("on the way %dh %dm", duration.toHours() % 24, duration.toMinutes() % 60);
        // ##############################################################################

        // ################################## Java-9 ##################################
        formatted = String.format("on the way %dh %dm", duration.toHoursPart(), duration.toMinutesPart());
        // ##############################################################################

        // Test
        if ("on the way 4h 15m".equals(formatted)) {
            System.out.println("Equal");
            // Do something
        } else {
            System.out.println("Not equal");
            // Do something else
        }
    }
}

Output:

Equal

Learn more about the the modern date-time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    Thank you so much, I haven't studied such constructions yet, but there will be something to work on – EYE Oct 17 '22 at 13:40