简体   繁体   中英

How can I add LocalTime into a Date object?

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!

As others have said, it's a problematic question in that a LocalTime and a Date really represent quite different and almost unrelated concepts. A LocalTime is a time of day without time zone, such as 19:45 (or 7:45 PM). A Date is a point on the time line; if it happens to coincide with 19:45 on some date in some time zone, it will not in other time zones.

I believe that the conventional way of misusing (indeed) a Date for an hour of day is setting it to that time of day on January 1, 1970 in the default time zone of the JVM. This practice carries all of the liabilities already mentioned. In particular the JVM default time zone setting can be changed at any time from another part of your program or any other program running in the same JVM. This means that a completely unrelated program may suddenly cause your Date to indicate a different time of day than the one you had initialized it to.

There's nothing better we can do, so here goes

LocalTime time = LocalTime.of(11, 0);

    Instant timeOnEpochDayInDefaultTimeZone = LocalDate.EPOCH
            .atTime(time)
            .atZone(ZoneId.systemDefault())
            .toInstant();
    Date oldfashionedDateObject = Date.from(timeOnEpochDayInDefaultTimeZone);

    System.out.println(oldfashionedDateObject);

And finally output is

Tue May 03 11:00:00 UTC 1999

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 Duration s 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? (eg format H:MM:SS)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM