Java Instant Calculate restrictToDaytime(Instant instantToRestrict, Instant thresholdTime, ZoneId userZoneId)

Here you can find the source of restrictToDaytime(Instant instantToRestrict, Instant thresholdTime, ZoneId userZoneId)

Description

restrict To Daytime

License

Open Source License

Declaration

public static Instant restrictToDaytime(Instant instantToRestrict, Instant thresholdTime, ZoneId userZoneId) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.time.*;

public class Main {
    private static final int latestHourForAutomatedMessages = 20;
    private static final int earliestHourForAutomatedMessage = 8;
    private static final LocalTime latestHour = LocalTime.of(latestHourForAutomatedMessages, 0);
    private static final LocalTime earliestHour = LocalTime.of(earliestHourForAutomatedMessage, 0);

    public static Instant restrictToDaytime(Instant instantToRestrict, Instant thresholdTime, ZoneId userZoneId) {
        ZonedDateTime zonedDateTime = instantToRestrict.atZone(userZoneId);
        if (zonedDateTime.getHour() <= earliestHourForAutomatedMessage) {
            zonedDateTime = ZonedDateTime.of(zonedDateTime.toLocalDate(), earliestHour, userZoneId);
            return thresholdTime == null || zonedDateTime.toInstant().isBefore(thresholdTime)
                    ? zonedDateTime.toInstant()
                    : thresholdTime;/*from w  w w  .  j  a va 2  s  .  c o  m*/
        } else if (zonedDateTime.getHour() >= latestHourForAutomatedMessages) {
            zonedDateTime = ZonedDateTime.of(zonedDateTime.toLocalDate(), latestHour, userZoneId);
            return zonedDateTime.toInstant();
        } else {
            return instantToRestrict;
        }
    }
}

Related

  1. isEqualOrAfterNow(Instant now, Instant instant)
  2. maxInstant(Instant v1, Instant v2)
  3. nanos(Instant instant)
  4. plus(Instant instant, Duration add)
  5. plusMilli(Instant _date, long _milli)
  6. roundUp(final Instant time, final Duration duration)
  7. secondsAgo(Instant end)