Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Store start and end time point

Tags:

java

date

Is there a convenient way to store time points (like a start and end time point)?

I need to store responsibilities where every timespan an userId is assigned. I thought of:

HashMap<Date[], Integer> responsibilities;

Where the Date-array stores the start- and end-date, but is there a more convenient way?

like image 398
F.M.F. Avatar asked Nov 25 '25 20:11

F.M.F.


2 Answers

create a class

class TimePane {

    private long start;
    private long end;

    .... 
    //setters, getters, equals, hashcode

}

and use the map

Map<TimePane, Integer> p = new HashMap<>();
like image 66
ΦXocę 웃 Пepeúpa ツ Avatar answered Nov 27 '25 09:11

ΦXocę 웃 Пepeúpa ツ


tl;dr

org.threeten.extra.Interval.of( start , end )

Interval

The Interval class found in the ThreeTen-Extra project contains a pair of java.time.Instant objects.

The Interval class has several methods you may find helpful such as contains, overlaps, abuts, intersection, union, and span.

The Interval class is defined as Half-Open, where the beginning is inclusive while the ending is exclusive. So a noon lunch break interval might start at 12:00:00 (noon) and run up to, but not include, 13:00:00 (1 PM). This approach is wise and practical, and is common in date-time handling.

The Instant class is built into Java 8 and later. It stores a moment on the timeline in UTC with a resolution of nanoseconds.

Instant start = Instant.now() ;
Instant end = start.plusHours( 2 );
Interval interval = Interval.of( start , end );

Extract either instant by calling getStart and getEnd.

To see the Instant object through the lens of a particular region’s wall-clock time, apply a time zone. The resulting ZonedDateTime is the very same simultaneous moment on the timeline but with a wall-clock time some number of hours and minutes ahead or behind UTC.

ZoneId z = ZoneId.of( "America/Montreal" ) ;  // A few hours behind UTC.
ZonedDateTime zdt = instant.atZone( z ) ;  

Map

Create your Map using the Interval as the key.

Map< Interval , User > map = new HashMap<>() ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

like image 25
Basil Bourque Avatar answered Nov 27 '25 09:11

Basil Bourque



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!