Java Code Examples for java.time.temporal.Temporal#get()

The following examples show how to use java.time.temporal.Temporal#get() . 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: CronAdjuster.java    From openhab-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Check for the nearest working day. E.g. 15W is the nearest working day around the 15th.
 *
 * @param temporal temporal to check
 * @return true if temporal is nearest to working day
 */
static boolean isNearestWorkDay(final Temporal temporal, final int target) {
    final int day = temporal.get(ChronoField.DAY_OF_MONTH);
    final DayOfWeek type = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));

    switch (type) {
        case MONDAY:
            return day == target // the actual day
                    || day == target + 1 // target was on a Sunday
                    || (day == target + 2 && day == 3); // target was Saturday 1

        case TUESDAY:
        case WEDNESDAY:
        case THURSDAY:
            return day == target;

        case FRIDAY:
            return day == target || day + 1 == target;

        // not a work day
        default:
        case SATURDAY:
        case SUNDAY:
            return false;
    }
}
 
Example 2
Source File: CronAdjuster.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @param temporal temporal to check
 * @return true if temporal is the last working day in the month
 */
private static boolean isLastWorkingDayInMonth(Temporal temporal) {
    final int day = temporal.get(ChronoField.DAY_OF_MONTH);
    final DayOfWeek type = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));
    final int max = (int) ChronoField.DAY_OF_MONTH.rangeRefinedBy(temporal).getMaximum();

    switch (type) {
        case MONDAY:
        case TUESDAY:
        case WEDNESDAY:
        case THURSDAY:
            return day == max;

        case FRIDAY:
            return day + 2 >= max;

        default:
        case SATURDAY:
        case SUNDAY:
            return false;
    }
}
 
Example 3
Source File: TemporalWriter.java    From night-config with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static void writeHour(Temporal temporal, CharacterOutput output) {
	int hours = temporal.get(ChronoField.HOUR_OF_DAY);
	int minutes = temporal.get(ChronoField.MINUTE_OF_HOUR);
	int seconds = temporal.get(ChronoField.SECOND_OF_MINUTE);
	writePadded(hours, 2, output);
	output.write(':');
	writePadded(minutes, 2, output);
	output.write(':');
	writePadded(seconds, 2, output);
	if (temporal.isSupported(ChronoField.NANO_OF_SECOND)) {
		int nanos = temporal.get(ChronoField.NANO_OF_SECOND);
		if (nanos != 0) {
			output.write('.');
			writePaddedAndTrimmed(nanos, 9, output);
		}
	} else if (temporal.isSupported(ChronoField.MILLI_OF_SECOND)) {
		int millis = temporal.get(ChronoField.MILLI_OF_SECOND);
		if (millis != 0) {
			output.write('.');
			writePaddedAndTrimmed(millis, 6, output);
		}
	}
}
 
Example 4
Source File: DateFormatter.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void format(StringBuilder sb, Temporal cal)
{
  int milli = cal.get(ChronoField.MILLI_OF_SECOND);
  
  sb.append((milli / 100) % 10);
  sb.append((milli / 10) % 10);
  sb.append((milli) % 10);
}
 
Example 5
Source File: CronAdjuster.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param temporal temporal to check
 * @return true if temporal is the last day in the month
 */
private static boolean isLastDayInMonth(Temporal temporal) {
    final int day = temporal.get(ChronoField.DAY_OF_MONTH);
    final int max = (int) ChronoField.DAY_OF_MONTH.rangeRefinedBy(temporal).getMaximum();

    return day == max;
}
 
Example 6
Source File: CronAdjuster.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param temporal temporal to check
 * @return true if temporal is the last day in the month
 */
private static boolean isLastDayInMonth(final Temporal temporal) {
    final int day = temporal.get(ChronoField.DAY_OF_MONTH);
    final int max = (int) ChronoField.DAY_OF_MONTH.rangeRefinedBy(temporal).getMaximum();

    return day == max;
}
 
Example 7
Source File: CronAdjuster.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param temporal temporal to check
 * @return true if temporal is the last week day in this month. I.e. the last Saturday
 */
private static boolean isLastOfThisWeekDayInMonth(final Temporal temporal) {
    final int day = temporal.get(ChronoField.DAY_OF_MONTH);
    final int max = (int) ChronoField.DAY_OF_MONTH.rangeRefinedBy(temporal).getMaximum();

    return day + 7 > max;
}
 
Example 8
Source File: UtcDateConverter.java    From agrest with Apache License 2.0 5 votes vote down vote up
@Override
protected T valueNonNull(JsonNode node) {

	Temporal temporal = ISODateParser.parser().fromString(node.asText());

	GregorianCalendar calendar = new GregorianCalendar();
	calendar.setTimeInMillis(0);

	ZoneId zone = temporal.query(TemporalQueries.zone());
	if (zone != null) {
		calendar.setTimeZone(TimeZone.getTimeZone(zone));
	}

	if (temporal.isSupported(ChronoField.YEAR)) {
		int year = temporal.get(ChronoField.YEAR);
		int monthOfYear = temporal.get(ChronoField.MONTH_OF_YEAR);
		int dayOfMonth = temporal.get(ChronoField.DAY_OF_MONTH);
		calendar.set(year, --monthOfYear, dayOfMonth);
	}

	if (temporal.isSupported(ChronoField.HOUR_OF_DAY)) {
		int hours = temporal.get(ChronoField.HOUR_OF_DAY);
		int minutes = temporal.get(ChronoField.MINUTE_OF_HOUR);
		int seconds = temporal.get(ChronoField.SECOND_OF_MINUTE);
		calendar.set(Calendar.HOUR_OF_DAY, hours);
		calendar.set(Calendar.MINUTE, minutes);
		calendar.set(Calendar.SECOND, seconds);
	}

	if (temporal.isSupported(ChronoField.MILLI_OF_SECOND)) {
		int millis = temporal.get(ChronoField.MILLI_OF_SECOND);
		calendar.setTimeInMillis(calendar.getTimeInMillis() + millis);
	}

	return normalizer.apply(calendar.getTime());
}
 
Example 9
Source File: TemporalWriter.java    From night-config with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void writeDate(Temporal temporal, CharacterOutput output) {
	int year = temporal.get(ChronoField.YEAR);
	int month = temporal.get(ChronoField.MONTH_OF_YEAR);
	int day = temporal.get(ChronoField.DAY_OF_MONTH);
	writePadded(year, 4, output);
	output.write('-');
	writePadded(month, 2, output);
	output.write('-');
	writePadded(day, 2, output);
}
 
Example 10
Source File: CronAdjuster.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param temporal temporal to check
 * @return true if temporal is the last week day in this month. I.e. the last Saturday
 */
private static boolean isLastOfThisWeekDayInMonth(Temporal temporal) {
    final int day = temporal.get(ChronoField.DAY_OF_MONTH);
    final int max = (int) ChronoField.DAY_OF_MONTH.rangeRefinedBy(temporal).getMaximum();

    return day + 7 > max;
}
 
Example 11
Source File: DateFormatter.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void format(StringBuilder sb, Temporal cal)
{
  int second = cal.get(ChronoField.SECOND_OF_MINUTE);
  
  sb.append((second / 10) % 6);
  sb.append((second) % 10);
}
 
Example 12
Source File: DateFormatter.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void format(StringBuilder sb, Temporal cal)
{
  int minute = cal.get(ChronoField.MINUTE_OF_HOUR);
  
  sb.append((minute / 10) % 6);
  sb.append((minute) % 10);
}
 
Example 13
Source File: DateFormatter.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void format(StringBuilder sb, Temporal cal)
{
  int hour = cal.get(ChronoField.HOUR_OF_DAY);
  
  sb.append(hour / 10);
  sb.append(hour % 10);
}
 
Example 14
Source File: DateFormatter.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void format(StringBuilder sb, Temporal cal)
{
  int day = cal.get(ChronoField.DAY_OF_MONTH);
  
  sb.append(day / 10);
  sb.append(day % 10);
}
 
Example 15
Source File: DateFormatter.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void format(StringBuilder sb, Temporal cal)
{
  int month = cal.get(ChronoField.MONTH_OF_YEAR);
  
  sb.append((month) / 10);
  sb.append((month) % 10);
}
 
Example 16
Source File: DateFormatter.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void format(StringBuilder sb, Temporal cal)
{
  int year = cal.get(ChronoField.YEAR);
  sb.append(year / 1000 % 10);
  sb.append(year / 100 % 10);
  sb.append(year / 10 % 10);
  sb.append(year % 10);
}
 
Example 17
Source File: LocalDateLib.java    From jdmn with Apache License 2.0 5 votes vote down vote up
public Integer weekday(Temporal dateTime) {
    if (dateTime == null) {
        return null;
    }

    return dateTime.get(ChronoField.DAY_OF_WEEK);
}
 
Example 18
Source File: LocalDateLib.java    From jdmn with Apache License 2.0 5 votes vote down vote up
public Integer day(Temporal dateTime) {
    if (dateTime == null) {
        return null;
    }

    return dateTime.get(ChronoField.DAY_OF_MONTH);
}
 
Example 19
Source File: LocalDateLib.java    From jdmn with Apache License 2.0 5 votes vote down vote up
public Integer month(Temporal dateTime) {
    if (dateTime == null) {
        return null;
    }

    return dateTime.get(ChronoField.MONTH_OF_YEAR);
}
 
Example 20
Source File: CronAdjuster.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * A check that we do not go ballistic with the year
 */
private static boolean checkMaxYear(Temporal temporal) {
    return temporal.get(ChronoField.YEAR) >= 2200;
}