Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java create date for dayOfWeek and time without a specific date, i.e. valid for any Monday

Tags:

java

localdate

How can I create a generic date (i.e. not attached to any actual yyMMdd but similar to:

val START = LocalTime.of(startHour, startMinute)
val END = LocalTime.of(endHour, endMinute)

However, I additionally want to include the dayOfWeek.

My intention is to calculate the overlap of time intervals, i.e. for two given (start, end) timestamps I want to calculate overlap with a specified weekday and time (i.e. like opening hours).

edit

I am not sure if a custom class is good enough. My intention is if I have a list of events like:

id,start,end
1,2018-01-01 08:00, 2018-01-01 08:00 || opening hours 8-10, duration: 1
2,2018-01-01 10:00, 2018-01-01 12:00 || opening hours 8-10, duration: 0
3,2018-01-02 10:00, 2018-01-02 12:00 || opening hours 10-15, duration 2

to validate if time interval of start-end intersects with another time interval (i.e. opening hours), but this other time interval depends on the day of week.

After constructing the object (where I currently have my probably as I can't combine dayOfWeek and Time in a generic way)

I would use isBefore/isAfter a couple of times and then calculate the duration of overlap.

like image 434
Georg Heiler Avatar asked Oct 19 '25 10:10

Georg Heiler


2 Answers

Just use the DayOfWeek enum built into Java. It offers seven predefined objects, one for every day of the week, such as DayOfWeek.MONDAY.

I don’t follow exactly your business problem, but it sounds like you need to define your own class.

public class Shift {
    DayOfWeek dayOfWeek ;
    LocalTime startTime , stopTime ;
}

Add a method that translates that into real moments.

public ZonedDateTime startAtDate( LocalDate ld , ZoneId z ) {
    LocalDate LocalDate = ld.with( TemporalAdjustors.previousOrSame( this.dayOfWeek ) ) ;
    ZonedDateTime zdt = ZonedDateTime.of( localDate , this.startTime , z ) ;
    return zdt ;
}

You’ll likely want to add the ThreeTen-Extra library to your project. It offers classes such as Interval and LocalDateTime that you might find useful. These classes offer a bunch of comparison methods such as abuts, overlaps, etc.

Looks like that library lacks a LocalTimeRange, so might want to roll your own. Perhaps even donate such a class to that project.


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.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - 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
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
like image 50
Basil Bourque Avatar answered Oct 22 '25 00:10

Basil Bourque


One simple approach is to use an enum map for those "standard" intervals.

Assuming the method below to calculate the overlap:

static int calculateOverlap(LocalTime expectedStartTime, LocalTime expectedEndTime, 
        LocalTime actualStartTime, LocalTime actualEndTime) {
    //calculate overlap...
}

You can look up the value in this way (using Pair for simplicity, you can use a custom class with two LocalTime fields):

Map<DayOfWeek, Pair<LocalTime, LocalTime>> openingHours = 
          new EnumMap<>(DayOfWeek.class);

openingHours.put(DayOfWeek.MONDAY, 
          Pair.of(LocalTime.of(8, 0), LocalTime.of(16, 0)));
openingHours.put(DayOfWeek.TUESDAY, 
          Pair.of(LocalTime.of(9, 0), LocalTime.of(17, 0)));
//...entries for other weekdays

And then look it up using:

MyEvent mondayEvent = ...

Pair<LocalTime, LocalTime> mondayHours = officialHours.get(DayOfWeek.MONDAY);
int overlap = calculateOverlap(mondayHours.getLeft(), mondayHours.getRight(),
        mondayEvent.getStart(), mondayEvent.getEnd());
like image 34
ernest_k Avatar answered Oct 21 '25 22:10

ernest_k



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!