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

The following examples show how to use java.time.temporal.Temporal#isSupported() . 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: TemporalWriter.java    From night-config with GNU Lesser General Public License v3.0 6 votes vote down vote up
static void write(Temporal temporal, CharacterOutput output) {
	if (temporal.isSupported(ChronoField.YEAR)) {
		writeDate(temporal, output);
		if (temporal.isSupported(ChronoField.HOUR_OF_DAY)) {
			output.write('T');
			writeHour(temporal, output);
			if (temporal.isSupported(ChronoField.OFFSET_SECONDS)) {
				int offsetSeconds = temporal.get(ChronoField.OFFSET_SECONDS);
				ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSeconds);
				output.write(offset.getId());
			}
		}
	} else if (temporal.isSupported(ChronoField.HOUR_OF_DAY)) {
		writeHour(temporal, output);
	}
}
 
Example 2
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 3
Source File: GamaDate.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
public GamaDate(final IScope scope, final Temporal d) {
	final ZoneId zone;
	if (d instanceof ChronoZonedDateTime) {
		zone = ZonedDateTime.from(d).getZone();
	} else if (d.isSupported(ChronoField.OFFSET_SECONDS)) {
		zone = ZoneId.ofOffset("", ZoneOffset.ofTotalSeconds(d.get(ChronoField.OFFSET_SECONDS)));
	} else {
		zone = GamaDateType.DEFAULT_ZONE;
	}
	if (!d.isSupported(MINUTE_OF_HOUR)) {
		internal = ZonedDateTime.of(LocalDate.from(d), LocalTime.of(0, 0), zone);
	} else if (!d.isSupported(DAY_OF_MONTH)) {
		internal = ZonedDateTime.of(LocalDate.from(
				scope == null ? Dates.DATES_STARTING_DATE.getValue() : scope.getSimulation().getStartingDate()),
				LocalTime.from(d), zone);
	} else {
		internal = d;
	}
}
 
Example 4
Source File: DvDate.java    From archie with Apache License 2.0 5 votes vote down vote up
@Override
public void setValue(Temporal value) {
    if(value.isSupported(ChronoField.HOUR_OF_DAY)) {
        //TODO: do we really need this validation?
        throw new IllegalArgumentException("value must only have a year, month or date, but this supports hours: " + value);
    }
    this.value = value;
}
 
Example 5
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 6
Source File: TestJDBC42JavaTimeConversions.java    From jaybird with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public boolean isSupportedBy(Temporal temporal) {
    return temporal.isSupported(this);
}