Example usage for java.util Calendar ZONE_OFFSET

List of usage examples for java.util Calendar ZONE_OFFSET

Introduction

In this page you can find the example usage for java.util Calendar ZONE_OFFSET.

Prototype

int ZONE_OFFSET

To view the source code for java.util Calendar ZONE_OFFSET.

Click Source Link

Document

Field number for get and set indicating the raw offset from GMT in milliseconds.

Usage

From source file:org.exoplatform.webui.utils.TimeConvertUtils.java

/**
 * Get current time GMT/Zulu or UTC,(zone time is 0+GMT)
 * @return Calendar // w  w  w . ja  v a2 s .  c  o  m
 */
public static Calendar getGreenwichMeanTime() {
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setLenient(false);
    int gmtoffset = calendar.get(Calendar.DST_OFFSET) + calendar.get(Calendar.ZONE_OFFSET);
    calendar.setTimeInMillis(System.currentTimeMillis() - gmtoffset);
    return calendar;
}

From source file:org.apache.axis2.databinding.utils.ConverterUtil.java

public static void appendTimeZone(Calendar calendar, StringBuffer dateString) {
    int timezoneOffSet = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
    int timezoneOffSetInMinits = timezoneOffSet / 60000;
    if (timezoneOffSetInMinits < 0) {
        dateString.append("-");
        timezoneOffSetInMinits = timezoneOffSetInMinits * -1;
    } else {//  www  .  jav  a  2  s. c o  m
        dateString.append("+");
    }
    int hours = timezoneOffSetInMinits / 60;
    int minits = timezoneOffSetInMinits % 60;

    if (hours < 10) {
        dateString.append("0");
    }
    dateString.append(hours).append(":");

    if (minits < 10) {
        dateString.append("0");
    }

    dateString.append(minits);
}

From source file:com.att.ajsc.csilogging.util.UtilLib.java

public static String getStartTimestamp(String epoch) {
    long stime = Long.parseLong((String) epoch);
    XmlCalendar cal = new XmlCalendar(new Date(stime));
    XMLGregorianCalendar initTime = null;
    try {/* w  w w .  j a v  a  2s  . c  o  m*/
        initTime = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal.get(Calendar.YEAR),
                cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY),
                cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND), cal.get(Calendar.MILLISECOND),
                Math.round(cal.get(Calendar.ZONE_OFFSET) / 1000 / 60));
    } catch (Exception ex) {
        initTime = null;
    }
    if (initTime == null)
        return null;
    else
        return initTime.toString();
}

From source file:com.krawler.common.timezone.Timezone.java

public static Date getGmtDate(Date userDate) throws ServiceException {
    Date cmpdate = new Date();
    //try{/*w w  w .j a va2  s.c om*/
    Calendar calInstance = Calendar.getInstance();

    int gmtoffset = calInstance.get(Calendar.DST_OFFSET) + calInstance.get(Calendar.ZONE_OFFSET);
    //SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy HH:mm a");
    //String cmp = sdf.format(userDate);

    cmpdate = new Date(userDate.getTime() - gmtoffset);

    /*} catch(ParseException e){
    throw ServiceException.FAILURE("Timezone.getGmtDate", e);
    }*/
    return cmpdate;
}

From source file:ca.uhn.hl7v2.model.primitive.CommonTSTest.java

/**
 * Test for http://sourceforge.net/support/tracker.php?aid=3410095
 */// w w  w  .  j  a v a 2 s . c o m
@Test
public void testSetCalendarUsingHighValueTimeZoneOffset() throws ParseException, DataTypeException {

    Calendar c = Calendar.getInstance();
    c.setTime(new SimpleDateFormat("yyyyMMdd HH:mm:ss Z").parse("20110102 12:00:00 -0000"));
    c.set(Calendar.ZONE_OFFSET, 12 * 60 * 60 * 1000);
    CommonTS commonTS = new CommonTS();
    commonTS.setValue(c);
    String val = commonTS.getValue();
    assertEquals("20110102070000+1200", val);
}

From source file:org.regenstrief.util.StopWatch.java

/**
 * Converts the given Calendar to an HL7 String (YYYY[MM[DD[HHMM[SS[.S[S[S[S]]]]]]]])
 * //from   www  .j  a  v  a2s .c  o  m
 * @param cal the Calendar
 * @param timeZone whether to include the time zone
 * @return the Calendar an an HL7 String
 **/
public static String toHL7String(final Calendar cal, final boolean timeZone) {
    long time;
    String value;

    if (cal == null) {
        return null;
    }

    final int day = cal.get(Calendar.DAY_OF_MONTH);
    final int hour = cal.get(Calendar.HOUR_OF_DAY);
    final int minute = cal.get(Calendar.MINUTE);
    final int second = cal.get(Calendar.SECOND);
    final int milli = cal.get(Calendar.MILLISECOND);
    time = cal.get(Calendar.YEAR);
    time = time * 100 + cal.get(Calendar.MONTH) + 1;
    final boolean isMilli = (milli > 0) || timeZone;
    final boolean isSecond = (second > 0) || isMilli;
    final boolean isMinute = (hour > 0) || (minute > 0) || isSecond;
    final boolean isDay = (day > 0) || isMinute;
    if (isDay) {
        time = time * 100 + day;
        if (isMinute) {
            time = time * 100 + hour;
            time = time * 100 + minute;
            if (isSecond) {
                time = time * 100 + second;
            }
        }
    }

    value = String.valueOf(time);

    if (isMilli) {
        value = value + '.' + Util.to3DigitString(milli);
    }

    if (timeZone) {
        //cal.getTimeZone().getOffset(date)
        //cal.getTimeZone().getRawOffset()
        final int off = cal.get(Calendar.ZONE_OFFSET); // Used by Java SIG
        final int tz = Math.abs(off);
        final int hours = tz / MILLIS_PER_HOUR;
        value = value + (off < 0 ? '-' : '+') + Util.to2DigitString(hours)
                + Util.to2DigitString((tz - (hours * MILLIS_PER_HOUR)) / MILLIS_PER_MINUTE);
    }

    return value;
}

From source file:org.yccheok.jstock.engine.GoogleStockHistoryServer.java

private boolean parse(String respond) {
    historyDatabase.clear();/*ww  w. j  a v  a  2 s  .c o m*/
    timestamps.clear();

    final long startTimeInMilli = this.duration.getStartDate().getTime().getTime();
    final long endTimeInMilli = this.duration.getEndDate().getTime().getTime();

    String[] stockDatas = respond.split("\r\n|\r|\n");

    double previousClosePrice = Double.MAX_VALUE;
    long time = 0;

    Symbol symbol = Symbol.newInstance(code.toString());
    String name = symbol.toString();
    Stock.Board board = Stock.Board.Unknown;
    Stock.Industry industry = Stock.Industry.Unknown;
    Calendar calendar = null;
    boolean initialized = false;

    long TIMEZONE_OFFSET = 0;

    for (String stockData : stockDatas) {
        // NYSE : TIMEZONE_OFFSET=-300

        if (stockData.isEmpty()) {
            continue;
        }

        char c = stockData.charAt(0);
        if (c != 'a' && false == Character.isDigit(c)) {
            if (stockData.startsWith("TIMEZONE_OFFSET")) {
                String[] fields = stockData.split("=");
                if (fields.length == 2) {
                    try {
                        TIMEZONE_OFFSET = Long.parseLong(fields[1]);
                    } catch (NumberFormatException ex) {
                        log.error(null, ex);
                    }
                }
            }
            continue;
        }

        String[] fields = stockData.split(",");

        // DATE,CLOSE,HIGH,LOW,OPEN,VOLUME
        if (fields.length < 6) {
            continue;
        }

        long currentTime = 0;

        final String fields0 = fields[0];
        if (fields0.charAt(0) == 'a') {
            if (fields0.length() > 1) {
                String timeStr = fields0.substring(1);
                try {
                    time = Long.parseLong(timeStr);
                } catch (NumberFormatException ex) {
                    log.error(null, ex);
                    continue;
                }

                currentTime = time;
            }
        } else {
            int index = 0;
            try {
                index = Integer.parseInt(fields0);
            } catch (NumberFormatException ex) {
                log.error(null, ex);
                continue;
            }

            currentTime = time + (index * 60 * 60 * 24);
        }

        if (initialized == false) {
            try {
                Stock stock = stockServer.getStock(googleCode);
                symbol = stock.symbol;
                name = stock.getName();
                board = stock.getBoard();
                industry = stock.getIndustry();
            } catch (StockNotFoundException exp) {
                log.error(null, exp);
            }

            calendar = Calendar.getInstance();

            initialized = true;
        }

        long currentTimeInMilli = currentTime * 1000;

        // Convert it to local time respect to stock exchange.
        // TIMEZONE_OFFSET is in minute.
        currentTimeInMilli = currentTimeInMilli + (TIMEZONE_OFFSET * 60 * 1000);

        // Remove time information, by resetting it to 00:00
        currentTimeInMilli = currentTimeInMilli / 1000 / 24 / 60 / 60;
        currentTimeInMilli = currentTimeInMilli * 60 * 60 * 24 * 1000;

        // Make it as local timestamp.
        //
        // For instance, Greenwich is 1:30pm right now.
        // We want to make Malaysia 1:30pm right now.
        //
        // That's why we are having -ve.
        calendar.setTimeInMillis(currentTimeInMilli);
        int offset = -(calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET));
        currentTimeInMilli = currentTimeInMilli + offset;

        if (currentTimeInMilli < startTimeInMilli) {
            continue;
        }
        if (currentTimeInMilli > endTimeInMilli) {
            break;
        }

        double closePrice = 0.0;
        double highPrice = 0.0;
        double lowPrice = 0.0;
        double prevPrice = 0.0;
        double openPrice = 0.0;
        // TODO: CRITICAL LONG BUG REVISED NEEDED.
        long volume = 0;
        //double adjustedClosePrice = 0.0;

        try {
            closePrice = Double.parseDouble(fields[1]);
            highPrice = Double.parseDouble(fields[2]);
            lowPrice = Double.parseDouble(fields[3]);
            openPrice = Double.parseDouble(fields[4]);
            prevPrice = (previousClosePrice == Double.MAX_VALUE) ? 0 : previousClosePrice;

            // TODO: CRITICAL LONG BUG REVISED NEEDED.
            volume = Long.parseLong(fields[5]);
            //adjustedClosePrice = Double.parseDouble(fields[6]);
        } catch (NumberFormatException exp) {
            log.error(null, exp);
        }

        double changePrice = (previousClosePrice == Double.MAX_VALUE) ? 0 : closePrice - previousClosePrice;
        double changePricePercentage = ((previousClosePrice == Double.MAX_VALUE) || (previousClosePrice == 0.0))
                ? 0
                : changePrice / previousClosePrice * 100.0;

        Stock stock = new Stock(code, symbol, name, board, industry, prevPrice, openPrice,
                closePrice, /* Last Price. */
                highPrice, lowPrice, volume, changePrice, changePricePercentage, 0, 0.0, 0, 0.0, 0, 0.0, 0, 0.0,
                0, 0.0, 0, 0.0, 0, currentTimeInMilli);

        historyDatabase.put(currentTimeInMilli, stock);

        // Something we do not understand Google server.
        //if (timestamps.isEmpty()) {
        timestamps.add(currentTimeInMilli);
        //} else {
        //    if (timestamps.get(timestamps.size() - 1) != currentTimeInMilli) {
        //        timestamps.add(currentTimeInMilli);
        //    }
        //}
        previousClosePrice = closePrice;
    }

    return (historyDatabase.size() > 0);
}

From source file:org.openbravo.erpCommon.utility.OBDateUtils.java

/**
 * Returns a new Date converted to UTC// www  .j a  v  a  2 s  .c o  m
 * 
 * @param date
 *          Date to be converted to UTC
 * 
 */
public static Date convertDateToUTC(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

    int gmtMillisecondOffset = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET));
    calendar.add(Calendar.MILLISECOND, -gmtMillisecondOffset);

    return calendar.getTime();
}

From source file:org.openbravo.client.kernel.reference.TimeUIDefinition.java

@Override
// Value is a date in local time format
public synchronized Object createFromClassicString(String value) {
    try {/*  w ww  .  j  a v  a2  s  .  c o m*/
        if (value == null || value.length() == 0 || value.equals("null")) {
            return null;
        }

        final Date localDate = getClassicFormat().parse(value);
        // If a date is not specified, 01-01-1970 will be set by default
        // Today's date should be returned
        Calendar now = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(localDate);
        calendar.set(Calendar.DATE, now.get(Calendar.DATE));
        calendar.set(Calendar.MONTH, now.get(Calendar.MONTH));
        calendar.set(Calendar.YEAR, now.get(Calendar.YEAR));

        // Applies the zone offset and the dst offset to convert the time from local to UTC
        int gmtMillisecondOffset = (now.get(Calendar.ZONE_OFFSET) + now.get(Calendar.DST_OFFSET));
        calendar.add(Calendar.MILLISECOND, -gmtMillisecondOffset);

        return xmlTimeFormat.format(calendar.getTime());
    } catch (Exception e) {
        throw new OBException("Exception when handling value " + value, e);
    }
}

From source file:org.kuali.rice.core.impl.datetime.DateTimeServiceImpl.java

public int dateDiff(Date startDate, Date endDate, boolean inclusive) {
    Calendar startDateCalendar = Calendar.getInstance();
    startDateCalendar.setTime(startDate);

    Calendar endDateCalendar = Calendar.getInstance();
    endDateCalendar.setTime(endDate);/*w w w  .  j a  va 2  s  . c  om*/

    int startDateOffset = -(startDateCalendar.get(Calendar.ZONE_OFFSET)
            + startDateCalendar.get(Calendar.DST_OFFSET)) / (60 * 1000);

    int endDateOffset = -(endDateCalendar.get(Calendar.ZONE_OFFSET) + endDateCalendar.get(Calendar.DST_OFFSET))
            / (60 * 1000);

    if (startDateOffset > endDateOffset) {
        startDateCalendar.add(Calendar.MINUTE, endDateOffset - startDateOffset);
    }

    if (inclusive) {
        startDateCalendar.add(Calendar.DATE, -1);
    }

    int dateDiff = Integer.parseInt(DurationFormatUtils.formatDuration(
            endDateCalendar.getTimeInMillis() - startDateCalendar.getTimeInMillis(), "d", true));

    return dateDiff;
}