Example usage for org.joda.time DateTimeZone getID

List of usage examples for org.joda.time DateTimeZone getID

Introduction

In this page you can find the example usage for org.joda.time DateTimeZone getID.

Prototype

@ToString
public final String getID() 

Source Link

Document

Gets the ID of this datetime zone.

Usage

From source file:ch.corten.aha.worldclock.TimeZoneInfo.java

License:Open Source License

/**
 * Convert a joda-time {@link org.joda.time.DateTimeZone} to an equivalent Java {@link java.util.TimeZone}.
 *
 * @param dateTimeZone a joda-time {@link org.joda.time.DateTimeZone}
 * @param time         the time when the time zones should be equivalent.
 * @return a Java {@link java.util.TimeZone} with the same offset for the given time.
 *//*w  w  w  . j  a  v  a 2 s.c  om*/
public static TimeZone convertToJavaTimeZone(DateTimeZone dateTimeZone, long time) {
    TimeZone timeZone = dateTimeZone.toTimeZone();
    long offset = dateTimeZone.getOffset(time);
    if (timeZone.getOffset(time) == offset) {
        return timeZone;
    }
    String[] ids = TimeZone.getAvailableIDs((int) offset);
    Log.d(TZ_ID_TAG, dateTimeZone.getID() + ": " + Arrays.toString(ids));
    for (String id : ids) {
        TimeZone tz = TimeZone.getTimeZone(id);
        if (tz.getOffset(time) == offset) {
            timeZone = tz;
            Log.d(TZ_ID_TAG, "Found time zone " + tz.getID() + " for " + dateTimeZone.getID() + " with offset: "
                    + offset);
            break;
        }
    }
    return timeZone;
}

From source file:com.cedarsoft.serialization.serializers.jackson.DateTimeZoneSerializer.java

License:Open Source License

@Override
public void serialize(@Nonnull JsonGenerator serializeTo, @Nonnull DateTimeZone object,
        @Nonnull Version formatVersion) throws IOException, JsonProcessingException {
    verifyVersionWritable(formatVersion);

    serializeTo.writeString(object.getID());
}

From source file:com.cedarsoft.serialization.serializers.stax.mate.ZoneInfoSerializer.java

License:Open Source License

@Override
public void serialize(@Nonnull DateTimeZone object, @Nonnull OutputStream out) throws IOException {
    out.write(object.getID().getBytes());
}

From source file:com.cenrise.test.azkaban.Utils.java

License:Apache License

/**
 * @param cronExpression: A cron expression is a string separated by white space, to provide a
 * parser and evaluator for Quartz cron expressions.
 * @return : org.quartz.CronExpression object.
 *
 * TODO: Currently, we have to transform Joda Timezone to Java Timezone due to CronExpression.
 * Since Java8 enhanced Time functionalities, We consider transform all Jodatime to Java Time in
 * future./*w w  w . j  av a  2 s  . c o  m*/
 */
public static CronExpression parseCronExpression(final String cronExpression, final DateTimeZone timezone) {
    if (cronExpression != null) {
        try {
            final CronExpression ce = new CronExpression(cronExpression);
            ce.setTimeZone(TimeZone.getTimeZone(timezone.getID()));
            return ce;
        } catch (final ParseException pe) {
            logger.error("this cron expression {" + cronExpression + "} can not be parsed. "
                    + "Please Check Quartz Cron Syntax.");
        }
        return null;
    } else {
        return null;
    }
}

From source file:com.enonic.cms.business.timezone.TimeZoneXmlCreator.java

License:Open Source License

private Element doCreateTimeZoneElement(DateTimeZone timeZone) {

    Element timeZoneEl = new Element("time-zone");
    timeZoneEl.setAttribute("ID", timeZone.getID());
    timeZoneEl.addContent(new Element("display-name").setText(timeZone.getID()));

    DateTime localTime = now.plus(timeZone.getOffsetFromLocal(now.getMillis()));
    Period offsetPeriod = new Period(now, localTime);
    //timeZoneEl.addContent( new Element( "hours-from-utc" ).setText( String.valueOf( offsetPeriod.getHours() ) ) );
    timeZoneEl.addContent(/*ww  w  . ja va 2 s  . c om*/
            new Element("hours-from-utc-as-human-readable").setText(getHoursAsHumanReadable(offsetPeriod)));

    return timeZoneEl;
}

From source file:com.facebook.presto.hive.HiveMetadataFactory.java

License:Apache License

public HiveMetadataFactory(HiveConnectorId connectorId, ExtendedHiveMetastore metastore,
        HdfsEnvironment hdfsEnvironment, HivePartitionManager partitionManager, DateTimeZone timeZone,
        int maxConcurrentFileRenames, boolean allowCorruptWritesForTesting, boolean respectTableFormat,
        boolean skipDeletionForAlter, boolean bucketWritingEnabled, HiveStorageFormat defaultStorageFormat,
        long perTransactionCacheMaximumSize, TypeManager typeManager, LocationService locationService,
        TableParameterCodec tableParameterCodec, JsonCodec<PartitionUpdate> partitionUpdateCodec,
        ExecutorService executorService, TypeTranslator typeTranslator, String prestoVersion) {
    this.connectorId = requireNonNull(connectorId, "connectorId is null").toString();

    this.allowCorruptWritesForTesting = allowCorruptWritesForTesting;
    this.respectTableFormat = respectTableFormat;
    this.skipDeletionForAlter = skipDeletionForAlter;
    this.bucketWritingEnabled = bucketWritingEnabled;
    this.defaultStorageFormat = requireNonNull(defaultStorageFormat, "defaultStorageFormat is null");
    this.perTransactionCacheMaximumSize = perTransactionCacheMaximumSize;

    this.metastore = requireNonNull(metastore, "metastore is null");
    this.hdfsEnvironment = requireNonNull(hdfsEnvironment, "hdfsEnvironment is null");
    this.partitionManager = requireNonNull(partitionManager, "partitionManager is null");
    this.timeZone = requireNonNull(timeZone, "timeZone is null");
    this.typeManager = requireNonNull(typeManager, "typeManager is null");
    this.locationService = requireNonNull(locationService, "locationService is null");
    this.tableParameterCodec = requireNonNull(tableParameterCodec, "tableParameterCodec is null");
    this.partitionUpdateCodec = requireNonNull(partitionUpdateCodec, "partitionUpdateCodec is null");
    this.typeTranslator = requireNonNull(typeTranslator, "typeTranslator is null");
    this.prestoVersion = requireNonNull(prestoVersion, "prestoVersion is null");

    if (!allowCorruptWritesForTesting && !timeZone.equals(DateTimeZone.getDefault())) {
        log.warn("Hive writes are disabled. "
                + "To write data to Hive, your JVM timezone must match the Hive storage timezone. "
                + "Add -Duser.timezone=%s to your JVM arguments", timeZone.getID());
    }//w w w  .ja v  a2 s .  co m

    renameExecution = new BoundedExecutor(executorService, maxConcurrentFileRenames);
}

From source file:com.facebook.util.TimeIntervalType.java

License:Apache License

/**
 * Validates that the specified interval value is valid for this 
 * interval type in the supplied time zone.
 * /*from ww  w  .  j av a 2s . co  m*/
 * @param timeZone the time zone
 * @param intervalLength the interval length
 */
public void validateValue(DateTimeZone timeZone, long intervalLength) {
    final DateTimeField field = fieldType.getField(TimeUtil.getChronology(timeZone.getID()));
    if (intervalLength < 1 || intervalLength > field.getMaximumValue()) {
        throw new IllegalArgumentException(
                "Supplied value " + intervalLength + " is out of bounds for " + name());
    }
}

From source file:com.getperka.flatpack.codexes.DateTimeZoneCodex.java

License:Apache License

@Override
public void writeNotNull(DateTimeZone object, SerializationContext context) throws IOException {
    context.getWriter().value(object.getID());
}

From source file:com.github.luuuis.myzone.preference.TimeZoneInfo.java

License:Apache License

/**
 * Creates a new TimeZoneInfo./*from   ww w . j  a  va2  s .c  om*/
 *
 * @param timezone a TimeZone
 */
private TimeZoneInfo(DateTimeZone timezone) {
    this.id = timezone.getID();
    this.timeZone = timezone;
}

From source file:com.google.ical.compat.jodatime.TimeZoneConverter.java

License:Apache License

/**
 * return a <code>java.util.Timezone</code> object that delegates to
 * the given Joda <code>DateTimeZone</code>.
 *///from   w ww .ja  va  2  s  . c om
public static TimeZone toTimeZone(final DateTimeZone dtz) {

    TimeZone tz = new TimeZone() {
        @Override
        public void setRawOffset(int n) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean useDaylightTime() {
            long firstTransition = MILLIS_SINCE_1_JAN_2000_UTC;
            return firstTransition != dtz.nextTransition(firstTransition);
        }

        @Override
        public boolean inDaylightTime(Date d) {
            long t = d.getTime();
            return dtz.getStandardOffset(t) != dtz.getOffset(t);
        }

        @Override
        public int getRawOffset() {
            return dtz.getStandardOffset(0);
        }

        @Override
        public int getOffset(long instant) {
            // This method is not abstract, but it normally calls through to the
            // method below.
            // It's optimized here since there's a direct equivalent in
            // DateTimeZone.
            // DateTimeZone and java.util.TimeZone use the same
            // epoch so there's no translation of instant required.
            return dtz.getOffset(instant);
        }

        @Override
        public int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds) {
            int millis = milliseconds; // milliseconds is day in standard time
            int hour = millis / MILLISECONDS_PER_HOUR;
            millis %= MILLISECONDS_PER_HOUR;
            int minute = millis / MILLISECONDS_PER_MINUTE;
            millis %= MILLISECONDS_PER_MINUTE;
            int second = millis / MILLISECONDS_PER_SECOND;
            millis %= MILLISECONDS_PER_SECOND;
            if (era == GregorianCalendar.BC) {
                year = -(year - 1);
            }

            // get the time in UTC in case a timezone has changed it's standard
            // offset, e.g. rid of a half hour from UTC.
            DateTime dt = null;
            try {
                dt = new DateTime(year, month + 1, day, hour, minute, second, millis, dtz);
            } catch (IllegalArgumentException ex) {
                // Java does not complain if you try to convert a Date that does not
                // exist due to the offset shifting forward, but Joda time does.
                // Since we're trying to preserve the semantics of TimeZone, shift
                // forward over the gap so that we're on a time that exists.
                // This assumes that the DST correction is one hour long or less.
                if (hour < 23) {
                    dt = new DateTime(year, month + 1, day, hour + 1, minute, second, millis, dtz);
                } else { // Some timezones shift at midnight.
                    Calendar c = new GregorianCalendar();
                    c.clear();
                    c.setTimeZone(TimeZone.getTimeZone("UTC"));
                    c.set(year, month, day, hour, minute, second);
                    c.add(Calendar.HOUR_OF_DAY, 1);
                    int year2 = c.get(Calendar.YEAR), month2 = c.get(Calendar.MONTH),
                            day2 = c.get(Calendar.DAY_OF_MONTH), hour2 = c.get(Calendar.HOUR_OF_DAY);
                    dt = new DateTime(year2, month2 + 1, day2, hour2, minute, second, millis, dtz);
                }
            }
            // since millis is in standard time, we construct the equivalent
            // GMT+xyz timezone and use that to convert.
            int offset = dtz.getStandardOffset(dt.getMillis());
            DateTime stdDt = new DateTime(year, month + 1, day, hour, minute, second, millis,
                    DateTimeZone.forOffsetMillis(offset));
            return getOffset(stdDt.getMillis());
        }

        @Override
        public String toString() {
            return dtz.toString();
        }

        @Override
        public boolean equals(Object that) {
            if (!(that instanceof TimeZone)) {
                return false;
            }
            TimeZone thatTz = (TimeZone) that;
            return getID().equals(thatTz.getID()) && hasSameRules(thatTz);
        }

        @Override
        public int hashCode() {
            return getID().hashCode();
        }

        private static final long serialVersionUID = 58752546800455L;
    };
    // Now fix the tzids.  DateTimeZone has a bad habit of returning
    // "+06:00" when it should be "GMT+06:00"
    String newTzid = cleanUpTzid(dtz.getID());
    tz.setID(newTzid);
    return tz;
}