Example usage for java.util TimeZone getOffset

List of usage examples for java.util TimeZone getOffset

Introduction

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

Prototype

public int getOffset(long date) 

Source Link

Document

Returns the offset of this time zone from UTC at the specified date.

Usage

From source file:Main.java

/**
 * This method returns the number of milliseconds (UTC time) for today's date at midnight in
 * the local time zone. For example, if you live in California and the day is September 20th,
 * 2016 and it is 6:30 PM, it will return 1474329600000. Now, if you plug this number into an
 * Epoch time converter, you may be confused that it tells you this time stamp represents 8:00
 * PM on September 19th local time, rather than September 20th. We're concerned with the GMT
 * date here though, which is correct, stating September 20th, 2016 at midnight.
 *
 * As another example, if you are in Hong Kong and the day is September 20th, 2016 and it is
 * 6:30 PM, this method will return 1474329600000. Again, if you plug this number into an Epoch
 * time converter, you won't get midnight for your local time zone. Just keep in mind that we
 * are just looking at the GMT date here.
 *
 * This method will ALWAYS return the date at midnight (in GMT time) for the time zone you
 * are currently in. In other words, the GMT date will always represent your date.
 *
 * Since UTC / GMT time are the standard for all time zones in the world, we use it to
 * normalize our dates that are stored in the database. When we extract values from the
 * database, we adjust for the current time zone using time zone offsets.
 *
 * @return The number of milliseconds (UTC / GMT) for today's date at midnight in the local
 * time zone/*from  w ww  .j a  v a2  s.co m*/
 */
public static long getNormalizedUtcDateForToday() {

    /*
     * This number represents the number of milliseconds that have elapsed since January
     * 1st, 1970 at midnight in the GMT time zone.
     */
    long utcNowMillis = System.currentTimeMillis();

    /*
     * This TimeZone represents the device's current time zone. It provides us with a means
     * of acquiring the offset for local time from a UTC time stamp.
     */
    TimeZone currentTimeZone = TimeZone.getDefault();

    /*
     * The getOffset method returns the number of milliseconds to add to UTC time to get the
     * elapsed time since the epoch for our current time zone. We pass the current UTC time
     * into this method so it can determine changes to account for daylight savings time.
     */
    long gmtOffsetMillis = currentTimeZone.getOffset(utcNowMillis);

    /*
     * UTC time is measured in milliseconds from January 1, 1970 at midnight from the GMT
     * time zone. Depending on your time zone, the time since January 1, 1970 at midnight (GMT)
     * will be greater or smaller. This variable represents the number of milliseconds since
     * January 1, 1970 (GMT) time.
     */
    long timeSinceEpochLocalTimeMillis = utcNowMillis + gmtOffsetMillis;

    /* This method simply converts milliseconds to days, disregarding any fractional days */
    long daysSinceEpochLocal = TimeUnit.MILLISECONDS.toDays(timeSinceEpochLocalTimeMillis);

    /*
     * Finally, we convert back to milliseconds. This time stamp represents today's date at
     * midnight in GMT time. We will need to account for local time zone offsets when
     * extracting this information from the database.
     */
    long normalizedUtcMidnightMillis = TimeUnit.DAYS.toMillis(daysSinceEpochLocal);

    return normalizedUtcMidnightMillis;
}

From source file:org.ow2.aspirerfid.queryandcapture.ui.MasterDataQueryAndCaptureGui.java

/**
 * Returns the time zone designator in a ISO6601-compliant format from the
 * given <code>Calendar</code> value.
 * //from w w w.j av  a  2s. c  om
 * @param cal
 *            The Calendar to be formatted.
 * @return The time zone designator from the given Calendar.
 */
private static String getTimeZone(final Calendar cal) {
    StringBuilder buf = new StringBuilder();
    TimeZone tz = cal.getTimeZone();
    // determine offset of timezone from UTC (incl. daylight saving)
    int offset = tz.getOffset(cal.getTimeInMillis());
    int hours = Math.abs((offset / (60 * 1000)) / 60);
    int minutes = Math.abs((offset / (60 * 1000)) % 60);
    buf.append(offset < 0 ? '-' : '+');
    buf.append(XX_FORMAT.format(hours));
    buf.append(':');
    buf.append(XX_FORMAT.format(minutes));
    return buf.toString();
}

From source file:com.jdo.CloudContactUtils.java

private static float getOffset(String timeId) {

    try {/*from   w w w  . j  a v  a 2 s  .  c om*/
        //Asia/Calcutta
        // String[] allTimeZones = TimeZone.getTimeZone(timeId);
        Date now = new Date();
        // for (int i = 0; i < allTimeZones.length; i++) {
        //System.out.println(allTimeZones[i]);
        TimeZone tz = TimeZone.getTimeZone(timeId);
        return (float) (tz.getOffset(now.getTime()) / 3600000.0);
        //}
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return 0;

}

From source file:com.arm.connector.bridge.core.Utils.java

public static int getUTCOffset() {
    TimeZone tz = TimeZone.getDefault();
    Calendar cal = GregorianCalendar.getInstance(tz);
    return tz.getOffset(cal.getTimeInMillis());
}

From source file:com.apptentive.android.sdk.util.Util.java

public static int getUtcOffset() {
    TimeZone timezone = TimeZone.getDefault();
    return timezone.getOffset(System.currentTimeMillis()) / 1000;
}

From source file:com.haulmont.cuba.core.global.TimeZones.java

/**
 * @return string representing the offset of the given time zone from GMT
 *///w  w w  .  j a  v  a 2s  . co m
public String getDisplayOffset(@Nullable TimeZone timeZone) {
    if (timeZone == null)
        return "";

    int offsetSec = timeZone.getOffset(timeSource.currentTimeMillis()) / 1000;
    int offsetHours = offsetSec / 3600;
    int offsetMins = (offsetSec % 3600) / 60;
    String str = StringUtils.leftPad(String.valueOf(Math.abs(offsetHours)), 2, '0') + ":"
            + StringUtils.leftPad(String.valueOf(Math.abs(offsetMins)), 2, '0');
    String sign = offsetHours >= 0 ? "+" : "-";
    return "GMT" + sign + str;
}

From source file:org.mule.el.datetime.AbstractInstant.java

@Override
public Instant withTimeZone(String newTimezone) {
    TimeZone timeZone = TimeZone.getTimeZone(newTimezone);
    calendar.add(Calendar.MILLISECOND, -timeZone.getOffset(calendar.getTimeInMillis()));
    calendar.setTimeZone(timeZone);//from   www. j a va 2s . c  om
    return this;
}

From source file:com.mi.xserv.XservBase.java

protected int getTimeZoneOffset() {
    // GMT es. italia +1
    TimeZone timezone = TimeZone.getDefault();
    int seconds = timezone.getOffset(Calendar.ZONE_OFFSET) / 1000;
    double minutes = seconds / 60;
    double hours = minutes / 60;
    return (int) hours;
}

From source file:org.apache.hadoop.hive.ql.udf.generic.GenericUDFFromUtcTimestamp.java

@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
    Object o0 = arguments[0].get();
    if (o0 == null) {
        return null;
    }/* w  ww  .  j  a  va  2 s  . co  m*/
    Object o1 = arguments[1].get();
    if (o1 == null) {
        return null;
    }

    String tzStr = textConverter.convert(o1).toString();
    TimeZone timezone = TimeZone.getTimeZone(tzStr);

    Timestamp timestamp = ((TimestampWritable) timestampConverter.convert(o0)).getTimestamp();

    int offset = timezone.getOffset(timestamp.getTime());
    if (invert()) {
        offset = -offset;
    }
    return applyOffset(offset, timestamp);
}

From source file:io.ionic.links.IonicDeeplink.java

private int getTimeZoneOffset() {
    TimeZone tz = TimeZone.getDefault();
    return tz.getOffset(new Date().getTime()) / 1000 / 60;
}