Example usage for java.util TimeZone inDaylightTime

List of usage examples for java.util TimeZone inDaylightTime

Introduction

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

Prototype

public abstract boolean inDaylightTime(Date date);

Source Link

Document

Queries if the given date is in Daylight Saving Time in this time zone.

Usage

From source file:com.mozilla.SUTAgentAndroid.service.DoCommand.java

public String GetTimeZone() {
    String sRet = "";
    TimeZone tz;

    tz = TimeZone.getDefault();/*from   w  ww.j av  a2s  . c o  m*/
    Date now = new Date();
    sRet = tz.getDisplayName(tz.inDaylightTime(now), TimeZone.LONG);

    return (sRet);
}

From source file:com.mozilla.SUTAgentAndroid.service.DoCommand.java

public String SetTimeZone(String sTimeZone) {
    String sRet = "Unable to set timezone to " + sTimeZone;
    TimeZone tz = null;
    AlarmManager amgr = null;//from   ww  w. j  a v  a2s  .c  o m

    if ((sTimeZone.length() > 0) && (sTimeZone.startsWith("GMT"))) {
        amgr = (AlarmManager) contextWrapper.getSystemService(Context.ALARM_SERVICE);
        if (amgr != null)
            amgr.setTimeZone(sTimeZone);
    } else {
        String[] zoneNames = TimeZone.getAvailableIDs();
        int nNumMatches = zoneNames.length;
        int lcv = 0;

        for (lcv = 0; lcv < nNumMatches; lcv++) {
            if (zoneNames[lcv].equalsIgnoreCase(sTimeZone))
                break;
        }

        if (lcv < nNumMatches) {
            amgr = (AlarmManager) contextWrapper.getSystemService(Context.ALARM_SERVICE);
            if (amgr != null)
                amgr.setTimeZone(zoneNames[lcv]);
        }
    }

    if (amgr != null) {
        tz = TimeZone.getDefault();
        Date now = new Date();
        sRet = tz.getDisplayName(tz.inDaylightTime(now), TimeZone.LONG);
    }

    return (sRet);
}

From source file:com.cloud.api.ApiResponseHelper.java

public String getDateStringInternal(Date inputDate) {
    if (inputDate == null) {
        return null;
    }//from ww  w.  j a v a  2  s  .  c om

    TimeZone tz = _usageSvc.getUsageTimezone();
    Calendar cal = Calendar.getInstance(tz);
    cal.setTime(inputDate);

    StringBuilder sb = new StringBuilder(32);
    sb.append(cal.get(Calendar.YEAR)).append('-');

    int month = cal.get(Calendar.MONTH) + 1;
    if (month < 10) {
        sb.append('0');
    }
    sb.append(month).append('-');

    int day = cal.get(Calendar.DAY_OF_MONTH);
    if (day < 10) {
        sb.append('0');
    }
    sb.append(day);

    sb.append("'T'");

    int hour = cal.get(Calendar.HOUR_OF_DAY);
    if (hour < 10) {
        sb.append('0');
    }
    sb.append(hour).append(':');

    int minute = cal.get(Calendar.MINUTE);
    if (minute < 10) {
        sb.append('0');
    }
    sb.append(minute).append(':');

    int seconds = cal.get(Calendar.SECOND);
    if (seconds < 10) {
        sb.append('0');
    }
    sb.append(seconds);

    double offset = cal.get(Calendar.ZONE_OFFSET);
    if (tz.inDaylightTime(inputDate)) {
        offset += (1.0 * tz.getDSTSavings()); // add the timezone's DST
        // value (typically 1 hour
        // expressed in milliseconds)
    }

    offset = offset / (1000d * 60d * 60d);
    int hourOffset = (int) offset;
    double decimalVal = Math.abs(offset) - Math.abs(hourOffset);
    int minuteOffset = (int) (decimalVal * 60);

    if (hourOffset < 0) {
        if (hourOffset > -10) {
            sb.append("-0");
        } else {
            sb.append('-');
        }
        sb.append(Math.abs(hourOffset));
    } else {
        if (hourOffset < 10) {
            sb.append("+0");
        } else {
            sb.append("+");
        }
        sb.append(hourOffset);
    }

    sb.append(':');

    if (minuteOffset == 0) {
        sb.append("00");
    } else if (minuteOffset < 10) {
        sb.append('0').append(minuteOffset);
    } else {
        sb.append(minuteOffset);
    }

    return sb.toString();
}

From source file:com.redhat.rhn.frontend.xmlrpc.system.SystemHandler.java

private Date convertLocalToUtc(Date in) {
    Calendar c = Calendar.getInstance();
    c.setTime(in);//from ww w  .  ja  v  a 2  s.  c  om
    TimeZone z = c.getTimeZone();
    int offset = z.getRawOffset();
    if (z.inDaylightTime(in)) {
        offset += z.getDSTSavings();
    }
    int offsetHrs = offset / 1000 / 60 / 60;
    int offsetMins = offset / 1000 / 60 % 60;
    c.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
    c.add(Calendar.MINUTE, (-offsetMins));
    c.set(Calendar.MILLISECOND, 0);
    return c.getTime();
}

From source file:org.sakaiproject.calendar.tool.CalendarAction.java

public String buildMainPanelContext(VelocityPortlet portlet, Context context, RunData runData,
        SessionState sstate) {/*from ww w . ja  v  a  2  s.c  om*/
    CalendarActionState state = (CalendarActionState) getState(portlet, runData, CalendarActionState.class);

    String template = (String) getContext(runData).get("template");

    // get current state (view); if not set use tool default or default to week view
    String stateName = state.getState();
    if (stateName == null) {
        stateName = portlet.getPortletConfig().getInitParameter(PORTLET_CONFIG_DEFAULT_VIEW);
        if (stateName == null)
            stateName = ServerConfigurationService.getString("calendar.default.view", "week");
        state.setState(stateName);
    }

    if (stateName.equals(STATE_SCHEDULE_IMPORT)) {
        buildImportContext(portlet, context, runData, state, getSessionState(runData));
    } else if (stateName.equals(STATE_MERGE_CALENDARS)) {
        // build the context to display the options panel
        mergedCalendarPage.buildContext(portlet, context, runData, state, getSessionState(runData));
    } else if (stateName.equals(STATE_CALENDAR_SUBSCRIPTIONS)) {
        // build the context to display the options panel
        calendarSubscriptionsPage.buildContext(portlet, context, runData, state, getSessionState(runData));
    } else if (stateName.equals(STATE_CUSTOMIZE_CALENDAR)) {
        // build the context to display the options panel
        //needed to track when user clicks 'Save' or 'Cancel'
        String sstatepage = "";

        Object statepageAttribute = sstate.getAttribute(SSTATE_ATTRIBUTE_ADDFIELDS_PAGE);

        if (statepageAttribute != null) {
            sstatepage = statepageAttribute.toString();
        }

        if (!sstatepage.equals(PAGE_ADDFIELDS)) {
            sstate.setAttribute(SSTATE_ATTRIBUTE_ADDFIELDS_PAGE, PAGE_MAIN);
        }

        customizeCalendarPage.buildContext(portlet, context, runData, state, getSessionState(runData));
    } else if ((stateName.equals("revise")) || (stateName.equals("goToReviseCalendar"))) {
        // build the context for the normal view show
        buildReviseContext(portlet, context, runData, state);
    } else if (stateName.equals("description")) {
        // build the context for the basic step of adding file
        buildDescriptionContext(portlet, context, runData, state);
    } else if (stateName.equals("year")) {
        // build the context for the advanced step of adding file
        buildYearContext(portlet, context, runData, state);
    } else if (stateName.equals("month")) {
        // build the context for the basic step of adding folder
        buildMonthContext(portlet, context, runData, state);
    } else if (stateName.equals("day")) {
        // build the context for the basic step of adding simple text
        buildDayContext(portlet, context, runData, state);
    } else if (stateName.equals("week")) {
        // build the context for the basic step of delete confirm page
        buildWeekContext(portlet, context, runData, state);
    } else if (stateName.equals("new")) {
        // build the context to display the property list
        buildNewContext(portlet, context, runData, state);
    } else if (stateName.equals("icalEx")) {
        buildIcalExportPanelContext(portlet, context, runData, state);
    } else if (stateName.equals("opaqueUrlClean")) {
        buildOpaqueUrlCleanContext(portlet, context, runData, state);
    } else if (stateName.equals("opaqueUrlExisting")) {
        buildOpaqueUrlExistingContext(portlet, context, runData, state);
    } else if (stateName.equals("delete")) {
        // build the context to display the property list
        buildDeleteContext(portlet, context, runData, state);
    } else if (stateName.equals("list")) {
        // build the context to display the list view
        buildListContext(portlet, context, runData, state);
    } else if (stateName.equals(STATE_SET_FREQUENCY)) {
        buildFrequencyContext(portlet, context, runData, state);
    }

    if (stateName.equals("description") || stateName.equals("year") || stateName.equals("month")
            || stateName.equals("day") || stateName.equals("week") || stateName.equals("list")) {
        // SAK-23566 capture the view calendar events
        EventTrackingService ets = (EventTrackingService) ComponentManager.get(EventTrackingService.class);
        String calendarRef = state.getPrimaryCalendarReference();
        if (ets != null && calendarRef != null) {
            // need to cleanup the cal references which look like /calendar/calendar/4ea74c4d-3f9e-4c32-b03f-15e7915e6051/main
            String eventRef = StringUtils.replace(calendarRef, "/main", "/" + stateName);
            String calendarEventId = state.getCalendarEventId();
            if (StringUtils.isNotBlank(calendarEventId)) {
                eventRef += "/" + calendarEventId;
            }
            ets.post(ets.newEvent("calendar.read", eventRef, false));
        }
    }

    TimeZone timeZone = TimeService.getLocalTimeZone();
    context.put("timezone", timeZone.getDisplayName(timeZone.inDaylightTime(new Date()), TimeZone.SHORT));

    //the AM/PM strings
    context.put("amString", CalendarUtil.getLocalAMString());
    context.put("pmString", CalendarUtil.getLocalPMString());

    // group realted variables
    context.put("siteAccess", CalendarEvent.EventAccess.SITE);
    context.put("groupAccess", CalendarEvent.EventAccess.GROUPED);

    context.put("message", state.getState());
    context.put("state", state.getKey());
    context.put("tlang", rb);
    context.put("config", configProps);
    context.put("dateFormat", getDateFormatString());
    context.put("timeFormat", getTimeFormatString());

    return template;

}