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);
}
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();
}
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);
}
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;
}
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;
}
Aggregations