-2

The expiry should be after 8 working hours (8 am to 5 pm UTC monday to Friday) for example the created date at 7 pm UTC time, the expiring time should be counted from next morning 8 am to 5 pm UTC time and expire. So it should expire at 4 pm UTC .

Another example, if broadcast happened at 4 pm SA time then expiry should be counted as 1 hour from today and 7 hours from next day (8 am to 5 pm time) so it should expire at 3 pm SA time . I have tried this solution but not working can anyone please help me out here

int workHourStartUtc = 6;
int workHourEndUtc = 15;
int expiryWindow = 8;
int currentHourUtc = LocalDateTime.now(ZoneOffset.UTC).getHour();
int hourCompletedToday = currentHourUtc - workHourStartUtc;
int offHours = LocalDate.now().getDayOfWeek() == 1 ? 72 : 24;
LocalDateTime yesterdayWorkEndUtc = LocalDateTime.now(ZoneOffset.UTC).toLocalDate().atTime(workHourEndUtc, 0).minusHours(offHours);
LocalDateTime expireDateBefore = hourCompletedToday >= expiryWindow ? LocalDateTime.now(ZoneOffset.UTC).minusHours(expiryWindow): yesterdayWorkEndUtc.minusHours(expiryWindow - hourCompletedToday);
  • How do you plan to handle lunchtime? Because when you say 8 working hours: 8am to 5pm this would be 9 hours of worktime? – buec95 Jan 05 '23 at 09:51
  • In this case i don't need to calculate the lunchtime basically i am running a scheduler which only expires the value of date between interval of 8 hours in weekdays if you can suggest any code snippet for the same will be helpful Thanks in advance @buec95 – Saurabh Kohade Jan 05 '23 at 10:37

1 Answers1

0

For sure there are much more elegant solutions but this could be one. I also added a break during lunch(12 to 13 o'clock) so for example if the start is at 12:30 the end will be at 13:30 (Lunchtime = no usage).

public static void main(String[] args) {

final int START_OF_DAY = 8;
final int END_OF_DAY = 17;
final int WORKING_HOURS = 8;
final int LUNCH = 1;
LocalDateTime endDateTime = LocalDateTime.now(ZoneOffset.UTC);
int startTime = endDateTime.getHour();
int startDay = endDateTime.getDayOfWeek().getValue();
int endTime = 0;
int startTimeEndDay = 0;

System.out.println("Start-Datetime: " + endDateTime);

if (startTime > START_OF_DAY && startTime <= 12) {
    startTimeEndDay = (END_OF_DAY - startTime - LUNCH);
} else if (startTime >= 13 && startTime <= END_OF_DAY) {
    startTimeEndDay = END_OF_DAY - startTime;
}

endTime = START_OF_DAY + (WORKING_HOURS - startTimeEndDay);
if (startTimeEndDay == 4) {
    endTime += LUNCH;
}

endDateTime = endDateTime.withHour(endTime);

if (startTime != START_OF_DAY) {
    if (startDay != 5) {
        endDateTime = endDateTime.plusDays(1);
    } else {
        endDateTime = endDateTime.plusDays(3);
    }
}     

System.out.println("Expiry-Datetime: " + endDateTime);

}

For example:
Input: 05.01.2023 15:32
Output: 06.01.2023 16:32

Input: 06.01.2023 08:37
Output: 09.01.2023 09:37

buec95
  • 91
  • 8