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.roller.weblogger.ui.struts2.editor.EntryBean.java

/**
 * Copy values from WeblogEntryData to this Form.
 *//* w  w  w .  java 2  s  .  com*/
public void copyFrom(WeblogEntry entry, Locale locale) {

    setId(entry.getId());
    setTitle(entry.getTitle());
    setLocale(entry.getLocale());
    setStatus(entry.getStatus());
    setSummary(entry.getSummary());
    setText(entry.getText());
    setCategoryId(entry.getCategory().getId());
    setTagsAsString(entry.getTagsAsString());

    // set comment count, ignoreSpam=false, approvedOnly=false
    setCommentCount(entry.getComments(false, false).size());

    // init plugins values
    if (entry.getPlugins() != null) {
        setPlugins(StringUtils.split(entry.getPlugins(), ","));
    }

    // init pubtime values
    if (entry.getPubTime() != null) {
        log.debug("entry pubtime is " + entry.getPubTime());

        //Calendar cal = Calendar.getInstance(locale);
        Calendar cal = Calendar.getInstance();
        cal.setTime(entry.getPubTime());
        cal.setTimeZone(entry.getWebsite().getTimeZoneInstance());

        setHours(cal.get(Calendar.HOUR_OF_DAY));
        setMinutes(cal.get(Calendar.MINUTE));
        setSeconds(cal.get(Calendar.SECOND));

        // TODO: at some point this date conversion should be locale sensitive,
        // however at this point our calendar widget does not take into account
        // locales and only operates in the standard English US locale.
        DateFormat df = new SimpleDateFormat("MM/dd/yy");
        df.setTimeZone(entry.getWebsite().getTimeZoneInstance());
        setDateString(df.format(entry.getPubTime()));

        log.debug("pubtime vals are " + getDateString() + ", " + getHours() + ", " + getMinutes() + ", "
                + getSeconds());
    }

    setAllowComments(entry.getAllowComments());
    setCommentDays(entry.getCommentDays());
    setRightToLeft(entry.getRightToLeft());
    setPinnedToMain(entry.getPinnedToMain());

    // enclosure url, if it exists
    Set<WeblogEntryAttribute> attrs = entry.getEntryAttributes();
    if (attrs != null && attrs.size() > 0) {
        for (WeblogEntryAttribute attr : attrs) {
            if ("att_mediacast_url".equals(attr.getName())) {
                setEnclosureURL(attr.getValue());
            }
        }
    }
}

From source file:com.knowbout.cc2nlp.server.CCEventServiceImpl.java

private String getFileDate(long timestamp) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone("GMT"));
    cal.setTimeInMillis(timestamp);//from   ww w  .  j a  v  a 2 s.  com
    return String.format("%1$tY-%1$tm-%1$td", cal);
}

From source file:org.appcelerator.util.DateUtil.java

/**
 * attempts to parse the string date in known formats
 *
 * @param str date string to parse//w  w  w.  j av  a 2 s.com
 * @return calendar for parsed date or <code>null</code> if not successful
 */
public static Calendar parseDate(String str) {
    if (str == null || str.length() == 0) {
        return null;
    }
    Date date;
    Calendar cal = null;
    try {
        synchronized (ISO8601_DATE_FORMAT) {
            date = ISO8601_DATE_FORMAT.parse(str);
        }
    } catch (Exception ex) {
        try {
            synchronized (RFC822_DATE_FORMAT) {
                date = RFC822_DATE_FORMAT.parse(str);
            }
        } catch (Exception ex2) {
            try {
                synchronized (ATOM_DATE_FORMAT) {
                    date = ATOM_DATE_FORMAT.parse(str);
                }
            } catch (Exception ex3) {
                try {
                    synchronized (APP_DATE_FORMAT) {
                        date = APP_DATE_FORMAT.parse(str);
                    }
                } catch (ParseException ex1) {
                    // last resort, try our common date formats
                    date = parseUsingCommonDateFormats(str);
                }
            }
        }
    }
    if (date != null) {
        cal = new GregorianCalendar();
        cal.setTime(date);
        cal.setTimeZone(GMT_TZ);
    } else {
        LOG.error("couldn't parse date: " + str, new Exception());
    }
    return cal;
}

From source file:org.dozer.converters.XMLGregorianCalendarConverter.java

/**
 * {@inheritDoc}/*ww w  .  jav a2  s  .co m*/
 */
public Object convert(Class destClass, Object srcObj) {
    Class sourceClass = srcObj.getClass();
    Calendar result = new GregorianCalendar();

    if (java.util.Date.class.isAssignableFrom(sourceClass)) {
        // Date --> XMLGregorianCalendar
        result.setTime((java.util.Date) srcObj);
    } else if (Calendar.class.isAssignableFrom(sourceClass)) {
        // Calendar --> XMLGregorianCalendar
        Calendar c = (Calendar) srcObj;
        result.setTime(c.getTime());
        result.setTimeZone(c.getTimeZone());
    } else if (XMLGregorianCalendar.class.isAssignableFrom(sourceClass)) {
        result = ((XMLGregorianCalendar) srcObj).toGregorianCalendar();
    } else if (dateFormat != null && String.class.isAssignableFrom(sourceClass)) {
        if ("".equals(srcObj)) {
            return null;
        }
        try {
            long time = dateFormat.parse((String) srcObj).getTime();
            result.setTime(new Date(time));
        } catch (ParseException e) {
            throw new ConversionException("Unable to parse source object using specified date format", e);
        }
    } else {
        try {
            long time = Long.parseLong(srcObj.toString());
            result.setTime(new Date(time));
        } catch (NumberFormatException e) {
            throw new ConversionException("Unable to determine time in millis of source object", e);
        }
    }

    return dataTypeFactory().newXMLGregorianCalendar((GregorianCalendar) result);
}

From source file:org.codekaizen.vtj.text.BpDateFormatTest.java

/**
 * DOCUMENT ME!//w  w w. j av a 2 s .co m
 */
public void testIsoFormatting() {
    BpDateFormat fmt1 = null;
    String str1 = null;
    Calendar cal = Calendar.getInstance();

    if (!cal.getTimeZone().getID().equals("America/Chicago")) {
        cal.setTimeZone(TimeZone.getTimeZone("America/Chicago"));
        cal.clear();
    }

    cal.set(2002, Calendar.JANUARY, 2, 8, 16, 44);
    cal.set(Calendar.MILLISECOND, 18);

    fmt1 = new BpDateFormat(BpDateFormat.ISO_DATE_ONLY, Locale.US);
    str1 = fmt1.format(cal.getTime());
    assertEquals("2002-01-02", str1);

    fmt1 = new BpDateFormat(BpDateFormat.ISO_YEAR_WEEK, Locale.US);
    str1 = fmt1.format(cal.getTime());
    assertEquals("2002-W01", str1);

    fmt1 = new BpDateFormat(BpDateFormat.ISO_TIME_ONLY, Locale.US);
    str1 = fmt1.format(cal.getTime());
    assertEquals("T08:16:44", str1);

    fmt1 = new BpDateFormat(BpDateFormat.ISO_DATE_TIME, Locale.US);
    str1 = fmt1.format(cal.getTime());
    assertEquals("2002-01-02T08:16:44.018-06:00", str1);

    cal.set(Calendar.MONTH, Calendar.JUNE);
    str1 = fmt1.format(cal.getTime());
    assertEquals("2002-06-02T08:16:44.018-05:00", str1);

    cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    cal.set(2002, Calendar.JANUARY, 2, 8, 16, 44);
    cal.set(Calendar.MILLISECOND, 18);
    fmt1.setTimeZone(TimeZone.getTimeZone("UTC"));
    str1 = fmt1.format(cal.getTime());
    assertEquals("2002-01-02T08:16:44.018Z", str1);

    cal.set(Calendar.MONTH, Calendar.JUNE);
    str1 = fmt1.format(cal.getTime());
    assertEquals("2002-06-02T08:16:44.018Z", str1);

    cal = Calendar.getInstance(TimeZone.getTimeZone("Asia/Tehran"));
    cal.set(2002, Calendar.JANUARY, 2, 8, 16, 44);
    cal.set(Calendar.MILLISECOND, 18);
    fmt1.setTimeZone(TimeZone.getTimeZone("Asia/Tehran"));
    str1 = fmt1.format(cal.getTime());
    assertEquals("2002-01-02T08:16:44.018+03:30", str1);

}

From source file:org.dozer.converters.CalendarConverter.java

public Object convert(Class destClass, Object srcObj) {
    Calendar result = new GregorianCalendar();
    Class srcFieldClass = srcObj.getClass();
    // Convert from Date to Calendar
    if (java.util.Date.class.isAssignableFrom(srcFieldClass)) {
        result.setTime((java.util.Date) srcObj);
    }/*  w  w  w .ja v a2s . c om*/
    //  Convert from Calendar to Calendar
    else if (Calendar.class.isAssignableFrom(srcFieldClass)) {
        Calendar c = (Calendar) srcObj;
        result.setTime(c.getTime());
        result.setTimeZone(c.getTimeZone());
    } else if (XMLGregorianCalendar.class.isAssignableFrom(srcFieldClass)) {
        Calendar c = ((XMLGregorianCalendar) srcObj).toGregorianCalendar();
        result.setTime(c.getTime());
        result.setTimeZone(c.getTimeZone());
    }
    // String to Calendar
    else if (dateFormat != null && String.class.isAssignableFrom(srcFieldClass)) {
        try {
            result.setTime(new Date(dateFormat.parse((String) srcObj).getTime()));
        } catch (ParseException e) {
            throw new ConversionException("Unable to parse source object using specified date format", e);
        }
        // Default conversion
    } else {
        try {
            result.setTime(new Date(Long.parseLong(srcObj.toString())));
        } catch (NumberFormatException e) {
            throw new ConversionException("Unable to determine time in millis of source object", e);
        }
    }
    return result;
}

From source file:com.github.dozermapper.core.converters.CalendarConverter.java

public Object convert(Class destClass, Object srcObj) {
    Calendar result = new GregorianCalendar();
    Class srcFieldClass = srcObj.getClass();
    // Convert from Date to Calendar
    if (java.util.Date.class.isAssignableFrom(srcFieldClass)) {
        result.setTime((java.util.Date) srcObj);
    } else if (Calendar.class.isAssignableFrom(srcFieldClass)) {
        //  Convert from Calendar to Calendar
        Calendar c = (Calendar) srcObj;
        result.setTime(c.getTime());/*  w w w . j  a  va2s.  c o  m*/
        result.setTimeZone(c.getTimeZone());
    } else if (XMLGregorianCalendar.class.isAssignableFrom(srcFieldClass)) {
        Calendar c = ((XMLGregorianCalendar) srcObj).toGregorianCalendar();
        result.setTime(c.getTime());
        result.setTimeZone(c.getTimeZone());
    } else if (dateFormat != null && String.class.isAssignableFrom(srcFieldClass)) {
        // String to Calendar
        try {
            result.setTime(new Date(dateFormat.parse((String) srcObj).getTime()));
        } catch (ParseException e) {
            throw new ConversionException("Unable to parse source object using specified date format", e);
        }
    } else {
        // Default conversion
        try {
            result.setTime(new Date(Long.parseLong(srcObj.toString())));
        } catch (NumberFormatException e) {
            throw new ConversionException("Unable to determine time in millis of source object", e);
        }
    }

    // Calendar to String
    if (dateFormat != null && String.class.isAssignableFrom(destClass)) {
        return dateFormat.format(result.getTime());
    }

    return result;
}

From source file:com.github.dozermapper.core.converters.XMLGregorianCalendarConverter.java

/**
 * {@inheritDoc}/*from  w  w  w .  ja  v a  2s.  c om*/
 */
public Object convert(Class destClass, Object srcObj) {
    Class sourceClass = srcObj.getClass();
    Calendar result = new GregorianCalendar();

    if (java.util.Date.class.isAssignableFrom(sourceClass)) {
        // Date --> XMLGregorianCalendar
        result.setTime(java.util.Date.class.cast(srcObj));
    } else if (Calendar.class.isAssignableFrom(sourceClass)) {
        // Calendar --> XMLGregorianCalendar
        Calendar c = Calendar.class.cast(srcObj);
        result.setTime(c.getTime());
        result.setTimeZone(c.getTimeZone());
    } else if (XMLGregorianCalendar.class.isAssignableFrom(sourceClass)) {
        result = XMLGregorianCalendar.class.cast(srcObj).toGregorianCalendar();
    } else if (dateFormat != null && String.class.isAssignableFrom(sourceClass)) {
        if ("".equals(String.class.cast(srcObj))) {
            return null;
        }

        try {
            long time = dateFormat.parse(String.class.cast(srcObj)).getTime();
            result.setTime(new Date(time));
        } catch (ParseException e) {
            throw new ConversionException("Unable to parse source object using specified date format", e);
        }
    } else {
        try {
            long time = Long.parseLong(srcObj.toString());
            result.setTime(new Date(time));
        } catch (NumberFormatException e) {
            throw new ConversionException("Unable to determine time in millis of source object", e);
        }
    }

    if (dateFormat != null && String.class.isAssignableFrom(destClass)) {
        return dateFormat.format(result.getTime());
    }

    return dataTypeFactory().newXMLGregorianCalendar(GregorianCalendar.class.cast(result));
}

From source file:com.stratelia.webactiv.util.DateUtilTest.java

@Test
public void testAddDays() {
    Calendar calend = Calendar.getInstance();
    calend.set(Calendar.DATE, 27);
    calend.set(Calendar.MONTH, Calendar.JUNE);
    calend.set(Calendar.YEAR, 2012);
    calend.setTimeZone(TimeZone.getTimeZone("GMT+01"));
    assertThat(calend.get(Calendar.DAY_OF_WEEK), is(Calendar.WEDNESDAY));
    DateUtil.addDaysExceptWeekEnds(calend, 2);
    assertThat(calend.get(Calendar.DAY_OF_WEEK), is(Calendar.FRIDAY));
    assertThat(calend.get(Calendar.DATE), is(29));
    assertThat(calend.get(Calendar.MONTH), is(Calendar.JUNE));
    assertThat(calend.get(Calendar.YEAR), is(2012));

    calend = Calendar.getInstance();
    calend.set(Calendar.DATE, 27);
    calend.set(Calendar.MONTH, Calendar.JUNE);
    calend.set(Calendar.YEAR, 2012);
    calend.setTimeZone(TimeZone.getTimeZone("GMT+01"));
    assertThat(calend.get(Calendar.DAY_OF_WEEK), is(Calendar.WEDNESDAY));
    DateUtil.addDaysExceptWeekEnds(calend, 4);
    assertThat(calend.get(Calendar.DAY_OF_WEEK), is(Calendar.TUESDAY));
    assertThat(calend.get(Calendar.DATE), is(3));
    assertThat(calend.get(Calendar.MONTH), is(Calendar.JULY));
    assertThat(calend.get(Calendar.YEAR), is(2012));
}

From source file:captureplugin.drivers.dreambox.connector.DreamboxConnector.java

/**
 * Remove a recording from the Dreambox/*from w  w w  .  j  a va2  s  .  co  m*/
 * 
 * @param dreamboxChannel
 *          the DreamboxChannel for the Program
 * @param prgTime
 *          ProgramTime to remove @return true, if successful
 * @param timezone
 *          Timezone to use for recording
 * @return True, if successful
 */
public boolean removeRecording(DreamboxChannel dreamboxChannel, ProgramTime prgTime, TimeZone timezone) {
    if (!mConfig.hasValidAddress()) {
        return false;
    }
    try {
        Calendar start = prgTime.getStartAsCalendar();
        start.setTimeZone(timezone);

        Calendar end = prgTime.getEndAsCalendar();
        end.setTimeZone(timezone);

        String shortInfo = prgTime.getProgram().getShortInfo();
        if (shortInfo == null) {
            shortInfo = "";
        }

        InputStream stream = openStreamForLocalUrl(
                "/web/tvbrowser?&command=del&action=0" + "&syear=" + start.get(Calendar.YEAR) + "&smonth="
                        + (start.get(Calendar.MONTH) + 1) + "&sday=" + start.get(Calendar.DAY_OF_MONTH)
                        + "&shour=" + start.get(Calendar.HOUR_OF_DAY) + "&smin=" + start.get(Calendar.MINUTE) +

                        "&eyear=" + end.get(Calendar.YEAR) + "&emonth=" + (end.get(Calendar.MONTH) + 1)
                        + "&eday=" + end.get(Calendar.DAY_OF_MONTH) + "&ehour=" + end.get(Calendar.HOUR_OF_DAY)
                        + "&emin=" + end.get(Calendar.MINUTE) +

                        "&sRef="
                        + URLEncoder.encode(dreamboxChannel.getName() + "|" + dreamboxChannel.getReference(),
                                "UTF8")
                        + "&name=" + URLEncoder.encode(prgTime.getProgram().getTitle(), "UTF8")
                        + "&description=" + URLEncoder.encode(shortInfo, "UTF8") +

                        "&afterevent=0&eit=&disabled=0&justplay=0&repeated=0");

        SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
        DreamboxStateHandler handler = new DreamboxStateHandler();
        saxParser.parse(stream, handler);
        return Boolean.valueOf(handler.getState());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }
    return false;
}