Example usage for java.time ZoneId getRules

List of usage examples for java.time ZoneId getRules

Introduction

In this page you can find the example usage for java.time ZoneId getRules.

Prototype

public abstract ZoneRules getRules();

Source Link

Document

Gets the time-zone rules for this ID allowing calculations to be performed.

Usage

From source file:Main.java

public static void main(String[] args) {
    ZoneId z = ZoneId.systemDefault();
    ZoneRules r = z.getRules();
    System.out.println(r);// w  w  w. j  a va  2 s .c  o  m
}

From source file:Main.java

public static void main(String[] args) {
    LocalDateTime now = LocalDateTime.now();
    ZoneId usChicago = ZoneId.of("America/Chicago");
    System.out.println("Zone ID:  " + usChicago.getId());

    ZoneRules rules = usChicago.getRules();
    System.out.println("isFixedOffset(): " + rules.isFixedOffset());
    ZoneOffset offset = rules.getOffset(now);
    System.out.println("Zone offset: " + offset);

    List<ZoneOffsetTransition> transitions = rules.getTransitions();
    System.out.println(transitions);

}

From source file:de.rkl.tools.tzconv.model.ApplicationModel.java

private static SetMultimap<ZoneOffset, ZoneId> sortAvailableZoneIds() {
    final SortedSetMultimap<ZoneOffset, ZoneId> zoneIdMap = TreeMultimap.create(Ordering.natural().reverse(),
            new Ordering<ZoneId>() {
                @Override/*from  ww w. j ava2s.  co  m*/
                public int compare(final ZoneId zoneId1, final ZoneId zoneId2) {
                    return ComparisonChain.start().compare(zoneId1.toString(), zoneId2.toString()).result();
                }
            }.nullsFirst());
    ZoneId.getAvailableZoneIds().stream().forEach(zoneId -> {
        final ZoneId zoneIdObject = ZoneId.of(zoneId);
        zoneIdMap.put(zoneIdObject.getRules().getStandardOffset(Instant.now()), zoneIdObject);
    });
    return ImmutableSetMultimap.copyOf(zoneIdMap);
}

From source file:daylightchart.daylightchart.calculation.RiseSetUtility.java

/**
 * Calculator for sunrise and sunset times for a year.
 *
 * @param location//  www  . j  av a 2s  .  com
 *        Location
 * @param year
 *        Year
 * @param options
 *        Options
 * @return Full years sunset and sunrise times for a location
 */
public static RiseSetYearData createRiseSetYear(final Location location, final int year,
        final Options options) {
    final TimeZoneOption timeZoneOption = options.getTimeZoneOption();
    final ZoneId zoneId;
    if (location != null) {
        final String timeZoneId;
        if (timeZoneOption != null && timeZoneOption == TimeZoneOption.USE_TIME_ZONE) {
            timeZoneId = location.getTimeZoneId();
        } else {
            timeZoneId = DefaultTimezones.createGMTTimeZoneId(location.getPointLocation().getLongitude());
        }
        zoneId = ZoneId.of(timeZoneId);
    } else {
        zoneId = ZoneId.systemDefault();
    }
    final boolean timeZoneUsesDaylightTime = !zoneId.getRules().getTransitionRules().isEmpty();
    final boolean useDaylightTime = timeZoneUsesDaylightTime && timeZoneOption != TimeZoneOption.USE_LOCAL_TIME;
    boolean wasDaylightSavings = false;

    final TwilightType twilight = options.getTwilightType();
    final RiseSetYearData riseSetYear = new RiseSetYearData(location, twilight, year);
    riseSetYear.setUsesDaylightTime(useDaylightTime);
    for (final LocalDate date : getYearsDates(year)) {
        final boolean inDaylightSavings = zoneId.getRules()
                .isDaylightSavings(date.atStartOfDay().atZone(zoneId).toInstant());
        if (wasDaylightSavings != inDaylightSavings) {
            if (!wasDaylightSavings) {
                riseSetYear.setDstStart(date);
            } else {
                riseSetYear.setDstEnd(date);
            }
        }
        wasDaylightSavings = inDaylightSavings;

        final RawRiseSet riseSet = calculateRiseSet(location, date, useDaylightTime, inDaylightSavings,
                TwilightType.NO);
        riseSetYear.addRiseSet(riseSet);

        if (twilight != null) {
            final RawRiseSet twilights = calculateRiseSet(location, date, useDaylightTime, inDaylightSavings,
                    twilight);
            riseSetYear.addTwilight(twilights);
        }

    }

    // Create band for twilight, clock-shift taken into account
    createBands(riseSetYear, DaylightBandType.twilight);
    // Create band, clock-shift taken into account
    createBands(riseSetYear, DaylightBandType.with_clock_shift);
    // Create band, without clock shift
    createBands(riseSetYear, DaylightBandType.without_clock_shift);

    return riseSetYear;

}

From source file:org.nodatime.tzvalidate.Java8Dump.java

@Override
public ZoneTransitions getTransitions(String id, int fromYear, int toYear) {
    ZoneId zone = ZoneId.of(id);
    ZoneRules rules = zone.getRules();
    DateTimeFormatter nameFormat = DateTimeFormatter.ofPattern("zzz", Locale.US);
    ZoneTransitions transitions = new ZoneTransitions(id);

    Instant start = ZonedDateTime.of(fromYear, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant();
    transitions.addTransition(null, rules.getOffset(start).getTotalSeconds() * 1000,
            rules.isDaylightSavings(start), nameFormat.format(start.atZone(zone)));
    Instant end = ZonedDateTime.of(toYear, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant();

    ZoneOffsetTransition transition = rules.nextTransition(start.minusNanos(1));
    while (transition != null && transition.getInstant().isBefore(end)) {
        Instant instant = transition.getInstant();
        transitions.addTransition(new Date(instant.toEpochMilli()),
                rules.getOffset(instant).getTotalSeconds() * 1000, rules.isDaylightSavings(instant),
                nameFormat.format(instant.atZone(zone)));
        transition = rules.nextTransition(instant);
    }//from w w w  .j  a v a  2s  . co  m
    return transitions;
}

From source file:org.openhab.binding.airvisualnode.internal.handler.AirVisualNodeHandler.java

private State getChannelState(String channelId, NodeData nodeData) {
    State state = UnDefType.UNDEF;

    // Handle system channel IDs separately, because 'switch/case' expressions must be constant expressions
    if (CHANNEL_BATTERY_LEVEL.equals(channelId)) {
        state = new DecimalType(BigDecimal.valueOf(nodeData.getStatus().getBattery()).longValue());
    } else if (CHANNEL_WIFI_STRENGTH.equals(channelId)) {
        state = new DecimalType(
                BigDecimal.valueOf(Math.max(0, nodeData.getStatus().getWifiStrength() - 1)).longValue());
    } else {/*from w  w w. j a  va2s.co m*/
        // Handle binding-specific channel IDs
        switch (channelId) {
        case CHANNEL_CO2:
            state = new QuantityType<>(nodeData.getMeasurements().getCo2Ppm(), PARTS_PER_MILLION);
            break;
        case CHANNEL_HUMIDITY:
            state = new QuantityType<>(nodeData.getMeasurements().getHumidityRH(), PERCENT);
            break;
        case CHANNEL_AQI_US:
            state = new QuantityType<>(nodeData.getMeasurements().getPm25AQIUS(), ONE);
            break;
        case CHANNEL_PM_25:
            // PM2.5 is in ug/m3
            state = new QuantityType<>(nodeData.getMeasurements().getPm25Ugm3(),
                    MICRO(GRAM).divide(CUBIC_METRE));
            break;
        case CHANNEL_TEMP_CELSIUS:
            state = new QuantityType<>(nodeData.getMeasurements().getTemperatureC(), CELSIUS);
            break;
        case CHANNEL_TIMESTAMP:
            // It seem the Node timestamp is Unix timestamp converted from UTC time plus timezone offset.
            // Not sure about DST though, but it's best guess at now
            Instant instant = Instant.ofEpochMilli(nodeData.getStatus().getDatetime() * 1000L);
            ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC"));
            ZoneId zoneId = ZoneId.of(nodeData.getSettings().getTimezone());
            ZoneRules zoneRules = zoneId.getRules();
            zonedDateTime.minus(Duration.ofSeconds(zoneRules.getOffset(instant).getTotalSeconds()));
            if (zoneRules.isDaylightSavings(instant)) {
                zonedDateTime.minus(Duration.ofSeconds(zoneRules.getDaylightSavings(instant).getSeconds()));
            }
            state = new DateTimeType(zonedDateTime);
            break;
        case CHANNEL_USED_MEMORY:
            state = new DecimalType(BigDecimal.valueOf(nodeData.getStatus().getUsedMemory()).longValue());
            break;
        }
    }

    return state;
}