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.openmeetings.app.remote.InvitationService.java

/**
 * send an invitation to another user by Mail
 * /*from  ww  w .  ja  v  a  2s .c  om*/
 * @param SID
 * @param username
 * @param message
 * @param baseurl
 * @param email
 * @param subject
 * @param room_id
 * @param conferencedomain
 * @param isPasswordProtected
 * @param invitationpass
 * @param valid
 * @param validFromDate
 * @param validFromTime
 * @param validToDate
 * @param validToTime
 * @param language_id
  * @param jNameTimeZone
 * @return
 */
public String sendInvitationHash(String SID, String username, String message, String baseurl, String email,
        String subject, Long room_id, String conferencedomain, Boolean isPasswordProtected,
        String invitationpass, Integer valid, Date validFromDate, String validFromTime, Date validToDate,
        String validToTime, Long language_id, String jNameTimeZone) {

    try {
        log.debug("sendInvitationHash: ");

        Integer validFromHour = Integer.valueOf(validFromTime.substring(0, 2)).intValue();
        Integer validFromMinute = Integer.valueOf(validFromTime.substring(3, 5)).intValue();

        Integer validToHour = Integer.valueOf(validToTime.substring(0, 2)).intValue();
        Integer validToMinute = Integer.valueOf(validToTime.substring(3, 5)).intValue();

        log.info("validFromHour: " + validFromHour);
        log.info("validFromMinute: " + validFromMinute);

        Calendar calFrom = Calendar.getInstance();
        calFrom.setTime(validFromDate);
        calFrom.set(Calendar.HOUR_OF_DAY, validFromHour);
        calFrom.set(Calendar.MINUTE, validFromMinute);
        calFrom.set(Calendar.SECOND, 0);

        Calendar calTo = Calendar.getInstance();
        calTo.setTime(validToDate);
        calTo.set(Calendar.HOUR_OF_DAY, validToHour);
        calTo.set(Calendar.MINUTE, validToMinute);
        calTo.set(Calendar.SECOND, 0);

        Date dFrom = calFrom.getTime();
        Date dTo = calTo.getTime();

        Long users_id = sessionManagement.checkSession(SID);
        Long user_level = userManagement.getUserLevelByID(users_id);

        OmTimeZone omTimeZone = omTimeZoneDaoImpl.getOmTimeZone(jNameTimeZone);

        // If everything fails
        if (omTimeZone == null) {
            Configuration conf = cfgManagement.getConfKey(3L, "default.timezone");
            if (conf != null) {
                jNameTimeZone = conf.getConf_value();
            }
            omTimeZone = omTimeZoneDaoImpl.getOmTimeZone(jNameTimeZone);
        }

        String timeZoneName = omTimeZone.getIcal();

        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone(timeZoneName));
        int offset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);

        log.debug("addAppointment offset :: " + offset);

        Date gmtTimeStart = new Date(dFrom.getTime() - offset);
        Date gmtTimeEnd = new Date(dTo.getTime() - offset);

        Invitations invitation = invitationManagement.addInvitationLink(user_level, username, message, baseurl,
                email, subject, room_id, conferencedomain, isPasswordProtected, invitationpass, valid, dFrom,
                dTo, users_id, baseurl, language_id, true, gmtTimeStart, gmtTimeEnd, null, username);

        if (invitation != null) {
            return "success";
        } else {
            return "Sys - Error";
        }
    } catch (Exception err) {
        log.error("[sendInvitationHash]", err);
    }

    return null;

    // return
    // invitationManagement.sendInvitionLink(user_level,
    // username, message, domain, room, roomtype, baseurl, email, subject,
    // room_id);
}

From source file:ISO8601DateTimeFormat.java

/**
 * Write the time zone string.//from w  ww.j  a v a  2  s . c o m
 * @param sbuf The buffer to append the time zone.
 */
protected final void writeTZ(StringBuffer sbuf) {
    int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
    if (offset == 0) {
        sbuf.append('Z');
    } else {
        int offsetHour = offset / 3600000;
        int offsetMin = (offset % 3600000) / 60000;
        if (offset >= 0) {
            sbuf.append('+');
        } else {
            sbuf.append('-');
            offsetHour = 0 - offsetHour;
            offsetMin = 0 - offsetMin;
        }
        appendInt(sbuf, offsetHour, 2);
        sbuf.append(':');
        appendInt(sbuf, offsetMin, 2);
    }
}

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

@Test
public void testGetCalendarRespectsDaylightSavings() throws DataTypeException, ParseException {

    SimpleDateFormat utcFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");

    CommonTS ts = new CommonTS();
    ts.setValue("19840601080102-0400");

    Calendar asCalendar = ts.getValueAsCalendar();
    assertEquals(8, asCalendar.get(Calendar.HOUR_OF_DAY));
    assertEquals(1, asCalendar.get(Calendar.MINUTE));
    assertEquals(2, asCalendar.get(Calendar.SECOND));
    assertEquals(-4, asCalendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000));

    Date asDate = ts.getValueAsDate();
    assertEquals(asCalendar.getTime(), asDate);
    assertEquals(asCalendar.getTimeInMillis(), asDate.getTime());

    Date midnightUtc = utcFmt.parse("1984-06-01 12:01:02 +0000");
    assertEquals(midnightUtc, ts.getValueAsDate());

    // Make sure it reverses correctly as well
    CommonTS ts2 = new CommonTS();
    ts2.setValue(asCalendar);// w w w .j ava  2 s.c  o m
    assertEquals(ts.getValue(), ts2.getValue());

    /*
     * We try this once on Jun 1 and once on Jan 1 so that we cover the case
     * of parsing a non-DST date during DST, as well as a DST date outside
     * of DST.
     */

    ts = new CommonTS();
    ts.setValue("19840101070102-0500");

    asCalendar = ts.getValueAsCalendar();
    assertEquals(7, asCalendar.get(Calendar.HOUR_OF_DAY));
    assertEquals(1, asCalendar.get(Calendar.MINUTE));
    assertEquals(2, asCalendar.get(Calendar.SECOND));
    assertEquals(-5, asCalendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000));

    asDate = ts.getValueAsDate();
    assertEquals(asCalendar.getTime(), asDate);
    assertEquals(asCalendar.getTimeInMillis(), asDate.getTime());

    midnightUtc = utcFmt.parse("1984-01-01 12:01:02 +0000");

    assertEquals(midnightUtc, ts.getValueAsDate());

    // Make sure it reverses correctly as well
    ts2 = new CommonTS();
    ts2.setValue(asCalendar);
    assertEquals(ts.getValue(), ts2.getValue());

}

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

private StringBuffer convertLocalTimeToUTC(String value) {
    StringBuffer localTimeColumnValue = null;
    try {/*from w  w  w .  ja  v a2  s.  c  o m*/
        Date UTCDate = getClassicFormat().parse(value);
        Calendar now = Calendar.getInstance();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(UTCDate);
        calendar.set(Calendar.DATE, now.get(Calendar.DATE));
        calendar.set(Calendar.MONTH, now.get(Calendar.MONTH));
        calendar.set(Calendar.YEAR, now.get(Calendar.YEAR));

        int gmtMillisecondOffset = (now.get(Calendar.ZONE_OFFSET) + now.get(Calendar.DST_OFFSET));
        calendar.add(Calendar.MILLISECOND, -gmtMillisecondOffset);
        localTimeColumnValue = getClassicFormat().format(calendar.getTime(), new StringBuffer(),
                new FieldPosition(0));
    } catch (ParseException e) {
        throw new OBException("Exception when parsing date ", e);
    }
    return localTimeColumnValue;
}

From source file:com.taobao.itest.util.DateConverter.java

/**
 * get GMT Time/* w  w w  . j a v a  2 s.  co  m*/
 * 
 * @param calendar
 * @return
 */
public Date getGmtDate(Long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);
    int offset = calendar.get(Calendar.ZONE_OFFSET) / 3600000 + calendar.get(Calendar.DST_OFFSET) / 3600000;
    calendar.add(Calendar.HOUR, -offset);
    Date date = calendar.getTime();

    return date;
}

From source file:org.kuali.coeus.s2sgen.impl.datetime.S2SDateTimeServiceImpl.java

/**
 * This method is used to get Calendar date for the corresponding date object.
 *
 * @param date(Date) date for which Calendar value has to be found.
 * @return calendar value corresponding to the date.
 *///from  w w w.jav a2  s.  c o  m
@Override
public Calendar convertDateToCalendar(java.util.Date date) {
    Calendar calendar = null;
    if (date != null) {
        calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.clear(Calendar.ZONE_OFFSET);
        calendar.clear(Calendar.DST_OFFSET);
    }
    return calendar;
}

From source file:org.openmeetings.utils.math.TimezoneUtil.java

public static long _getOffset(TimeZone timezone) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(timezone);/* w  w  w  . ja  va 2  s. c  om*/
    return cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
}

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

public static String convertToString(Date value) {

    if (isCustomClassPresent) {
        // this means user has define a seperate converter util class
        return invokeToStringMethod(value, Date.class);
    } else {//from   www  . j a va2 s.c o  m
        // lexical form of the date is '-'? yyyy '-' mm '-' dd zzzzzz?
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(value);
        if (!calendar.isSet(Calendar.ZONE_OFFSET)) {
            calendar.setTimeZone(TimeZone.getDefault());
        }
        StringBuffer dateString = new StringBuffer(16);
        appendDate(dateString, calendar);
        appendTimeZone(calendar, dateString);
        return dateString.toString();
    }
}

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

public static Date getGmtDate() throws ServiceException {
    Date cmpdate = new Date();
    try {//  w  w  w  . j a v  a 2 s .c  o  m
        Calendar calInstance = Calendar.getInstance();
        int gmtoffset = calInstance.get(Calendar.DST_OFFSET) + calInstance.get(Calendar.ZONE_OFFSET);
        long date = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy HH:mm a");
        String cmp = sdf.format(new java.util.Date(date));
        cmpdate = new Date(sdf.parse(cmp).getTime() - gmtoffset);
    } catch (ParseException e) {
        throw ServiceException.FAILURE("Timezone.getGmtDate", e);
    }
    return cmpdate;
}

From source file:org.sakaiproject.nakamura.api.resource.lite.LiteJsonImporterTest.java

@Test
public void testObject() {
    LiteJsonImporter liteImporter = new LiteJsonImporter();
    Assert.assertEquals("1", liteImporter.getObject("1", String.class));
    Assert.assertEquals((Long) 1L, liteImporter.getObject("1", Long.class));
    Assert.assertEquals((Integer) 1, liteImporter.getObject("1", Integer.class));
    Assert.assertEquals((Double) 1.0, liteImporter.getObject("1", Double.class));
    Assert.assertEquals(new BigDecimal("1.094"), liteImporter.getObject("1.094", BigDecimal.class));
    Assert.assertEquals((Boolean) true, liteImporter.getObject("true", Boolean.class));
    Calendar c = liteImporter.getObject("20110329T101523+0330", Calendar.class);
    Assert.assertEquals(2011, c.get(Calendar.YEAR));
    Assert.assertEquals(2, c.get(Calendar.MONTH));
    Assert.assertEquals(29, c.get(Calendar.DAY_OF_MONTH));
    Assert.assertEquals(10, c.get(Calendar.HOUR_OF_DAY));
    Assert.assertEquals(15, c.get(Calendar.MINUTE));
    Assert.assertEquals(23, c.get(Calendar.SECOND));
    Assert.assertEquals(((3 * 60 + 30) * 60) * 1000, c.get(Calendar.ZONE_OFFSET));
    c = liteImporter.getObject("20110329T101523Z", Calendar.class);
    Assert.assertEquals(2011, c.get(Calendar.YEAR));
    Assert.assertEquals(2, c.get(Calendar.MONTH));
    Assert.assertEquals(29, c.get(Calendar.DAY_OF_MONTH));
    Assert.assertEquals(10, c.get(Calendar.HOUR_OF_DAY));
    Assert.assertEquals(15, c.get(Calendar.MINUTE));
    Assert.assertEquals(23, c.get(Calendar.SECOND));
    Assert.assertEquals(0, c.get(Calendar.ZONE_OFFSET));
    c = liteImporter.getObject("20110329T101523-0330", Calendar.class);
    Assert.assertEquals(2011, c.get(Calendar.YEAR));
    Assert.assertEquals(2, c.get(Calendar.MONTH));
    Assert.assertEquals(29, c.get(Calendar.DAY_OF_MONTH));
    Assert.assertEquals(10, c.get(Calendar.HOUR_OF_DAY));
    Assert.assertEquals(15, c.get(Calendar.MINUTE));
    Assert.assertEquals(23, c.get(Calendar.SECOND));
    Assert.assertEquals(-((3 * 60 + 30) * 60) * 1000, c.get(Calendar.ZONE_OFFSET));
}