Example usage for java.util Calendar setTimeZone

List of usage examples for java.util Calendar setTimeZone

Introduction

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

Prototype

public void setTimeZone(TimeZone value) 

Source Link

Document

Sets the time zone with the given time zone value.

Usage

From source file:org.apache.logging.log4j.core.util.datetime.FastDatePrinter.java

@Override
public <B extends Appendable> B format(Calendar calendar, final B buf) {
    // do not pass in calendar directly, this will cause TimeZone of FastDatePrinter to be ignored
    if (!calendar.getTimeZone().equals(mTimeZone)) {
        calendar = (Calendar) calendar.clone();
        calendar.setTimeZone(mTimeZone);
    }/*ww w.j  ava  2  s .  com*/
    return applyRules(calendar, buf);
}

From source file:com.concursive.connect.web.modules.calendar.utils.CalendarView.java

/**
 * Gets the Today attribute of the CalendarView object
 *
 * @return The Today value//from w  ww  . j a v  a  2 s  .  c  om
 */
public String getToday() {
    Calendar today = Calendar.getInstance(locale);
    today.set(Calendar.HOUR, 0);
    today.set(Calendar.MINUTE, 0);
    today.set(Calendar.SECOND, 0);
    today.set(Calendar.MILLISECOND, 0);
    if (timeZone != null) {
        today.setTimeZone(timeZone);
    }
    if (locale != null) {
        SimpleDateFormat formatter = (SimpleDateFormat) SimpleDateFormat.getDateInstance(DateFormat.LONG,
                locale);
        return formatter.format(today.getTime());
    } else {
        return (this.getMonthName(today) + " " + today.get(Calendar.DAY_OF_MONTH) + ", "
                + today.get(Calendar.YEAR));
    }
}

From source file:org.obm.domain.dao.ContactDaoJdbcImpl.java

private Event getEvent(AccessToken token, String displayName, Date startDate) throws SQLException {

    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone("GMT"));
    cal.setTime(startDate);// w  w w  .j ava  2  s.  c  o m

    Event e = new Event();

    e.setExtId(eventExtIdFactory.generate());
    e.setTitle(displayName);
    e.setStartDate(cal.getTime());
    e.setDuration(3600);
    e.setAllday(true);
    EventRecurrence rec = new EventRecurrence();
    rec.setDays(new RecurrenceDays());
    rec.setFrequence(1);
    rec.setKind(RecurrenceKind.yearly);
    rec.setEnd(null);
    e.setRecurrence(rec);
    e.setPrivacy(EventPrivacy.PRIVATE);
    e.setPriority(1);
    e.setOwnerEmail(token.getUserEmail());

    Attendee at = UserAttendee.builder().email(token.getUserEmail()).participationRole(ParticipationRole.CHAIR)
            .participation(Participation.accepted()).asOrganizer()
            .entityId(obmHelper.fetchEntityId("User", token.getObmId())).build();

    e.addAttendee(at);

    logger.info("inserting birthday with date " + cal.getTime());
    return e;
}

From source file:com.concursive.connect.web.modules.calendar.utils.CalendarView.java

/**
 * Returns true if today is the current calendar day being drawn
 *
 * @param tmp    Description of Parameter
 * @param indate Description of Parameter
 * @return The CurrentDay value//from  w w w  .j a va  2s.com
 */
public boolean isCurrentDay(Calendar tmp, int indate) {
    Calendar thisMonth = Calendar.getInstance(locale);
    thisMonth.set(Calendar.HOUR, 0);
    thisMonth.set(Calendar.MINUTE, 0);
    thisMonth.set(Calendar.SECOND, 0);
    thisMonth.set(Calendar.MILLISECOND, 0);
    if (timeZone != null) {
        thisMonth.setTimeZone(timeZone);
    }
    if ((indate == thisMonth.get(Calendar.DAY_OF_MONTH))
            && (tmp.get(Calendar.MONTH) == thisMonth.get(Calendar.MONTH))
            && (tmp.get(Calendar.YEAR) == thisMonth.get(Calendar.YEAR))) {
        return true;
    } else {
        return false;
    }
}

From source file:org.openhab.binding.sonos.handler.ZonePlayerHandler.java

public void snoozeAlarm(Command command) {
    if (isAlarmRunning() && command instanceof DecimalType) {

        int minutes = ((DecimalType) command).intValue();

        Map<String, String> inputs = new HashMap<String, String>();

        Calendar snoozePeriod = Calendar.getInstance();
        snoozePeriod.setTimeZone(TimeZone.getTimeZone("GMT"));
        snoozePeriod.setTimeInMillis(0);
        snoozePeriod.add(Calendar.MINUTE, minutes);
        SimpleDateFormat pFormatter = new SimpleDateFormat("HH:mm:ss");
        pFormatter.setTimeZone(TimeZone.getTimeZone("GMT"));

        try {/*from   w w w.j av  a 2  s  . c om*/
            inputs.put("Duration", pFormatter.format(snoozePeriod.getTime()));
        } catch (NumberFormatException ex) {
            logger.error("Action Invalid Value Format Exception {}", ex.getMessage());
        }

        Map<String, String> result = service.invokeAction(this, "AVTransport", "SnoozeAlarm", inputs);

        for (String variable : result.keySet()) {
            this.onValueReceived(variable, result.get(variable), "AVTransport");
        }
    } else {
        logger.warn("There is no alarm running on {} ", this);
    }
}

From source file:ch.dbs.actions.user.UserAction.java

/**
 * Data privacy: checks if the allowed range is exceeded. <p></p>
 * //from w w w .ja v a2 s . c o  m
 * @param Strinf date_from
 * @return true/false
 */
private boolean checkAnonymize(final String date_from, final String timezone) {
    boolean check = false;

    if (date_from != null && ReadSystemConfigurations.isAnonymizationActivated()) {
        final Bestellungen b = new Bestellungen();
        final Calendar cal = b.stringFromMysqlToCal(date_from);
        final Calendar limit = Calendar.getInstance();
        limit.setTimeZone(TimeZone.getTimeZone(timezone));
        limit.add(Calendar.MONTH, -ReadSystemConfigurations.getAnonymizationAfterMonths());
        limit.add(Calendar.DAY_OF_MONTH, -1);
        if (cal.before(limit)) {
            check = true;
        }
    }

    return check;
}

From source file:org.openhab.binding.sonos.handler.ZonePlayerHandler.java

public void setAlarm(boolean alarmSwitch) {

    List<SonosAlarm> sonosAlarms = getCurrentAlarmList();

    // find the nearest alarm - take the current time from the Sonos System,
    // not the system where openhab is running
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    fmt.setTimeZone(TimeZone.getTimeZone("GMT"));

    String currentLocalTime = getTime();
    Date currentDateTime = null;//www  . j a  v a  2 s  . c  o m
    try {
        currentDateTime = fmt.parse(currentLocalTime);
    } catch (ParseException e) {
        logger.error("An exception occurred while formatting a date");
        e.printStackTrace();
    }

    if (currentDateTime != null) {
        Calendar currentDateTimeCalendar = Calendar.getInstance();
        currentDateTimeCalendar.setTimeZone(TimeZone.getTimeZone("GMT"));
        currentDateTimeCalendar.setTime(currentDateTime);
        currentDateTimeCalendar.add(Calendar.DAY_OF_YEAR, 10);
        long shortestDuration = currentDateTimeCalendar.getTimeInMillis() - currentDateTime.getTime();

        SonosAlarm firstAlarm = null;

        for (SonosAlarm anAlarm : sonosAlarms) {
            SimpleDateFormat durationFormat = new SimpleDateFormat("HH:mm:ss");
            durationFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
            Date durationDate = null;
            try {
                durationDate = durationFormat.parse(anAlarm.getDuration());
            } catch (ParseException e) {
                logger.error("An exception occurred while parsing a date : '{}'", e.getMessage());
            }

            long duration = durationDate.getTime();

            if (duration < shortestDuration && anAlarm.getRoomUUID().equals(getUDN())) {
                shortestDuration = duration;
                firstAlarm = anAlarm;
            }
        }

        // Set the Alarm
        if (firstAlarm != null) {

            if (alarmSwitch) {
                firstAlarm.setEnabled(true);
            } else {
                firstAlarm.setEnabled(false);
            }

            updateAlarm(firstAlarm);

        }
    }
}

From source file:fr.aliacom.obm.common.contact.ContactDaoJdbcImpl.java

private Event getEvent(AccessToken token, String displayName, Date startDate) {

    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone("GMT"));
    cal.setTime(startDate);//from  w  ww  .  j av  a2  s  .  c om

    Event e = new Event();

    e.setExtId(eventExtIdFactory.generate());
    e.setTitle(displayName);
    e.setStartDate(cal.getTime());
    e.setDuration(3600);
    e.setAllday(true);
    EventRecurrence rec = new EventRecurrence();
    rec.setDays(new RecurrenceDays());
    rec.setFrequence(1);
    rec.setKind(RecurrenceKind.yearly);
    rec.setEnd(null);
    e.setRecurrence(rec);
    e.setPrivacy(EventPrivacy.PRIVATE);
    e.setPriority(1);

    Attendee at = UserAttendee.builder().email(token.getUserEmail()).participationRole(ParticipationRole.CHAIR)
            .participation(Participation.accepted()).build();

    e.addAttendee(at);

    logger.info("inserting birthday with date " + cal.getTime());
    return e;
}

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

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

    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
    Date date = format.parse("20100609 12:40:05");
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);//from ww  w .j  a v a 2 s .  co m

    CommonTS commonTS = new CommonTS();
    commonTS.setValueToMinute(cal);
    assertEquals("201006091240", commonTS.getValue());

    commonTS = new CommonTS();
    commonTS.setValueToMinute(date);
    assertEquals("201006091240", commonTS.getValue());

    commonTS = new CommonTS();
    commonTS.setValueToSecond(cal);
    assertEquals("20100609124005", commonTS.getValue());

    commonTS = new CommonTS();
    commonTS.setValueToSecond(date);
    assertEquals("20100609124005", commonTS.getValue());

    commonTS = new CommonTS();
    cal.set(Calendar.MILLISECOND, 250);
    cal.set(Calendar.ZONE_OFFSET, -5 * 1000 * 60 * 60);
    commonTS.setValue(cal);
    String value = commonTS.getValue();
    assertEquals("20100609124005.25-0400", value);

    // 3410095 : proof to avoid overflow
    commonTS = new CommonTS();
    cal.set(Calendar.ZONE_OFFSET, 11 * 1000 * 60 * 60);
    commonTS.setValue(cal);
    value = commonTS.getValue();
    assertEquals("20100609124005.25+1200", value);

    // 3410095-related : proof to cover time zones not at the full hour,
    // e.g. India
    commonTS = new CommonTS();
    cal.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
    commonTS.setValue(cal);
    value = commonTS.getValue();
    assertEquals("20100609061005.25+0530", value);

    commonTS = new CommonTS();
    commonTS.setValue("201006091240");
    assertEquals("201006091240", commonTS.getValue());
    String formatted = format.format(commonTS.getValueAsDate());
    assertEquals("20100609 12:40:00", formatted);

    commonTS = new CommonTS();
    commonTS.setValue("201006091240");
    assertEquals("20100609 12:40:00", format.format(commonTS.getValueAsCalendar().getTime()));

    // Check millis and offset
    commonTS = new CommonTS();
    commonTS.setValue("20100609124005.25-0400");
    cal = commonTS.getValueAsCalendar();
    assertEquals(250, cal.get(Calendar.MILLISECOND));
    assertEquals(-4 * 1000 * 60 * 60, cal.get(Calendar.ZONE_OFFSET));

    // Check DST offset
    commonTS = new CommonTS();
    Date dt = new Date(1364857200035L);
    SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss.SSSZ");
    fmt.setTimeZone(TimeZone.getTimeZone("Canada/Eastern"));
    String dateString = fmt.format(dt);
    commonTS.setValue(dt);
    String tsString = commonTS.getValue();
    assertEquals(dateString, tsString);
}