Example usage for java.util GregorianCalendar setTimeZone

List of usage examples for java.util GregorianCalendar setTimeZone

Introduction

In this page you can find the example usage for java.util GregorianCalendar setTimeZone.

Prototype

@Override
    public void setTimeZone(TimeZone zone) 

Source Link

Usage

From source file:DateUtils.java

public static final String getDateTimeFromDate(Date dt, String tzString) {
    try {/*  ww w  .  jav a 2  s.co m*/
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(dt);
        gc.setTimeZone(TimeZone.getTimeZone(tzString));
        StringBuffer ret = new StringBuffer();
        ret.append(gc.get(Calendar.YEAR));
        ret.append("-");
        ret.append(gc.get(Calendar.MONTH) - 1);
        ret.append("-");
        ret.append(gc.get(Calendar.DATE));
        ret.append(" ");
        ret.append(gc.get(Calendar.HOUR));
        ret.append(":");
        ret.append(gc.get(Calendar.MINUTE));
        ret.append(" ");
        if (gc.get(Calendar.AM_PM) == 0) {
            ret.append("AM");
        } else {
            ret.append("PM");
        }
        return ret.toString();
    } catch (Exception e) {
        return "";
    }
}

From source file:com.athena.peacock.common.core.util.XMLGregorialCalendarUtil.java

/**
 * <pre>// w ww .  j a  v  a2 s.  c  o m
 * ?  GMT+09:00? UTC  .
 * </pre>
 * @param date 
 * @param tz 
 * @return ? 
 * @throws DatatypeConfigurationException 
 */
private static GregorianCalendar convertTimezone(Date date, TimeZone tz) throws DatatypeConfigurationException {
    Assert.notNull(date, "date must not be null.");
    Assert.notNull(tz, "tz must not be null.");

    GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("ROK"));
    cal.setTime(date);
    cal.setTimeZone(tz);
    return cal;
}

From source file:com.athena.peacock.common.core.util.XMLGregorialCalendarUtil.java

/**
 * <pre>// w  w  w .j  ava 2 s  .co  m
 * YYYY-MM-DDTHH:MI:SS.SSS+09:00 ? ?(:2007-02-13T10:25:05.986+07:00)  ? GregorianCalendar ?.
 * </pre>
 * 
 * @param stringTypeDate YYYY-MM-DDTHH:MI:SS.SSS+09:00 ? ?(:2007-02-13T10:25:05.986+07:00)
 * @return  ? GregorianCalendar
 */
private static GregorianCalendar gmtStringToGregorianCalendar(String stringTypeDate)
        throws DatatypeConfigurationException {
    String yyyy = stringTypeDate.substring(0, 4);
    String mm = stringTypeDate.substring(5, 7);
    String dd = stringTypeDate.substring(8, 10);

    String hh = "00";
    String mi = "00";
    String ss = "00";
    String ms = "00";
    String tz = null;

    if (stringTypeDate.length() > 23) {
        hh = stringTypeDate.substring(11, 13);
        mi = stringTypeDate.substring(14, 16);
        ss = stringTypeDate.substring(17, 19);
        ms = stringTypeDate.substring(20, 23);
        tz = stringTypeDate.substring(23);
    } else {
        tz = stringTypeDate.substring(10);
        //tz = "+09:00";
    }

    if (tz.equals("Z")) {
        tz = "UTC";
    } else {
        tz = "GMT" + tz;
    }

    int iyyyy = Integer.parseInt(yyyy);
    int imm = Integer.parseInt(mm) - 1;
    int idd = Integer.parseInt(dd);
    int ihh = Integer.parseInt(hh);
    int imi = Integer.parseInt(mi);
    int iss = Integer.parseInt(ss);
    int ims = Integer.parseInt(ms);

    Calendar c = Calendar.getInstance();
    c.setTimeZone(TimeZone.getTimeZone(tz));
    c.set(iyyyy, imm, idd, ihh, imi, iss);
    c.set(Calendar.MILLISECOND, ims);

    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(c.getTime());
    cal.setTimeZone(TimeZone.getTimeZone("ROK"));

    return cal;
}

From source file:org.onosproject.drivers.bti.Bti7000SnmpAlarmConsumer.java

/**
 * Converts an SNMP string representation into a {@link Date} object,
 * and applies time zone conversion to provide the time on the local machine, ie PSM server.
 *
 * @param actAlarmDateAndTime MIB-II DateAndTime formatted. May optionally contain
 *                            a timezone offset in 3 extra bytes
 * @param sysInfoTimeZone     Must be supplied if actAlarmDateAndTime is just local time (with no timezone)
 * @param swVersion           Must be supplied if actAlarmDateAndTime is just local time (with no timezone)
 * @return adjusted {@link Date} or a simple conversion if other fields are null.
 *//*from  w ww .  ja va  2s. c o m*/
public static Date getLocalDateAndTime(String actAlarmDateAndTime, String sysInfoTimeZone, String swVersion) {
    if (StringUtils.isBlank(actAlarmDateAndTime)) {
        return null;
    }

    GregorianCalendar decodedDateAndTimeCal = btiMakeCalendar(OctetString.fromHexString(actAlarmDateAndTime));
    if ((sysInfoTimeZone == null) || (swVersion == null)) {
        return decodedDateAndTimeCal.getTime();
    }

    TimeZone javaTimeZone = getTimeZone();
    decodedDateAndTimeCal.setTimeZone(javaTimeZone);

    GregorianCalendar localTime = new GregorianCalendar();
    localTime.setTimeInMillis(decodedDateAndTimeCal.getTimeInMillis());

    return localTime.getTime();
}

From source file:eu.openanalytics.rsb.Util.java

/**
 * Converts a {@link GregorianCalendar} into a {@link XMLGregorianCalendar}
 * /*from   w  w  w .  j a  v  a  2 s  . c om*/
 * @param calendar
 * @return
 */
public static XMLGregorianCalendar convertToXmlDate(final GregorianCalendar calendar) {
    final GregorianCalendar zuluDate = new GregorianCalendar();
    zuluDate.setTimeZone(TimeZone.getTimeZone("UTC"));
    zuluDate.setTimeInMillis(calendar.getTimeInMillis());

    final XMLGregorianCalendar xmlDate = XML_DATATYPE_FACTORY.newXMLGregorianCalendar(zuluDate);
    return xmlDate;
}

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

public static XMLGregorianCalendar epochToXmlGC(long epoch) {
    try {/* w  w w .  java2s.c o  m*/
        DatatypeFactory dtf = DatatypeFactory.newInstance();
        GregorianCalendar gcal = new GregorianCalendar();
        gcal.setTimeInMillis(epoch);
        gcal.setTimeZone(TimeZone.getTimeZone("Z"));
        XMLGregorianCalendar xgc = dtf.newXMLGregorianCalendar(gcal);
        return xgc;
    } catch (Exception e) {
        // Do nothing!!! - return a null;
    }
    return null;
}

From source file:org.energyos.espi.datacustodian.utils.factories.EspiFactory.java

public static UsagePoint newUsagePoint(RetailCustomer retailCustomer) {
    UsagePoint usagePoint = newSimpleUsagePoint();

    usagePoint.setRetailCustomer(retailCustomer);
    usagePoint.addMeterReading(newMeterReading());
    usagePoint.addElectricPowerUsageSummary(newElectricPowerUsageSummary());
    usagePoint.addElectricPowerQualitySummary(newElectricPowerQualitySummary());
    usagePoint.setLocalTimeParameters(newLocalTimeParameters());

    usagePoint.getRelatedLinks().add(new LinkType("related", usagePoint.getSelfHref() + "/MeterReading"));
    usagePoint.getRelatedLinks()/* w ww  .j a v a  2 s .  com*/
            .add(new LinkType("related", usagePoint.getSelfHref() + "/ElectricPowerUsageSummary"));
    usagePoint.getRelatedLinks()
            .add(new LinkType("related", usagePoint.getSelfHref() + "/ElectricPowerQualitySummary"));

    GregorianCalendar published = new GregorianCalendar(2012, Calendar.NOVEMBER, 15, 0, 0, 0);
    published.setTimeZone(TimeZone.getTimeZone("UTC"));
    usagePoint.setPublished(published);

    GregorianCalendar updated = new GregorianCalendar(2012, Calendar.OCTOBER, 24, 0, 0, 0);
    updated.setTimeZone(TimeZone.getTimeZone("UTC"));
    usagePoint.setUpdated(updated);

    return usagePoint;
}

From source file:org.energyos.espi.common.test.EspiFactory.java

public static UsagePoint newUsagePoint(RetailCustomer retailCustomer) {
    UsagePoint usagePoint = newSimpleUsagePoint();

    usagePoint.setRetailCustomer(retailCustomer);
    usagePoint.addMeterReading(newMeterReading());
    usagePoint.addElectricPowerUsageSummary(newElectricPowerUsageSummary());
    usagePoint.addElectricPowerQualitySummary(newElectricPowerQualitySummary());
    usagePoint.setLocalTimeParameters(newLocalTimeParameters());

    usagePoint.getRelatedLinks().add(new LinkType("related", usagePoint.getSelfHref() + "/MeterReading"));
    usagePoint.getRelatedLinks()/* ww w  . j av a  2 s. c  om*/
            .add(new LinkType("related", usagePoint.getSelfHref() + "/ElectricPowerUsageSummary"));
    usagePoint.getRelatedLinks()
            .add(new LinkType("related", usagePoint.getSelfHref() + "/ElectricPowerQualitySummary"));
    usagePoint.setSelfLink(new LinkType("self", usagePoint.getSelfHref()));
    usagePoint.setUpLink(new LinkType("up", usagePoint.getUpHref()));

    GregorianCalendar published = new GregorianCalendar(2012, Calendar.NOVEMBER, 15, 0, 0, 0);
    published.setTimeZone(TimeZone.getTimeZone("UTC"));
    usagePoint.setPublished(published);

    GregorianCalendar updated = new GregorianCalendar(2012, Calendar.OCTOBER, 24, 0, 0, 0);
    updated.setTimeZone(TimeZone.getTimeZone("UTC"));
    usagePoint.setUpdated(updated);

    return usagePoint;
}

From source file:org.apache.lens.regression.util.Util.java

public static XMLGregorianCalendar getGregorianCalendar(String date, String format)
        throws ParseException, DatatypeConfigurationException {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(new SimpleDateFormat(format).parse(date));
    cal.setTimeZone(TimeZone.getTimeZone("UTC"));
    XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
    return calendar;
}

From source file:org.getobjects.appserver.core.WOMessage.java

public static Date dateFromHttpFormattedString(final String _httpDate) {
    int dayOfMonth = Integer.parseInt(_httpDate.substring(5, 7));
    int year = Integer.parseInt(_httpDate.substring(12, 16));
    int hourOfDay = Integer.parseInt(_httpDate.substring(17, 19));
    int minute = Integer.parseInt(_httpDate.substring(20, 22));
    int second = Integer.parseInt(_httpDate.substring(23, 25));

    int monthOfYear;
    String monthName = _httpDate.substring(8, 11);
    for (monthOfYear = 0; monthOfYear < 12; monthOfYear++) {
        if (httpMonthNames[monthOfYear].equals(monthName))
            break;
    }/* ww w . j  a  v  a2s .c o m*/
    String tzName = _httpDate.substring(26);
    TimeZone tz = tzName.equals("GMT") ? gmt : TimeZone.getTimeZone(tzName);

    GregorianCalendar cal = new GregorianCalendar();
    cal.set(year, monthOfYear, dayOfMonth, hourOfDay, minute, second);
    cal.setTimeZone(tz);
    return cal.getTime();
}