Search in sources :

Example 6 with DayOfWeek

use of java.time.DayOfWeek in project java8-tutorial by winterbe.

the class LocalDateTime1 method main.

public static void main(String[] args) {
    LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59);
    DayOfWeek dayOfWeek = sylvester.getDayOfWeek();
    // WEDNESDAY
    System.out.println(dayOfWeek);
    Month month = sylvester.getMonth();
    // DECEMBER
    System.out.println(month);
    long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY);
    // 1439
    System.out.println(minuteOfDay);
    Instant instant = sylvester.atZone(ZoneId.systemDefault()).toInstant();
    Date legacyDate = Date.from(instant);
    // Wed Dec 31 23:59:59 CET 2014
    System.out.println(legacyDate);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, yyyy - HH:mm");
    LocalDateTime parsed = LocalDateTime.parse("Nov 03, 2014 - 07:13", formatter);
    String string = parsed.format(formatter);
    // Nov 03, 2014 - 07:13
    System.out.println(string);
}
Also used : LocalDateTime(java.time.LocalDateTime) DayOfWeek(java.time.DayOfWeek) Month(java.time.Month) Instant(java.time.Instant) DateTimeFormatter(java.time.format.DateTimeFormatter) Date(java.util.Date)

Example 7 with DayOfWeek

use of java.time.DayOfWeek in project jgnash by ccavanaugh.

the class Engine method removeSecurityHistoryByDayOfWeek.

/**
     * Handles Background removal of {@code SecurityNode} history.  This can an expensive operation that block normal
     * operations, so the removal is partitioned into small events to prevent stalling.
     *
     * @param securityNode SecurityNode being processed
     * @param daysOfWeek   Collection of {code DayOfWeek} to remove
     */
public void removeSecurityHistoryByDayOfWeek(final SecurityNode securityNode, final Collection<DayOfWeek> daysOfWeek) {
    final Thread thread = new Thread(() -> {
        long delay = 0;
        for (final SecurityHistoryNode historyNode : new ArrayList<>(securityNode.getHistoryNodes())) {
            for (final DayOfWeek dayOfWeek : daysOfWeek) {
                if (historyNode.getLocalDate().getDayOfWeek() == dayOfWeek) {
                    backgroundExecutorService.schedule(new BackgroundCallable<>((Callable<Void>) () -> {
                        removeSecurityHistory(securityNode, historyNode.getLocalDate());
                        return null;
                    }), delay, TimeUnit.MILLISECONDS);
                    delay += 750;
                }
            }
        }
    });
    thread.setDaemon(true);
    thread.setPriority(Thread.MIN_PRIORITY);
    thread.start();
}
Also used : DayOfWeek(java.time.DayOfWeek) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable)

Example 8 with DayOfWeek

use of java.time.DayOfWeek in project jdk8u_jdk by JetBrains.

the class ZoneOffsetTransitionRule method readExternal.

/**
     * Reads the state from the stream.
     *
     * @param in  the input stream, not null
     * @return the created object, not null
     * @throws IOException if an error occurs
     */
static ZoneOffsetTransitionRule readExternal(DataInput in) throws IOException {
    int data = in.readInt();
    Month month = Month.of(data >>> 28);
    int dom = ((data & (63 << 22)) >>> 22) - 32;
    int dowByte = (data & (7 << 19)) >>> 19;
    DayOfWeek dow = dowByte == 0 ? null : DayOfWeek.of(dowByte);
    int timeByte = (data & (31 << 14)) >>> 14;
    TimeDefinition defn = TimeDefinition.values()[(data & (3 << 12)) >>> 12];
    int stdByte = (data & (255 << 4)) >>> 4;
    int beforeByte = (data & (3 << 2)) >>> 2;
    int afterByte = (data & 3);
    LocalTime time = (timeByte == 31 ? LocalTime.ofSecondOfDay(in.readInt()) : LocalTime.of(timeByte % 24, 0));
    ZoneOffset std = (stdByte == 255 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds((stdByte - 128) * 900));
    ZoneOffset before = (beforeByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + beforeByte * 1800));
    ZoneOffset after = (afterByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + afterByte * 1800));
    return ZoneOffsetTransitionRule.of(month, dom, dow, time, timeByte == 24, defn, std, before, after);
}
Also used : Month(java.time.Month) DayOfWeek(java.time.DayOfWeek) LocalTime(java.time.LocalTime) ZoneOffset(java.time.ZoneOffset)

Example 9 with DayOfWeek

use of java.time.DayOfWeek in project scylla by bptlab.

the class DateTimeUtils method getAvailabilityTime.

/**
 * Calculates the duration of availability from the start datetime of simulation to the given end datetime (which is
 * calculated from the provided time instant which describes the time relative to simulation start). The calculation
 * considers the timetable.
 *
 * @param timetable
 *            the timetable
 * @param timeInstant
 *            the time instant from which the end datetime is calculated
 * @return the duration of availability
 */
public static long getAvailabilityTime(List<TimetableItem> timetable, TimeInstant timeInstant) {
    long availabilityTime = 0;
    ZonedDateTime endDateTime = getDateTime(timeInstant);
    if (timetable == null) {
        return timeInstant.getTimeRounded(timeUnit);
    } else if (timetable.isEmpty()) {
        return availabilityTime;
    }
    int index = getTimeTableIndexWithinOrNext(startDateTime, timetable);
    ZonedDateTime dateTime = startDateTime;
    while (dateTime.compareTo(endDateTime) < 0) {
        TimetableItem item = timetable.get(index);
        DayOfWeek untilWeekday = item.getWeekdayTo();
        LocalTime untilTime = item.getEndTime();
        if (isWithin(dateTime, item)) {
            untilWeekday = endDateTime.getDayOfWeek();
            untilTime = endDateTime.toLocalTime();
        }
        ZonedDateTime dateTimeUntilEnd = getNextOrSameZonedDateTime(dateTime, untilWeekday, untilTime);
        DayOfWeek startWeekday = item.getWeekdayFrom();
        LocalTime startTime = item.getBeginTime();
        if (isWithin(dateTime, item)) {
            startWeekday = dateTime.getDayOfWeek();
            startTime = dateTime.toLocalTime();
        }
        ZonedDateTime dateTimeOfStart = getNextOrSameZonedDateTime(dateTime, startWeekday, startTime);
        if (dateTime.compareTo(dateTimeOfStart) < 0) {
            // i.e. dateTime is before dateTimeOfStart
            dateTime = dateTimeOfStart;
        }
        if (dateTimeUntilEnd.compareTo(endDateTime) > 0) {
            // end of timetable item is after end of simulation
            dateTimeUntilEnd = endDateTime;
        }
        long durationUntilEnd = chronoUnit.between(dateTime, dateTimeUntilEnd);
        availabilityTime += durationUntilEnd;
        dateTime = dateTimeUntilEnd;
        if (!dateTime.equals(endDateTime)) {
            // prepare next timetable item
            index++;
            if (index == timetable.size()) {
                index = 0;
            }
            TimetableItem nextItem = timetable.get(index);
            untilWeekday = nextItem.getWeekdayFrom();
            untilTime = nextItem.getBeginTime();
            dateTime = getNextZonedDateTime(dateTime, untilWeekday, untilTime);
        }
    }
    return availabilityTime;
}
Also used : DayOfWeek(java.time.DayOfWeek) LocalTime(java.time.LocalTime) ZonedDateTime(java.time.ZonedDateTime) TimetableItem(de.hpi.bpt.scylla.model.global.resource.TimetableItem)

Example 10 with DayOfWeek

use of java.time.DayOfWeek in project scylla by bptlab.

the class DateTimeUtils method isWithin.

/**
 * Checks whether provided datetime is within interval of given timetable item. Interval of timetable item is
 * left-closed, right-open [start,end).
 *
 * @param retrievalDateTime
 *            the datetime in question
 * @param timetableItem
 *            the timetable item in question
 * @return true if datetime is within the interval
 */
public static boolean isWithin(ZonedDateTime retrievalDateTime, TimetableItem timetableItem) {
    // info from retrievalDateTime
    DayOfWeek dayOfWeek = retrievalDateTime.getDayOfWeek();
    int hour = retrievalDateTime.getHour();
    int minute = retrievalDateTime.getMinute();
    int second = retrievalDateTime.getSecond();
    LocalTime localTime = LocalTime.of(hour, minute, second);
    // info from timetableItem
    DayOfWeek weekdayFrom = timetableItem.getWeekdayFrom();
    DayOfWeek weekdayTo = timetableItem.getWeekdayTo();
    LocalTime beginTime = timetableItem.getBeginTime();
    LocalTime endTime = timetableItem.getEndTime();
    if (weekdayFrom.getValue() < weekdayTo.getValue()) {
        if (weekdayFrom.getValue() <= dayOfWeek.getValue() && dayOfWeek.getValue() <= weekdayTo.getValue()) {
            if (weekdayFrom == dayOfWeek) {
                if (beginTime.compareTo(localTime) <= 0) {
                    return true;
                }
                ;
            } else if (weekdayTo == dayOfWeek) {
                if (localTime.compareTo(endTime) < 0) {
                    return true;
                }
            } else {
                // dayOfWeek is between weekdayFrom and weekdayTo
                return true;
            }
        }
    } else if (weekdayFrom.getValue() == weekdayTo.getValue()) {
        if (weekdayFrom == dayOfWeek) {
            // == weekdayTo
            if (beginTime.compareTo(endTime) < 0) {
                if (beginTime.compareTo(localTime) <= 0 && localTime.compareTo(endTime) < 0) {
                    return true;
                }
                ;
            } else {
                if (beginTime.compareTo(localTime) <= 0 || localTime.compareTo(endTime) < 0) {
                    return true;
                }
            }
        }
    } else // e.g. if weekdayFrom is SUNDAY and weekdayTo is TUESDAY
    if (weekdayFrom.getValue() > weekdayTo.getValue()) {
        if (weekdayFrom.getValue() <= dayOfWeek.getValue() || dayOfWeek.getValue() <= weekdayTo.getValue()) {
            if (weekdayFrom == dayOfWeek) {
                if (beginTime.compareTo(localTime) <= 0) {
                    return true;
                }
                ;
            } else if (weekdayTo == dayOfWeek) {
                if (localTime.compareTo(endTime) < 0) {
                    return true;
                }
            } else {
                // dayOfWeek is between weekdayTo and weekdayFrom
                return true;
            }
        }
    }
    return false;
}
Also used : DayOfWeek(java.time.DayOfWeek) LocalTime(java.time.LocalTime)

Aggregations

DayOfWeek (java.time.DayOfWeek)27 LocalDate (java.time.LocalDate)10 Test (org.testng.annotations.Test)9 Month (java.time.Month)8 LocalTime (java.time.LocalTime)7 TimetableItem (de.hpi.bpt.scylla.model.global.resource.TimetableItem)5 ZonedDateTime (java.time.ZonedDateTime)4 LocalDateTime (java.time.LocalDateTime)3 DateTimeFormatter (java.time.format.DateTimeFormatter)2 WeekFields (java.time.temporal.WeekFields)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Locale (java.util.Locale)2 Map (java.util.Map)2 TimeUnit (java.util.concurrent.TimeUnit)2 AssetAttribute (org.openremote.model.asset.AssetAttribute)2 ScyllaValidationException (de.hpi.bpt.scylla.exception.ScyllaValidationException)1 ProcessNodeInfo (de.hpi.bpt.scylla.logger.ProcessNodeInfo)1 ProcessNodeTransitionType (de.hpi.bpt.scylla.logger.ProcessNodeTransitionType)1 ResourceInfo (de.hpi.bpt.scylla.logger.ResourceInfo)1