Example usage for org.joda.time DateTimeZone forTimeZone

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

Introduction

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

Prototype

public static DateTimeZone forTimeZone(TimeZone zone) 

Source Link

Document

Gets a time zone instance for a JDK TimeZone.

Usage

From source file:com.landenlabs.all_devtool.ClockFragment.java

License:Open Source License

/**
 * Update clock on screen//w ww .j  a  v  a  2s.  com
 */
private void updateClock() {
    m_date.setTime(System.currentTimeMillis());
    m_timeZone = TimeZone.getDefault();
    s_timeZoneFormat.setTimeZone(m_timeZone);
    String localTmStr = s_timeZoneFormat.format(m_date);

    m_clockLocalTv.setText(localTmStr);
    String gmtTmStr = s_timeGmtFormat.format(m_date);
    m_clockGmtTv.setText(gmtTmStr);

    long currDay = TimeUnit.DAYS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    m_timePartsTv.setText(String.format("Days since Jan 1 1970: %d", currDay));

    Locale ourLocale = Locale.getDefault();
    StringBuilder tzStr1 = new StringBuilder();
    StringBuilder tzStr2 = new StringBuilder();

    tzStr1.append(String.format("Locale %s\n", ourLocale.getDisplayName()));

    tzStr1.append(getTzDetailStr(m_timeZone));
    tzStr1.append("Daylight Savings:\n");

    // tzStr.append((m_timeZone.useDaylightTime() ? "Has" : "No") + " daylight savings\n");
    String ds_short = m_timeZone.getDisplayName(false, TimeZone.SHORT, ourLocale);
    tzStr1.append(
            String.format("    %s=%s\n", ds_short, m_timeZone.getDisplayName(false, TimeZone.LONG, ourLocale)));
    if (m_timeZone.useDaylightTime()) {
        String std_short = m_timeZone.getDisplayName(true, TimeZone.SHORT, ourLocale);
        tzStr1.append(String.format("    %s=%s\n", std_short,
                m_timeZone.getDisplayName(true, TimeZone.LONG, ourLocale)));

        // ----
        // DateTimeZone zone1 = DateTimeZone.forID("Europe/London");
        // DateTimeZone zone2 = DateTimeZone.forID("America/New_York");

        DateTimeZone zone = DateTimeZone.forTimeZone(m_timeZone);
        DateTimeFormatter format = DateTimeFormat.mediumDateTime();

        long current = System.currentTimeMillis();
        for (int i = 0; i < 4; i++) {
            long next = zone.nextTransition(current);
            if (current == next) {
                break;
            }

            tzStr1.append(String.format("    %s %s\n",
                    zone.isStandardOffset(next - 3600000) ? std_short : ds_short, format.print(next)));
            current = next;
        }
        m_localeTv.setText(tzStr1.toString());

        String[] ids = TimeZone.getAvailableIDs();
        if (ids != null && ids.length > 0) {
            switch (m_daylightFilter) {
            case NoDS:
                tzStr2.append("TimeZones (no Daylight savings):\n");
                break;
            case HasDS:
                tzStr2.append("TimeZone (Has Daylight savings):\n");
                break;
            case InDS:
                tzStr2.append("TimeZone (In Daylight savings):\n");
                break;
            }

            SparseIntArray zones = new SparseIntArray();
            for (int tzIdx = 0; tzIdx < ids.length; tzIdx++) {
                TimeZone tz = TimeZone.getTimeZone(ids[tzIdx]);
                boolean addTz = false;
                switch (m_daylightFilter) {
                case NoDS:
                    addTz = !tz.useDaylightTime();
                    break;
                case HasDS:
                    addTz = tz.useDaylightTime() && !tz.inDaylightTime(m_date);
                    break;
                case InDS:
                    addTz = tz.inDaylightTime(m_date);
                    break;
                }
                if (addTz) {
                    zones.put(tz.getRawOffset(), tzIdx);
                }
            }

            for (int idx = 0; idx != zones.size(); idx++) {
                TimeZone tz = TimeZone.getTimeZone(ids[zones.valueAt(idx)]);
                tzStr2.append(getTzOffsetStr(tz, s_timeFormat));
            }
        }

    }
    m_dayLight.setText(tzStr2.toString());

    m_clockSysClkUpTm.setText("uptimeMillis:" + formatInterval(SystemClock.uptimeMillis()));
    m_clockSysClkReal.setText("elapsedRealtime:" + formatInterval(SystemClock.elapsedRealtime()));
}

From source file:com.linkedin.pinot.common.data.DateTimeFormatPatternSpec.java

License:Apache License

public DateTimeFormatPatternSpec(String timeFormat, String sdfPatternWithTz) {
    _timeFormat = TimeFormat.valueOf(timeFormat);
    if (_timeFormat.equals(TimeFormat.SIMPLE_DATE_FORMAT)) {
        Matcher m = SDF_PATTERN_WITH_TIMEZONE.matcher(sdfPatternWithTz);
        _sdfPattern = sdfPatternWithTz;/*  ww w.  j a  va  2 s .  com*/
        if (m.find()) {
            _sdfPattern = m.group(SDF_PATTERN_GROUP).trim();
            String timezoneString = m.group(TIMEZONE_GROUP).trim();
            _dateTimeZone = DateTimeZone.forTimeZone(TimeZone.getTimeZone(timezoneString));
        }
        _dateTimeFormatter = DateTimeFormat.forPattern(_sdfPattern).withZone(_dateTimeZone);
    }
}

From source file:com.metamx.druid.jackson.DefaultObjectMapper.java

License:Open Source License

public DefaultObjectMapper(JsonFactory factory) {
    super(factory);
    SimpleModule serializerModule = new SimpleModule("Druid default serializers", new Version(1, 0, 0, null));
    JodaStuff.register(serializerModule);
    serializerModule.addDeserializer(Granularity.class, new JsonDeserializer<Granularity>() {
        @Override/*from  w w  w.  j a v a 2  s.  c o m*/
        public Granularity deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
            return Granularity.valueOf(jp.getText().toUpperCase());
        }
    });
    serializerModule.addDeserializer(DateTimeZone.class, new JsonDeserializer<DateTimeZone>() {
        @Override
        public DateTimeZone deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
            String tzId = jp.getText();
            try {
                return DateTimeZone.forID(tzId);
            } catch (IllegalArgumentException e) {
                // also support Java timezone strings
                return DateTimeZone.forTimeZone(TimeZone.getTimeZone(tzId));
            }
        }
    });
    serializerModule.addSerializer(DateTimeZone.class, new JsonSerializer<DateTimeZone>() {
        @Override
        public void serialize(DateTimeZone dateTimeZone, JsonGenerator jsonGenerator,
                SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
            jsonGenerator.writeString(dateTimeZone.getID());
        }
    });
    serializerModule.addSerializer(Sequence.class, new JsonSerializer<Sequence>() {
        @Override
        public void serialize(Sequence value, final JsonGenerator jgen, SerializerProvider provider)
                throws IOException, JsonProcessingException {
            jgen.writeStartArray();
            value.accumulate(null, new Accumulator() {
                @Override
                public Object accumulate(Object o, Object o1) {
                    try {
                        jgen.writeObject(o1);
                    } catch (IOException e) {
                        throw Throwables.propagate(e);
                    }
                    return o;
                }
            });
            jgen.writeEndArray();
        }
    });
    serializerModule.addSerializer(ByteOrder.class, ToStringSerializer.instance);
    serializerModule.addDeserializer(ByteOrder.class, new JsonDeserializer<ByteOrder>() {
        @Override
        public ByteOrder deserialize(JsonParser jp, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
            if (ByteOrder.BIG_ENDIAN.toString().equals(jp.getText())) {
                return ByteOrder.BIG_ENDIAN;
            }
            return ByteOrder.LITTLE_ENDIAN;
        }
    });
    registerModule(serializerModule);

    configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    configure(SerializationConfig.Feature.AUTO_DETECT_GETTERS, false);
    configure(SerializationConfig.Feature.AUTO_DETECT_FIELDS, false);
    configure(SerializationConfig.Feature.INDENT_OUTPUT, false);
}

From source file:com.metinkale.prayerapp.utils.TimeZoneChangedReceiver.java

License:Apache License

@Override
public void onReceive(Context context, @NonNull Intent intent) {
    String tzId = intent.getStringExtra("time-zone");

    try {/*from  w w w . j a  v  a2s. c om*/
        DateTimeZone newDefault = DateTimeZone.forTimeZone(TimeZone.getDefault());
        DateTimeZone.setDefault(newDefault);
        Log.d("prayer-times-android",
                "TIMEZONE_CHANGED received, changed default timezone to \"" + tzId + "\"");
    } catch (IllegalArgumentException e) {
        Log.d("prayer-times-android", "Could not recognize timezone id \"" + tzId + "\"", e);
    }
}

From source file:com.microsoft.exchange.utils.TimeZoneHelper.java

License:Open Source License

/**
 * Converts the given Java time zone into into the corresponding EWS representation.
 *
 * @param tz time zone to convert// w w w .j  a  v  a 2  s  .  c  o m
 * @return the resulting SerializableTimeZone object
 */
public static SerializableTimeZone toSerializableTimeZone(TimeZone tz) {
    long now = DateTimeUtils.currentTimeMillis();
    DateTimeZone zone = DateTimeZone.forTimeZone(tz);
    int standardOffset = zone.getStandardOffset(now);
    SerializableTimeZone result = new SerializableTimeZone();
    result.setBias(toBias(standardOffset));
    // check if the zone has no transitions
    if (zone.isFixed()) {
        // fake transitions for fixed zones
        result.setStandardTime(FIXED_STANDARD_TIME);
        result.setDaylightTime(FIXED_DAYLIGHT_TIME);
    } else {
        // it is assumed that 2 subsequent transition will have both STD and DST
        long transition = setTransitionTime(zone, now, result);
        setTransitionTime(zone, transition, result);
    }
    return result;
}

From source file:com.moss.jodapersist.AnsiTimeOfDay.java

License:Open Source License

public void nullSafeSet(PreparedStatement statement, Object value, int index)
        throws HibernateException, SQLException {
    if (value == null) {
        statement.setTimestamp(index, null);
    } else {//from   www  .  j av a 2 s . co m
        TimeOfDay tmd = (TimeOfDay) value;

        long millisInZone = tmd.toDateTimeToday(DateTimeZone.forTimeZone(timeZone)).getMillis();
        Timestamp timestamp = new Timestamp(millisInZone);

        Time time = new Time(tmd.getHourOfDay(), tmd.getMinuteOfHour(), tmd.getSecondOfMinute());

        statement.setTime(index, time);
    }
}

From source file:com.moss.jodapersist.StringTimeOfDayUserType.java

License:Open Source License

public void nullSafeSet(PreparedStatement statement, Object value, int index)
        throws HibernateException, SQLException {

    if (value == null) {
        statement.setString(index, null);
    } else {/*w w  w  .  j a  v a2  s .c o m*/
        TimeOfDay tmd = (TimeOfDay) value;
        long millisInZone = tmd.toDateTimeToday(DateTimeZone.forTimeZone(timeZone)).getMillis();
        Timestamp timestamp = new Timestamp(millisInZone);

        statement.setString(index,
                formatter.format(tmd.toDateTimeToday(DateTimeZone.forTimeZone(super.timeZone)).toDate()));
    }
}

From source file:com.mycollab.core.utils.DateTimeUtils.java

License:Open Source License

public static String formatDate(Date date, String dateFormat, Locale locale, TimeZone timezone) {
    if (date == null) {
        return "";
    }/*from   w  ww .  j a va  2 s . co  m*/

    DateTimeFormatter formatter = DateTimeFormat.forPattern(dateFormat).withLocale(locale);
    if (timezone != null) {
        formatter = formatter.withZone(DateTimeZone.forTimeZone(timezone));
    }

    return formatter.print(new DateTime(date));
}

From source file:com.mycollab.core.utils.DateTimeUtils.java

License:Open Source License

public static Date convertDateTimeByTimezone(Date date, TimeZone timeZone) {
    DateTime dateTime = new DateTime(date);
    return dateTime.toDateTime(DateTimeZone.forTimeZone(timeZone)).toLocalDateTime().toDate();
}

From source file:com.mycollab.vaadin.UserUIContext.java

License:Open Source License

/**
 * @param date is the UTC date value// w ww.  j a v a 2 s. c o m
 * @return
 */
public static String formatDateTime(Date date) {
    if (date == null) {
        return "";
    } else {
        DateTime jodaDate = new DateTime(date)
                .toDateTime(DateTimeZone.forTimeZone(UserUIContext.getUserTimeZone()));
        if (jodaDate.getHourOfDay() > 0 || jodaDate.getMinuteOfHour() > 0) {
            DateTimeFormatter formatter = DateTimeFormat.forPattern(MyCollabUI.getDateTimeFormat())
                    .withLocale(UserUIContext.getUserLocale());
            return formatter.print(jodaDate);
        } else {
            DateTimeFormatter formatter = DateTimeFormat.forPattern(MyCollabUI.getDateFormat())
                    .withLocale(UserUIContext.getUserLocale());
            return formatter.print(jodaDate);
        }
    }
}