Java Dailly Tip: Get time period ( hour / half-hour / quarter hour ) of day

Java Dailly Tip: Get time period ( hour / half-hour / quarter hour ) of day

Problem

Is there any way to download every hour and another period of the day? I mean exactly the result in the form of a list.

[
"00:00", // start of the day e.q 14.03
"01:00",
"02:00",
....
"23:00",
"00:00  // end of the day e.q 14.03
]

and

[
"00:00", // start of the day e.q 14.03
"00:30",
"01:00"
"01:30"
....
"00:00  // end of the day e.q 14.03
]

and

[
"00:00", // start of the day e.q 14.03
"00:15",
"00:30"
"00:45"
"01:00"
....
"00:00  // end of the day e.q 14.03
]

14.03 means March 14 for the sake of the example.

Of course it is possible to add this manually, but it would not be a particularly elegant solution. Is it possible to do it without explicitly declaring constants as values of each hour? The best solution would be a function without using loops and if else constructions.

I have not yet been able to find an implementation of such a solution. I myself also spend another hour on this without success. I need it to implement a functionality that creates a Map or a list of pairs from a list of such e.q hours:

[
"00:00 - 01:00",
"01:00 - 02:00",
"02:00 - 03:00 "
//.......
"23:00 - 00:00"
]

Has anyone had occasion to implement such a problem and would be able to help?

Solution

first, use Duration. A Duration is most suitable in situations that measure machine-based time, such as code that uses an Instant object. A Duration object is measured in seconds or nanoseconds and does not use date-based constructs such as years, months, and days, though the class provides methods that convert to days, hours, and minutes. A Duration can have a negative value, if it is created with an end point that occurs before the start point.

The following code calculates, in nanoseconds, the duration between two instants:

Instant t1, t2;
...
long ns = Duration.between(t1, t2).toNanos();
The following code adds 10 seconds to an Instant:

Instant start;
...
Duration gap = Duration.ofSeconds(10);
Instant later = start.plus(gap);

A Duration is not connected to the timeline, in that it does not track time zones or daylight saving time. Adding a Duration equivalent to 1 day to a ZonedDateTime results in exactly 24 hours being added, regardless of daylight saving time or other time differences that might result.

Second, use ChronoUnit. The ChronoUnit enum, discussed in the The Temporal Package, defines the units used to measure time. The ChronoUnit.between method is useful when you want to measure an amount of time in a single unit of time only, such as days or seconds. The between method works with all temporal-based objects, but it returns the amount in a single unit only. The following code calculates the gap, in milliseconds, between two time-stamps:

import java.time.Instant;
import java.time.temporal.Temporal;
import java.time.temporal.ChronoUnit;

Instant previous, current, gap;
...
current = Instant.now();
if (previous != null) {
    gap = ChronoUnit.MILLIS.between(previous,current);
}
...

Share this Post