Example usage for java.util TimeZone getRawOffset

List of usage examples for java.util TimeZone getRawOffset

Introduction

In this page you can find the example usage for java.util TimeZone getRawOffset.

Prototype

public abstract int getRawOffset();

Source Link

Document

Returns the amount of time in milliseconds to add to UTC to get standard time in this time zone.

Usage

From source file:com.mycompany.craftdemo.utility.java

public static boolean isValidTime() {
    Calendar calendar = Calendar.getInstance();
    TimeZone fromTimeZone = calendar.getTimeZone();
    TimeZone toTimeZone = TimeZone.getTimeZone("EST");

    calendar.setTimeZone(fromTimeZone);// w  w w.  j  ava  2 s . c o  m
    calendar.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1);
    if (fromTimeZone.inDaylightTime(calendar.getTime())) {
        calendar.add(Calendar.MILLISECOND, calendar.getTimeZone().getDSTSavings() * -1);
    }

    calendar.add(Calendar.MILLISECOND, toTimeZone.getRawOffset());
    if (toTimeZone.inDaylightTime(calendar.getTime())) {
        calendar.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings());
    }

    int hour = calendar.get(Calendar.HOUR_OF_DAY);

    return hour >= 9 && hour <= 16;
}

From source file:at.newsagg.utils.ParserUtils.java

private static int localTimeDiff(TimeZone tz, Date date) {
    if (tz.inDaylightTime(date)) {
        int dstSavings = 0;
        if (tz.useDaylightTime()) {
            dstSavings = 3600000; // shortcut, JDK 1.4 allows cleaner impl
        }//from  w w  w. j  ava2 s .  com
        return tz.getRawOffset() + dstSavings;
    }
    return tz.getRawOffset();
}

From source file:com.androidinspain.deskclock.Utils.java

/**
 * Returns string denoting the timezone hour offset (e.g. GMT -8:00)
 *
 * @param useShortForm Whether to return a short form of the header that rounds to the
 *                     nearest hour and excludes the "GMT" prefix
 *//*from  w w w  .  j a  va  2 s . co  m*/
public static String getGMTHourOffset(TimeZone timezone, boolean useShortForm) {
    final int gmtOffset = timezone.getRawOffset();
    final long hour = gmtOffset / DateUtils.HOUR_IN_MILLIS;
    final long min = (Math.abs(gmtOffset) % DateUtils.HOUR_IN_MILLIS) / DateUtils.MINUTE_IN_MILLIS;

    if (useShortForm) {
        return String.format(Locale.ENGLISH, "%+d", hour);
    } else {
        return String.format(Locale.ENGLISH, "GMT %+d:%02d", hour, min);
    }
}

From source file:com.android.deskclock.Utils.java

/**
 * Returns string denoting the timezone hour offset (e.g. GMT -8:00)
 * @param useShortForm Whether to return a short form of the header that rounds to the
 *                     nearest hour and excludes the "GMT" prefix
 *//*from  w  w  w.jav  a  2s  .c om*/
public static String getGMTHourOffset(TimeZone timezone, boolean useShortForm) {
    final int gmtOffset = timezone.getRawOffset();
    final long hour = gmtOffset / DateUtils.HOUR_IN_MILLIS;
    final long min = (Math.abs(gmtOffset) % DateUtils.HOUR_IN_MILLIS) / DateUtils.MINUTE_IN_MILLIS;

    if (useShortForm) {
        return String.format("%+d", hour);
    } else {
        return String.format("GMT %+d:%02d", hour, min);
    }
}

From source file:net.sf.jhylafax.fax.HylaFAXClientHelper.java

public static String calculateTime(Date sendTime, String timeZoneID) {
    if (sendTime == null) {
        return "NOW";
    } else {//from  w ww. j  a  v a 2s.  co m
        long date = sendTime.getTime();

        TimeZone tz = TimeZone.getTimeZone(timeZoneID);
        //         tz.setStartRule(Calendar.MARCH, -1, Calendar.SUNDAY,  2*60*60*1000);
        //         tz.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,  2*60*60*1000);

        date -= tz.getRawOffset();
        if (tz.inDaylightTime(sendTime)) {
            date -= 3600 * 1000;
        }

        return new SimpleDateFormat("yyyyMMddHHmm").format(new Date(date));
    }
}

From source file:com.synelixis.xifi.AuthWebClient.AToken.java

private Date getGMTime() {
    Calendar c = Calendar.getInstance();
    System.out.println("current: " + c.getTime());

    TimeZone z = c.getTimeZone();
    int offset = z.getRawOffset();
    if (z.inDaylightTime(new Date())) {
        offset = offset + z.getDSTSavings();
    }/*from  ww  w  .j av a2s. c o m*/
    int offsetHrs = offset / 1000 / 60 / 60;
    int offsetMins = offset / 1000 / 60 % 60;
    c.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
    c.add(Calendar.MINUTE, (-offsetMins));
    return c.getTime();

}

From source file:ca.uhn.hl7v2.model.tests.DataTypeUtilTest.java

@Test
public void testGetLocalGMTOffset() {
    TimeZone tz = TimeZone.getDefault();
    int offset = tz.getRawOffset();

    int hours = offset / (60 * 60 * 1000);
    int minutes = (offset - (hours * (60 * 60 * 1000))) / (60 * 1000);
    int gmtOffset = hours * 100 + minutes;

    assertEquals("GMT offset", gmtOffset, DataTypeUtil.getLocalGMTOffset());
}

From source file:org.apache.synapse.mediators.deprecation.DeprecationMediator.java

private boolean isDeprecated(SynapseObject service) {

    try {// ww  w . j  a  v a2 s .  c om
        if (service.getBoolean("enabled").booleanValue()) {
            Calendar current = Calendar.getInstance();
            TimeZone tz = current.getTimeZone();
            int offset = tz.getRawOffset();
            Calendar calendar = new GregorianCalendar(tz);

            DateFormat df = new SimpleDateFormat("d/M/y:H:m");
            df.setTimeZone(tz);
            Date d1 = service.getDate(DeprecationConstants.CFG_DEPRECATION_FROM_DATE);
            Calendar fromCalendar = new GregorianCalendar(tz);
            d1.setTime(d1.getTime() + offset);
            fromCalendar.setTime(d1);
            String toDate = service.getDate(DeprecationConstants.CFG_DEPRECATION_TO_DATE).toString();
            if (toDate == null || (toDate.length() == 0)) {
                return calendar.before(fromCalendar);
            }

            Date d2 = service.getDate("toDate");
            Calendar toCalendar = new GregorianCalendar(tz);
            d2.setTime(d2.getTime() + offset);
            toCalendar.setTime(d2);
            return (calendar.after(fromCalendar) && calendar.before(toCalendar));
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return false;
}

From source file:com.couchbase.trombi.services.GeoService.java

@Cacheable("cities")
public CityInfo findCity(String cityName, String countryName) {
    GeoApiContext context = new GeoApiContext().setApiKey(geoApiKey);
    GeocodingResult[] results = GeocodingApi.newRequest(context)
            .components(ComponentFilter.locality(cityName), ComponentFilter.country(countryName))
            .awaitIgnoreError();//from  ww  w. j  ava 2  s. co m

    LatLng location = results[0].geometry.location;
    String stdCity = cityName;
    String stdCountry = countryName;
    for (AddressComponent ac : results[0].addressComponents) {
        for (AddressComponentType acType : ac.types) {
            if (acType == AddressComponentType.COUNTRY) {
                stdCountry = ac.longName;
            } else if (acType == AddressComponentType.LOCALITY) {
                stdCity = ac.longName;
            }
        }
    }

    TimeZone tz = TimeZoneApi.getTimeZone(context, location).awaitIgnoreError();

    return new CityInfo(stdCity, stdCountry, new Point(location.lat, location.lng),
            (int) TimeUnit.MILLISECONDS.toHours(tz.getRawOffset()));
}

From source file:org.sipfoundry.sipxconfig.device.DeviceTimeZone.java

public void setTimeZone(TimeZone tz) {
    String tzn = tz.getID();/*from  w ww.j  a v a2  s  .c o  m*/
    m_useDaylight = tz.useDaylightTime();
    m_offset = tz.getRawOffset() / (int) DateUtils.MILLIS_PER_MINUTE;

    m_dstSavings = tz.getDSTSavings() / (int) DateUtils.MILLIS_PER_MINUTE;

    // Until there is a setting for DST rule, it must be guessed here based on the timezone
    // name
    //
    // XCF-977 - Doing string compares on ID is not predicable! On my gentoo system, I get
    // US/Eastern but on a FC4 machine I get America/New_York. Both running java1.5.0.06
    // We'll wait for XCF-874 to address this properly. Until then, do not break whatever
    // seems to work for some systems in Europe, but default everyone else to US. Otherwise
    // the default of zero is not very helpful
    // See also: http://en.wikipedia.org/wiki/List_of_zoneinfo_time_zones
    if (tzn.matches("^Europe/.*")) {
        setDstParameters(DST_EU);
    } else {
        setDstParameters(DST_US);
    }
}