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:saschpe.birthdays.service.CalendarSyncService.java

private static ContentProviderOperation insertEvent(Context context, long calendarId, Date eventDate, int year,
        String title, String description, String lookupKey) {
    ContentProviderOperation.Builder builder = ContentProviderOperation
            .newInsert(getCalendarUri(context, CalendarContract.Events.CONTENT_URI));

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(eventDate);//from w  ww . j  a v  a  2 s .c o m
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.HOUR, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));

    /* Define over entire day.
     *
     * Note: ALL_DAY is enough on original Android calendar, but some calendar apps (Business
     * Calendar) do not display the event if time between dtstart and dtend is 0
     */
    final long dtstart = calendar.getTimeInMillis();
    final long dtend = dtstart + DateUtils.DAY_IN_MILLIS;

    builder.withValue(CalendarContract.Events.CALENDAR_ID, calendarId);
    builder.withValue(CalendarContract.Events.DTSTART, dtstart);
    builder.withValue(CalendarContract.Events.DTEND, dtend);
    builder.withValue(CalendarContract.Events.EVENT_TIMEZONE, Time.TIMEZONE_UTC);
    builder.withValue(CalendarContract.Events.ALL_DAY, 1);
    builder.withValue(CalendarContract.Events.TITLE, title);
    builder.withValue(CalendarContract.Events.DESCRIPTION, description);
    builder.withValue(CalendarContract.Events.STATUS, CalendarContract.Events.STATUS_CONFIRMED);

    /* Enable reminders for this event
     * Note: Need to be explicitly set on Android < 4 to enable reminders
     */
    builder.withValue(CalendarContract.Events.HAS_ALARM, 1);

    // Set availability to free.
    if (Build.VERSION.SDK_INT >= 14) {
        builder.withValue(CalendarContract.Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_FREE);
    }
    // Add button to open contact
    if (Build.VERSION.SDK_INT >= 16 && lookupKey != null) {
        builder.withValue(CalendarContract.Events.CUSTOM_APP_PACKAGE, context.getPackageName());
        final Uri contactLookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI,
                lookupKey);
        builder.withValue(CalendarContract.Events.CUSTOM_APP_URI, contactLookupUri.toString());
    }

    return builder.build();
}

From source file:org.floggy.synchronization.jme.core.impl.JSONSerializationManager.java

/**
* DOCUMENT ME!/* ww  w. j a  va  2 s  .c om*/
*
* @param name DOCUMENT ME!
* @param jsonObject DOCUMENT ME!
*
* @return DOCUMENT ME!
*
* @throws JSONException DOCUMENT ME!
*/
public static Calendar receiveCalendar(String name, JSONObject jsonObject) throws JSONException {
    Calendar value = null;
    JSONObject calendar = jsonObject.optJSONObject(name);

    if (calendar != null) {
        value = Calendar.getInstance();
        value.setTimeZone(TimeZone.getTimeZone(calendar.getJSONObject("timeZone").getString("ID")));
        value.setTime(new Date(calendar.getLong("time")));
    }

    return value;
}

From source file:com.neuron.trafikanten.HelperFunctions.java

public static Long jsonToDate(String jsonString) {
    Calendar calendar = Calendar.getInstance();
    int indexOfPlus = jsonString.indexOf("+");
    int startOfDate = 5;
    if (jsonString.startsWith("/")) {
        // LEGACY : parse /Date( strings : Some date strings are /Date(253402297140000+0100)/, new ones are Date(253402297140000+0100)
        startOfDate = 6;/*w  w  w.  j  ava2 s . c  om*/
        calendar.setTimeInMillis(Long.parseLong(
                jsonString.substring(startOfDate, indexOfPlus > 0 ? indexOfPlus : jsonString.length() - 2)));
    } else {
        calendar.setTimeInMillis(Long.parseLong(
                jsonString.substring(startOfDate, indexOfPlus > 0 ? indexOfPlus : jsonString.length() - 1)));
    }
    if (indexOfPlus > -1) {
        calendar.setTimeZone(
                TimeZone.getTimeZone("GMT-" + jsonString.substring(indexOfPlus, indexOfPlus + 3) + ":00"));
    }
    return calendar.getTimeInMillis();
}

From source file:it.geosolutions.geobatch.imagemosaic.ImageMosaicUpdater.java

/**
 * Update a time attrib.//  w ww.jav  a2  s. co  m
 * <P/>
 * TODO: use the proper SPI for parsing the extracted strings
 */
private static void updateFeatureTimeAttrib(SimpleFeature feature, File baseDir, String propName,
        Properties indexerProps, String granuleName) throws IOException, ParseException {

    // find regex file
    String propertyCollectors = indexerProps.getProperty("PropertyCollectors");
    String regexname = null;
    for (String coll : propertyCollectors.split(",")) {
        Matcher m = PROP_COLLECTOR_PATTERN.matcher(coll);
        if (m.matches()) {
            //                String spi = m.group(1);
            String rex = m.group(2);
            String pname = m.group(3);
            if (pname.equals(propName)) {
                regexname = rex;
                break;
            }
        } else {
            LOGGER.error("Error in indexer: Collector not parsable: " + coll);
            continue;
        }
    }

    if (regexname == null) {
        LOGGER.error("Error in indexer: regex not found for attribute: " + propName);
        return;
    }

    final File regexFile = new File(baseDir, regexname + ".properties");
    final Properties regexProps = ImageMosaicProperties.getPropertyFile(regexFile);
    String regex = regexProps.getProperty("regex");
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("regex for prop " + propName + ": --->" + regex + "<--");
    }
    final Pattern pattern = Pattern.compile(regex);
    final Matcher matcher = pattern.matcher(granuleName);
    if (matcher.find()) {
        String matchedGroup = matcher.group();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("time regex is matching: [" + matchedGroup + "]");
        }
        //                  List<Date> dates = TimeParser.parse(matchedGroup);
        TimeParser timeParser = new TimeParser();
        List<Date> dates = timeParser.parse(matchedGroup);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("TimeParser parsed dates:" + dates);
        }

        if (dates != null && !dates.isEmpty()) {
            Calendar cal = Calendar.getInstance();
            cal.setTimeZone(TimeZone.getTimeZone("UTC"));
            cal.setTime(dates.get(0));

            feature.setAttribute(propName, cal.getTime());
        }
    } else {
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("time regex is not matching");
        }
    }
}

From source file:net.sourceforge.jcctray.model.CCNet.java

private SimpleDateFormat getDateFormatter(TimeZone timeZone) {
    SimpleDateFormat dateFormatter = new SimpleDateFormat("h:mm:ss a, dd MMM", LOCALE_US);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(timeZone);
    dateFormatter.setCalendar(calendar);
    return dateFormatter;
}

From source file:org.infoscoop.web.AccessStatsServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String rssUrl = request.getParameter("rssUrl");
    LogDAO dao = LogDAO.newInstance();/*from w ww.  jav  a 2  s . c o m*/

    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone("UTC"));
    cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 0, 0, 0);
    cal.add(Calendar.DATE, -1);
    int onedaycnt = dao.getRssAccessCount(rssUrl, dateFormat.format(cal.getTime()));
    cal.add(Calendar.DATE, -6);
    int oneweekcnt = dao.getRssAccessCount(rssUrl, dateFormat.format(cal.getTime()));
    cal.add(Calendar.DATE, 7);
    cal.add(Calendar.MONTH, -1);
    int onemonthcnt = dao.getRssAccessCount(rssUrl, dateFormat.format(cal.getTime()));
    cal.add(Calendar.MONTH, -5);
    int sixmonthcnt = dao.getRssAccessCount(rssUrl, dateFormat.format(cal.getTime()));
    int allcnt = dao.getRssAccessCount(rssUrl);
    request.setAttribute("onedaycnt", new Integer(onedaycnt));
    request.setAttribute("oneweekcnt", new Integer(oneweekcnt));
    request.setAttribute("onemonthcnt", new Integer(onemonthcnt));
    request.setAttribute("sixmonthcnt", new Integer(sixmonthcnt));
    request.setAttribute("allcnt", new Integer(allcnt));

    request.getRequestDispatcher("accessStats.jsp").forward(request, response);
}

From source file:org.carewebframework.vista.mbroker.HODate.java

/**
 * Sets the date to the value corresponding to the $H value.
 *
 * @param value $H-formatted date./*from w  ww  .j  a v  a2s  . co m*/
 */
public void setHODate(String value) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getDefault());
    String[] pcs = value.split("\\,", 2);
    long ms = (Long.parseLong(pcs[0]) - 47116) * 24 * 60 * 60 * 1000;
    cal.setTimeInMillis(ms);
    hasTime = pcs.length == 2;

    if (hasTime) {
        int frac = Integer.parseInt(pcs[1]);
        cal.set(Calendar.SECOND, frac % 60);
        frac /= 60;
        cal.set(Calendar.MINUTE, frac % 60);
        frac /= 60;
        cal.set(Calendar.HOUR, frac);
    }

    setTime(cal.getTimeInMillis());
}

From source file:org.lieuofs.commune.biz.GestionCommuneTest.java

@Test
public void rechercheLaTourDeTreme() {
    // numro OFS 2154 : la Tour de Trme a t incluse dans Bulle le 1er janvier 2006
    CommuneCritere critere = new CommuneCritere();
    critere.setCodeCanton("FR");
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone("Europe/Zurich"));
    cal.set(2005, Calendar.DECEMBER, 31);
    critere.setDateValidite(cal.getTime());
    List<ICommuneSuisse> communes = gestionnaire.rechercher(critere);
    boolean trouve = false;
    for (ICommuneSuisse commune : communes) {
        if (2154 == commune.getNumeroOFS()) {
            trouve = true;/*from   www  . j av a2 s  .co  m*/
            break;
        }
    }
    assertTrue("Le 31 dcembre 2005 la Tour-de-Trme est une commune fribourgeoise", trouve);
    cal.add(Calendar.DATE, 1);
    critere.setDateValidite(cal.getTime());
    communes = gestionnaire.rechercher(critere);
    trouve = false;
    for (ICommuneSuisse commune : communes) {
        if (2154 == commune.getNumeroOFS()) {
            trouve = true;
            break;
        }
    }
    assertTrue("Le 1er janvier 2006 la Tour-de-Trme n'est plus une commune fribourgeoise", !trouve);
}

From source file:com.boozallen.cognition.ingest.storm.bolt.enrich.SetDateBoltTest.java

@Test
public void testEnrichDataswift(@Mocked final LogRecord record) throws Exception {
    final Calendar calendar = Calendar.getInstance();
    calendar.clear();/*  w  w  w.ja  v  a2 s  .co  m*/
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    calendar.set(2014, Calendar.JUNE, 10, 20, 22, 4);

    bolt.dateFields = Arrays.asList("interaction.created_at");
    bolt.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z";

    new Expectations() {
        {
            record.getValue(bolt.dateFields.get(0));
            result = "Tue, 10 Jun 2014 20:22:04 +0000";
            record.setDate(calendar.getTime());
        }
    };
    bolt.process(record);
}

From source file:com.boozallen.cognition.ingest.storm.bolt.enrich.SetDateBoltTest.java

@Test
public void testEnrichGnip(@Mocked final LogRecord record) throws Exception {
    final Calendar calendar = Calendar.getInstance();
    calendar.clear();/*w  w w  .ja va  2 s  .  co  m*/
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    calendar.set(2015, Calendar.JULY, 12, 0, 0, 52);

    bolt.dateFields = Arrays.asList("postedTime");
    bolt.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSX";

    new Expectations() {
        {
            record.getValue(bolt.dateFields.get(0));
            result = "2015-07-12T00:00:52.000Z";
            record.setDate(calendar.getTime());
        }
    };
    bolt.process(record);
}