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.hp.hpl.jena.sparql.util.Utils.java

private static String calcTimezone(Calendar cal) {
    Date date = cal.getTime();//from   w w w . j a  v  a  2  s  .co  m
    TimeZone z = cal.getTimeZone();
    int tz = z.getRawOffset();

    if (z.inDaylightTime(date)) {
        int tzDst = z.getDSTSavings();
        tz = tz + tzDst;
    }

    String sign = "+";
    if (tz < 0) {
        sign = "-";
        tz = -tz;
    }

    int tzH = tz / (60 * 60 * 1000); // Integer divide towards zero.
    int tzM = (tz - tzH * 60 * 60 * 1000) / (60 * 1000);

    String tzH_str = Integer.toString(tzH);
    String tzM_str = Integer.toString(tzM);

    if (tzH < 10)
        tzH_str = "0" + tzH_str;
    if (tzM < 10)
        tzM_str = "0" + tzM_str;
    return sign + tzH_str + ":" + tzM_str;
}

From source file:com.aerospike.load.Utils.java

/**
 * Parse command line parameters.//from  ww  w  .  j  a v a2s.  c  om
 * @param cl Commandline arguments
 * @return Parameters
 * @throws Exception
 */
protected static Parameters parseParameters(CommandLine cl) {

    String host = cl.getOptionValue("h", "127.0.0.1");
    String portString = cl.getOptionValue("p", "3000");
    int port = Integer.parseInt(portString);
    String namespace = cl.getOptionValue("n", "test");
    String set = cl.getOptionValue("s", null);

    String timeToLive = cl.getOptionValue("et", "-1");
    int ttl = (Integer.parseInt(timeToLive));

    String timeout = cl.getOptionValue("tt", "0");
    int to = (Integer.parseInt(timeout));

    //Get timezone offset
    //get user input for timeZone and check for valid ID
    if (cl.hasOption("T")) {
        if (!Utils.checkTimeZoneID(cl))
            log.error("TimeZone given is not a valid ID");
    }
    String timeZone = cl.getOptionValue("T", TimeZone.getDefault().getID());
    TimeZone source = TimeZone.getTimeZone(timeZone);
    TimeZone local = TimeZone.getDefault();

    long timeZoneOffset = local.getRawOffset() - source.getRawOffset();

    String errorCount = cl.getOptionValue("ec", "0");
    int abortErrorCount = Integer.parseInt(errorCount);

    String writeAction = cl.getOptionValue("wa", "UPDATE");
    WritePolicy writePolicy = new WritePolicy();
    writePolicy.recordExistsAction = RecordExistsAction.valueOf(writeAction.toUpperCase());
    writePolicy.timeout = to;

    char delimiter = ',';
    boolean ignoreFirstLine = true;

    boolean verbose = false;
    if (cl.hasOption("v")) {
        verbose = true;
    }

    return new Parameters(host, port, namespace, set, ttl, Constants.CSV_FILE, delimiter, timeZoneOffset,
            ignoreFirstLine, verbose, abortErrorCount, writePolicy);
}

From source file:es.tekniker.framework.ktek.util.Utils.java

private static Calendar getCalendarUTC() {
    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  w  ww . ja va2 s .  co  m*/
    int offsetHrs = offset / 1000 / 60 / 60;
    int offsetMins = offset / 1000 / 60 % 60;

    System.out.println("offsetHrs: " + offsetHrs);
    System.out.println("offsetMins: " + offsetMins);
    c.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
    c.add(Calendar.MINUTE, (-offsetMins));

    System.out.println("GMT Time: " + c.getTime());

    return c;
}

From source file:es.tekniker.framework.ktek.util.Utils.java

private static long getTimeinMillis4TimeUTC(int hours, int minutes, int seconds) {
    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  w  ww  .  j a v a 2 s .  c  o m*/
    int offsetHrs = offset / 1000 / 60 / 60;
    int offsetMins = offset / 1000 / 60 % 60;

    System.out.println("offsetHrs: " + offsetHrs);
    System.out.println("offsetMins: " + offsetMins);

    c.set(Calendar.HOUR_OF_DAY, (hours - offsetHrs));
    c.set(Calendar.MINUTE, (minutes - offsetMins));
    c.set(Calendar.SECOND, seconds);

    System.out.println("GMT Time: " + c.getTime());

    long timeinmillis = c.getTimeInMillis();
    System.out.println(" system time in millis " + timeinmillis);

    return timeinmillis;
}

From source file:es.tekniker.framework.ktek.util.Utils.java

private static long getTimeinMillis4FullDateTimeUTC(int year, int month, int day, int hours, int minutes,
        int seconds) {
    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  w w  w. j av  a 2 s.  c o  m*/
    int offsetHrs = offset / 1000 / 60 / 60;
    int offsetMins = offset / 1000 / 60 % 60;

    System.out.println("offsetHrs: " + offsetHrs);
    System.out.println("offsetMin: " + offsetMins);

    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, day);

    c.set(Calendar.HOUR_OF_DAY, (hours - offsetHrs));
    c.set(Calendar.MINUTE, (minutes - offsetMins));
    c.set(Calendar.SECOND, seconds);

    System.out.println("GMT Time: " + c.getTime());

    long timeinmillis = c.getTimeInMillis();
    System.out.println(" system time in millis " + timeinmillis);

    return timeinmillis;
}

From source file:io.github.sparta.helpers.date.DateUtil.java

/**
 * convert server date to new date with user Locale.
 * /*  w w  w .  j a  v a2  s . com*/
 * @param userTimeZone
 *            user TimeZone id
 * @param serverDate
 *            date in server's Local
 * @return serverDate data in user's Local
 */
public static Date convertToUserDate(String userTimeZone, Date serverDate) {
    TimeZone userLocal = TimeZone.getTimeZone(userTimeZone);
    int rawOffset = userLocal.getRawOffset() - TimeZone.getDefault().getRawOffset();
    return new Date(serverDate.getTime() + rawOffset);
}

From source file:io.github.sparta.helpers.date.DateUtil.java

/**
 * Get time zones./*from  ww w  .j  a v a  2  s . co  m*/
 * 
 * @return map typezone id and GMT
 */
public static Map<String, String> getFilteredTimeZoneMap() {
    if (timezoneIDMap == null) {
        timezoneIDMap = new LinkedHashMap<String, String>();
        String[] ids = TimeZone.getAvailableIDs();
        for (String id : ids) {
            TimeZone zone = TimeZone.getTimeZone(id);
            int offset = zone.getRawOffset();
            int offsetSecond = offset / CONSTANT_1000;
            int hour = offsetSecond / (CONSTANT_60 * CONSTANT_60);
            int minutes = (offsetSecond % (CONSTANT_60 * CONSTANT_60)) / CONSTANT_60;
            timezoneIDMap.put(TimeZone.getTimeZone(id).getDisplayName(),
                    String.format("(GMT%+d:%02d) %s", hour, minutes, id));
        }
    }
    return timezoneIDMap;
}

From source file:org.eclipse.hawkbit.ui.utils.SPDateTimeUtil.java

/**
 * Get browser time zone.//w w  w  .j  ava  2  s  .co  m
 *
 * @return TimeZone
 */
public static TimeZone getBrowserTimeZone() {
    final WebBrowser webBrowser = com.vaadin.server.Page.getCurrent().getWebBrowser();
    final String[] timeZones = TimeZone.getAvailableIDs(webBrowser.getRawTimezoneOffset());
    TimeZone tz = TimeZone.getDefault();
    for (final String string : timeZones) {
        final TimeZone t = TimeZone.getTimeZone(string);
        if (t.getRawOffset() == webBrowser.getRawTimezoneOffset()) {
            tz = t;
        }
    }
    return tz;
}

From source file:io.github.sparta.helpers.date.DateUtil.java

/**
 * convert user date to new date with server side Locale.
 * /*from  ww w.  j a va2  s. c om*/
 * @param userTimeZone
 *            user TimeZone id
 * @param userDate
 *            date in user's Local
 * @return serverDate data in server's Local
 */
public static Date convertToServerDate(String userTimeZone, Date userDate) {
    TimeZone userLocal = TimeZone.getTimeZone(userTimeZone);
    int rawOffset = TimeZone.getDefault().getRawOffset() - userLocal.getRawOffset();
    return new Date(userDate.getTime() + rawOffset);
}

From source file:com.ykun.commons.utils.commons.DateUtils.java

/**
 * ?GMT//  ww  w.  j ava  2s.c o m
 *
 * @param date     
 * @param timezone 
 * @return Date
 */
public static Date getDateBaseGMT(Date date, String timezone) {
    TimeZone srcZone = TimeZone.getTimeZone("GMT");
    TimeZone toZone = TimeZone.getTimeZone(timezone);
    Long targetTime = date.getTime() - srcZone.getRawOffset() + toZone.getRawOffset();
    return new Date(targetTime);
}