Example usage for java.text DateFormat setTimeZone

List of usage examples for java.text DateFormat setTimeZone

Introduction

In this page you can find the example usage for java.text DateFormat setTimeZone.

Prototype

public void setTimeZone(TimeZone zone) 

Source Link

Document

Sets the time zone for the calendar of this DateFormat object.

Usage

From source file:com.adobe.communities.ugc.migration.importer.UGCImportHelper.java

protected void extractEvent(final JsonParser jsonParser, final Resource resource) throws IOException {

    String author = null;//from   ww w .  java  2s  .c o  m
    final JSONObject requestParams = new JSONObject();
    try {
        jsonParser.nextToken();
        JsonToken token = jsonParser.getCurrentToken();
        while (token.equals(JsonToken.FIELD_NAME)) {
            String field = jsonParser.getCurrentName();
            jsonParser.nextToken();

            if (field.equals(PN_START) || field.equals(PN_END)) {
                final Long value = jsonParser.getValueAsLong();
                final Calendar calendar = new GregorianCalendar();
                calendar.setTimeInMillis(value);
                final Date date = calendar.getTime();
                final TimeZone tz = TimeZone.getTimeZone("UTC");
                // this is the ISO-8601 format expected by the CalendarOperations object
                final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm+00:00");
                df.setTimeZone(tz);
                if (field.equals(PN_START)) {
                    requestParams.put(CalendarRequestConstants.START_DATE, df.format(date));
                } else {
                    requestParams.put(CalendarRequestConstants.END_DATE, df.format(date));
                }
            } else if (field.equals(CalendarRequestConstants.TAGS)) {
                List<String> tags = new ArrayList<String>();
                if (jsonParser.getCurrentToken().equals(JsonToken.START_ARRAY)) {
                    token = jsonParser.nextToken();
                    while (!token.equals(JsonToken.END_ARRAY)) {
                        tags.add(URLDecoder.decode(jsonParser.getValueAsString(), "UTF-8"));
                        token = jsonParser.nextToken();
                    }
                    requestParams.put(CalendarRequestConstants.TAGS, tags);
                } else {
                    LOG.warn("Tags field came in without an array of tags in it - not processed");
                    // do nothing, just log the error
                }
            } else if (field.equals("jcr:createdBy")) {
                author = URLDecoder.decode(jsonParser.getValueAsString(), "UTF-8");
            } else if (field.equals(ContentTypeDefinitions.LABEL_TIMESTAMP_FIELDS)) {
                jsonParser.skipChildren(); // we do nothing with these because they're going to be put into json
            } else {
                try {
                    final String value = URLDecoder.decode(jsonParser.getValueAsString(), "UTF-8");
                    requestParams.put(field, value);
                } catch (NullPointerException e) {
                    throw e;
                }
            }
            token = jsonParser.nextToken();
        }
    } catch (final JSONException e) {
        throw new IOException("Unable to build a JSON object with the inputs provided", e);
    }
    try {
        Map<String, Object> eventParams = new HashMap<String, Object>();
        eventParams.put("event", requestParams.toString());
        calendarOperations.create(resource, author, eventParams, null,
                resource.getResourceResolver().adaptTo(Session.class));
    } catch (final OperationException e) {
        //probably caused by creating a folder that already exists. We ignore it, but still log the event.
        LOG.info("There was an operation exception while creating an event");
    }
}

From source file:com.citrix.cpbm.portal.fragment.controllers.AbstractHomeController.java

private void fetchEvents(User user, ModelMap map) {
    // get alerts
    List<Event> alertsForToday = new ArrayList<Event>();
    List<Event> alertsForYesterday = new ArrayList<Event>();

    try {//from  w w w .j a va2s . c o  m
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        if (user.getTimeZone() != null) {
            formatter.setTimeZone(TimeZone.getTimeZone(user.getTimeZone()));
        }
        String date = formatter.format(new Date());
        alertsForToday = eventService.showEvents(user, date, 1, 2, null, null, null, null, null, false);
        if (alertsForToday.size() > 2) {
            map.addAttribute("alerts_for_today", alertsForToday.subList(0, 2));
        } else {
            map.addAttribute("alerts_for_today", alertsForToday);
        }
    } catch (Exception e) {
        logger.error(e);
    }

    try {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        if (user.getTimeZone() != null) {
            formatter.setTimeZone(TimeZone.getTimeZone(user.getTimeZone()));
        }
        String date = formatter.format(DateUtils.minusOneDay(new Date()));
        alertsForYesterday = eventService.showEvents(user, date, 1, 1, null, null, null, null, null, false);

        if (alertsForYesterday.size() > 1) {
            map.addAttribute("alerts_for_yesterday", alertsForYesterday.subList(0, 1));
        } else {
            map.addAttribute("alerts_for_yesterday", alertsForYesterday);
        }
    } catch (Exception e) {
        logger.error(e);
    }
}

From source file:org.sakaiproject.tool.summarycalendar.ui.CalendarBean.java

public void selectDate(ActionEvent e) {
    try {/*from   www .j  a  v  a2 s .  c o m*/
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        Map paramMap = context.getRequestParameterMap();
        String dateStr = (String) paramMap.get("selectedDay");
        DateFormat df = new SimpleDateFormat(msgs.getString("date_link_format"), msgs.getLocale());
        df.setTimeZone(getCurrentUserTimezone());
        selectedDay = df.parse(dateStr);
        selectedEventRef = null;
        updateEventList = true;
    } catch (Exception ex) {
        LOG.error("Error getting selectedDate:" + ex.toString());
    }
}

From source file:org.dasein.cloud.azure.storage.BlobStore.java

private @Nullable Blob toBlob(@Nonnull String regionId, @Nullable Node node, @Nonnull String bucket,
        boolean isContainer) {
    if (node == null) {
        return null;
    }/*  www.j  a v a 2  s.co  m*/
    NodeList attributes = node.getChildNodes();
    String object = null, location = null;
    long size = -1L, creationDate = 0L;

    for (int i = 0; i < attributes.getLength(); i++) {
        Node attribute = attributes.item(i);

        if (attribute.getNodeType() == Node.TEXT_NODE) {
            continue;
        }
        String aname = attribute.getNodeName().toLowerCase();
        String value;

        if (attribute.getChildNodes().getLength() > 0) {
            value = attribute.getFirstChild().getNodeValue();
        } else {
            continue;
        }
        if (aname.equalsIgnoreCase("Name")) {
            object = value;
        } else if (aname.equalsIgnoreCase("Url")) {
            location = value;
        } else if (aname.equalsIgnoreCase("Properties")) {
            NodeList propertyAttributes = attribute.getChildNodes();

            for (int j = 0; j < propertyAttributes.getLength(); j++) {
                Node property = propertyAttributes.item(j);
                String propertyName = property.getNodeName();
                String propertyValue;

                if (property.getChildNodes().getLength() > 0) {
                    propertyValue = property.getFirstChild().getNodeValue();
                } else {
                    continue;
                }

                if (propertyName.equalsIgnoreCase("Content-Length")) {
                    size = Long.valueOf(propertyValue);
                } else if (propertyName.equalsIgnoreCase("Last-Modified")) {
                    String RFC1123_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z";
                    DateFormat rfc1123Format = new SimpleDateFormat(RFC1123_PATTERN);

                    rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
                    try {
                        creationDate = rfc1123Format.parse(propertyValue).getTime();
                    } catch (ParseException e) {
                        logger.warn("Invalid date: " + propertyValue);
                    }
                }
            }
        }
    }
    if (isContainer) {
        return Blob.getInstance(regionId, location, object, creationDate);
    } else {
        return Blob.getInstance(regionId, location, bucket, object, creationDate,
                new Storage<Byte>(size, Storage.BYTE));
    }
}

From source file:org.springframework.cloud.stream.app.http.source.DefaultMixedCaseContentTypeHttpHeaderMapper.java

private String formatDate(long date) {
    DateFormat dateFormat = new SimpleDateFormat(DATE_FORMATS[0], Locale.US);
    dateFormat.setTimeZone(GMT);
    return dateFormat.format(new Date(date));
}

From source file:com.forrestguice.suntimeswidget.SuntimesUtils.java

/**
 * formats a time display string (lets the system determine the exact format).
 *
 * @param context a context/*from  w  ww  . java  2  s . c om*/
 * @param cal     a Calendar representing some point in time
 * @return a time display string (short format)
 */
public TimeDisplayText calendarTimeSysDisplayString(Context context, @NonNull Calendar cal) {
    DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(context);
    timeFormat.setTimeZone(cal.getTimeZone());
    TimeDisplayText retValue = new TimeDisplayText(timeFormat.format(cal.getTime()), "", "");
    retValue.setRawValue(cal.getTimeInMillis());
    return retValue;
}

From source file:microsoft.exchange.webservices.data.ExchangeServiceBase.java

/**
 * Converts the date time to universal date time string.
 *
 * @param dt the date/*from w w  w.j  a  v  a 2  s. co  m*/
 * @return String representation of DateTime in yyyy-MM-ddTHH:mm:ssZ format.
 */
protected String convertDateTimeToUniversalDateTimeString(Date dt) {
    String utcPattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    DateFormat utcFormatter = new SimpleDateFormat(utcPattern);
    utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    return utcFormatter.format(dt);
}

From source file:org.springframework.cloud.stream.app.http.source.DefaultMixedCaseContentTypeHttpHeaderMapper.java

private long getFirstDate(String headerValue, String headerName) {
    for (String dateFormat : DATE_FORMATS) {
        DateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, Locale.US);
        simpleDateFormat.setTimeZone(GMT);
        try {/*www .j av  a2 s.  c  om*/
            return simpleDateFormat.parse(headerValue).getTime();
        } catch (ParseException e) {
            // ignore
        }
    }
    throw new IllegalArgumentException(
            "Cannot parse date value '" + headerValue + "' for '" + headerName + "' header");
}

From source file:org.apache.asterix.external.classad.BuiltinClassAdFunctions.java

public static void make_formatted_time(ClassAdTime time_components, String format, Value result) {
    //replace c++ format elements with java elements
    format = format.replace("%m", "MM");
    format = format.replace("%d", "dd");
    format = format.replace("%Y", "yyyy");
    format = format.replace("%M", "mm");
    format = format.replace("%S", "ss");
    format = format.replace("%A", "EEEE");
    format = format.replace("%a", "EEE");
    format = format.replace("%B", "MMMM");
    format = format.replace("%b", "MMM");
    format = format.replace("%H", "HH");
    format = format.replace("%Y", "y");
    format = format.replace("%", "");
    DateFormat df = new SimpleDateFormat(format);
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
    result.setStringValue(df.format(time_components.getCalendar().getTime()));
}