Java Code Examples for java.time.ZonedDateTime#getHour()

The following examples show how to use java.time.ZonedDateTime#getHour() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SerializerH3ZonedDateTime.java    From baratine with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void writeObject(OutRawH3 os,
                        int defId,
                        ZonedDateTime time,
                        OutH3 out)
{
  os.writeObject(typeSequence());

  long l = time.getYear() << 8;

  l |= time.getMonthValue();
  l <<= 8;

  l |= time.getDayOfMonth();
  l <<= 32;

  l |= (time.getHour() * 60 * 60 + time.getMinute() * 60 + time.getSecond());

  os.writeLong(l);
  os.writeLong(time.getNano());

  os.writeString(time.getZone().getId());
}
 
Example 2
Source File: SingleExecutionTime.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
private boolean dateValuesInExpectedRanges(final ZonedDateTime validCronDate, final ZonedDateTime date) {
    boolean everythingInRange = true;
    if (cronDefinition.getFieldDefinition(YEAR) != null) {
        everythingInRange = validCronDate.getYear() == date.getYear();
    }
    if (cronDefinition.getFieldDefinition(MONTH) != null) {
        everythingInRange = everythingInRange && validCronDate.getMonthValue() == date.getMonthValue();
    }
    if (cronDefinition.getFieldDefinition(DAY_OF_MONTH) != null) {
        everythingInRange = everythingInRange && validCronDate.getDayOfMonth() == date.getDayOfMonth();
    }
    if (cronDefinition.getFieldDefinition(DAY_OF_WEEK) != null) {
        everythingInRange = everythingInRange && validCronDate.getDayOfWeek().getValue() == date.getDayOfWeek().getValue();
    }
    if (cronDefinition.getFieldDefinition(HOUR) != null) {
        everythingInRange = everythingInRange && validCronDate.getHour() == date.getHour();
    }
    if (cronDefinition.getFieldDefinition(MINUTE) != null) {
        everythingInRange = everythingInRange && validCronDate.getMinute() == date.getMinute();
    }
    if (cronDefinition.getFieldDefinition(SECOND) != null) {
        everythingInRange = everythingInRange && validCronDate.getSecond() == date.getSecond();
    }
    return everythingInRange;
}
 
Example 3
Source File: RangeFilterTests.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "ZonedDateTimeRanges")
public void testRangeOfZonedDateTimes(ZonedDateTime start, ZonedDateTime end, Duration step, boolean parallel) {
    final boolean ascend = start.isBefore(end);
    final Range<ZonedDateTime> range = Range.of(start, end, step, v -> v.getHour() == 6);
    final Array<ZonedDateTime> array = range.toArray(parallel);
    final ZonedDateTime first = array.first(v -> true).map(ArrayValue::getValue).get();
    final ZonedDateTime last = array.last(v -> true).map(ArrayValue::getValue).get();
    Assert.assertEquals(array.typeCode(), ArrayType.ZONED_DATETIME);
    Assert.assertTrue(!array.style().isSparse());
    Assert.assertEquals(range.start(), start, "The range start");
    Assert.assertEquals(range.end(), end, "The range end");
    int index = 0;
    ZonedDateTime value = first;
    while (ascend ? value.isBefore(last) : value.isAfter(last)) {
        final ZonedDateTime actual = array.getValue(index);
        Assert.assertEquals(actual, value, "Value matches at " + index);
        Assert.assertTrue(ascend ? actual.compareTo(start) >= 0 && actual.isBefore(end) : actual.compareTo(start) <= 0 && actual.isAfter(end), "Value in bounds at " + index);
        value = ascend ? value.plus(step) : value.minus(step);
        while (value.getHour() == 6) value = ascend ? value.plus(step) : value.minus(step);
        index++;
    }
}
 
Example 4
Source File: RoundLcdClockSkin.java    From Medusa with Apache License 2.0 6 votes vote down vote up
private void drawHours(final ZonedDateTime TIME) {
    int hourCounter = 1;
    int hour        = TIME.getHour();
    double strokeWidth = size * 0.06;
    hoursCtx.setLineCap(StrokeLineCap.BUTT);
    hoursCtx.clearRect(0, 0, size, size);
    for (int i = 450 ; i >= 90 ; i--) {
        hoursCtx.save();
        if (i % 30 == 0) {
            //draw hours
            hoursCtx.setStroke(hourColor);
            hoursCtx.setLineWidth(strokeWidth);
            if (hour == 0 || hour == 12) {
                hoursCtx.strokeArc(strokeWidth * 0.5, strokeWidth * 0.5, size - strokeWidth, size - strokeWidth, i + 1 - 30, 28, ArcType.OPEN);
            } else if (hourCounter <= (TIME.get(ChronoField.AMPM_OF_DAY) == 1 ? hour - 12 : hour)) {
                hoursCtx.strokeArc(strokeWidth * 0.5, strokeWidth * 0.5, size - strokeWidth, size - strokeWidth, i + 1 - 30, 28, ArcType.OPEN);
                hourCounter++;
            }
        }
        hoursCtx.restore();
    }
}
 
Example 5
Source File: WorkLog.java    From public with Apache License 2.0 6 votes vote down vote up
public void stopWorking(Person person) {
    ZonedDateTime start = workStart.getOrDefault(person, null);
    if (start == null) {
        throw new IllegalStateException("This person hasn't started working!");
    }

    ZonedDateTime stop = ZonedDateTime.now();

    int hours = 0;
    if (start.toLocalDate().equals(stop.toLocalDate())) {
        // Easy case, it's the same day
        hours = stop.getHour() - start.getHour();
    } else {
        // A bit more complicated
        hours += 24 - start.getHour();
        hours += stop.getDayOfYear() - start.getDayOfYear();
        hours += stop.getHour();
    }

    hoursWorked.merge(person, hours, Integer::sum);
    workStart.remove(person);
}
 
Example 6
Source File: JavatimeTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 7
Source File: JavatimeTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 8
Source File: JavatimeTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 9
Source File: Clock.java    From Medusa with Apache License 2.0 5 votes vote down vote up
/**
 * Calling this method will check for the current time of the day and
 * switches on/off the night mode.
 * @param TIME
 */
private void checkForNight(final ZonedDateTime TIME) {
    int hour   = TIME.getHour();
    int minute = TIME.getMinute();

    if (0 <= hour && minute >= 0 && hour <= 5 && minute <= 59|| 17 <= hour && minute <= 59 && hour <= 23 && minute <= 59) {
        if(isNightMode()) return;
        setNightMode(true);
    } else {
        if (!isNightMode()) return;
        setNightMode(false);
    }
}
 
Example 10
Source File: JavatimeTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 11
Source File: JavatimeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 12
Source File: TimeHelper.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Formats a datetime stamp from an {@code instant} using a formatting pattern.
 *
 * <p>Note: a formatting pattern containing 'a' (for the period; AM/PM) is treated differently at noon/midday.
 * Using that pattern with a datetime whose time falls on "12:00 PM" will cause it to be formatted as "12:00 NOON".</p>
 *
 * @param instant  the instant to be formatted
 * @param timeZone the time zone to compute local datetime
 * @param pattern  formatting pattern, see Oracle docs for DateTimeFormatter for pattern table
 * @return the formatted datetime stamp string
 */
private static String formatInstant(Instant instant, ZoneId timeZone, String pattern) {
    if (instant == null || timeZone == null || pattern == null) {
        return "";
    }
    ZonedDateTime zonedDateTime = instant.atZone(timeZone);
    String processedPattern = pattern;
    if (zonedDateTime.getHour() == 12 && zonedDateTime.getMinute() == 0) {
        processedPattern = pattern.replace("a", "'NOON'");
    }
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(processedPattern);
    return zonedDateTime.format(formatter);
}
 
Example 13
Source File: IncrementalTimeConverterUtil.java    From siddhi with Apache License 2.0 5 votes vote down vote up
private static long getNextEmitTimeForHour(long currentTime, String timeZone) {
    ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(currentTime),
            ZoneId.of(timeZone));
    if (zonedDateTime.getHour() == 23) {
        if (zonedDateTime.getDayOfMonth() + 1 > zonedDateTime.getMonth().length(zonedDateTime.getYear() % 4 == 0)) {
            // if the current day is the last day of the month
            if (zonedDateTime.getMonthValue() == 12) {
                // For a time in December, emit time should be beginning of January next year
                return ZonedDateTime
                        .of(zonedDateTime.getYear() + 1, 1, 1, 0, 0, 0, 0, ZoneId.of(timeZone))
                        .toEpochSecond() * 1000;
            } else {
                // For any other month, the 1st day of next month must be considered
                return ZonedDateTime.of(zonedDateTime.getYear(), zonedDateTime.getMonthValue() + 1, 1, 0, 0, 0, 0,
                        ZoneId.of(timeZone)).toEpochSecond() * 1000;
            }
        } else {
            // for any other days
            return ZonedDateTime
                    .of(zonedDateTime.getYear(), zonedDateTime.getMonthValue(), zonedDateTime.getDayOfMonth() + 1,
                            0, 0, 0, 0, ZoneId.of(timeZone)).toEpochSecond() * 1000;
        }
    } else  {
        return ZonedDateTime
                .of(zonedDateTime.getYear(), zonedDateTime.getMonthValue(), zonedDateTime.getDayOfMonth(),
                        zonedDateTime.getHour() + 1, 0, 0, 0, ZoneId.of(timeZone)).toEpochSecond() * 1000;
    }
}
 
Example 14
Source File: OffsetTimeLib.java    From jdmn with Apache License 2.0 5 votes vote down vote up
public Integer hour(ZonedDateTime dateTime) {
    if (dateTime == null) {
        return null;
    }

    return dateTime.getHour();
}
 
Example 15
Source File: BackupTask.java    From sherlock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Method to backup redis data as redis local dump and (as json dump if specified).
 * @param timestampMinutes ping timestamp (in minutes) of backup task thread
 * @throws IOException exception
 */
public void backupRedisDB(long timestampMinutes) throws IOException {
    ZonedDateTime date = TimeUtils.zonedDateTimeFromMinutes(timestampMinutes);
    // save redis snapshot
    if (date.getMinute() == 0 && date.getHour() == 0) {
        jobMetadataAccessor.saveRedisJobsMetadata();
        // save redis data as json file if path is specified
        if (CLISettings.BACKUP_REDIS_DB_PATH != null) {
            BackupUtils.startBackup();
        }
    }
}
 
Example 16
Source File: WallClockTimeImpl.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Obtain current wall clock time in UTC time zone
 * @return current wall clock time in UTC time zone
 */
public static WallClockTime utc() {
    ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
    int years = now.getYear();
    int months = now.getMonthValue();
    int days = now.getDayOfMonth();
    int hours = now.getHour();
    int minutes = now.getMinute();
    double seconds = (double) now.getSecond() + ((double) now.getNano()) / 1e9;
    return new WallClockTimeImpl(years, months, days, hours, minutes, seconds);
}
 
Example 17
Source File: JavatimeTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 18
Source File: JavatimeTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 19
Source File: Calendar.java    From calendar-component with Apache License 2.0 4 votes vote down vote up
private static int getMinuteOfDay(ZonedDateTime date) {
    return date.getHour() * 60 + date.getMinute();
}
 
Example 20
Source File: Scatter3dArranger.java    From constellation with Apache License 2.0 4 votes vote down vote up
private float getFloatValueFromObject(Object attributeValue, boolean logarithmic) {
    if (attributeValue == null) {
        return 0.0f;
    }

    if (attributeValue instanceof Float) {
        return scaleValue((float) attributeValue, logarithmic);
    }

    if (attributeValue instanceof Double) {
        return scaleValue((float) attributeValue, logarithmic);
    }

    if (attributeValue instanceof String) {
        String val = (String) attributeValue;
        float finalVal = 0.0f;
        float multiplier = 1;
        for (int i = 0; i < val.length(); i++) {
            char ch = val.charAt(i);
            float chVal = (float) ch;
            finalVal += ((float) chVal) * multiplier;
            multiplier /= 26;
        }
        return scaleValue(finalVal, logarithmic);
    }

    if (attributeValue instanceof Integer) {
        float ret = (Integer) attributeValue;
        return scaleValue(ret, logarithmic);
    }

    if (attributeValue instanceof ConstellationColor) {
        ConstellationColor color = (ConstellationColor) attributeValue;
        float red = color.getRed() / 256;
        float green = color.getGreen() / 256;
        float blue = color.getBlue() / 256;
        return scaleValue((red + green + blue) * 100, logarithmic);
    }

    if (attributeValue instanceof ZonedDateTime) {
        ZonedDateTime c = (ZonedDateTime) attributeValue;
        float year = c.getYear();
        float month = c.getMonthValue();
        float monthDay = c.getDayOfMonth();
        float hour = c.getHour();
        float minute = c.getMinute();
        return scaleValue((year - 2010) + month / 12 + monthDay / (366) + hour / (366 * 24) + minute / (366 * 24 * 60), logarithmic);
    }

    if (attributeValue instanceof RawData) {
        String s = ((RawData) attributeValue).toString();
        return getFloatValueFromObject(s, logarithmic);
    }

    return 0.0f;
}