Example usage for java.text DateFormat setCalendar

List of usage examples for java.text DateFormat setCalendar

Introduction

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

Prototype

public void setCalendar(Calendar newCalendar) 

Source Link

Document

Set the calendar to be used by this date format.

Usage

From source file:org.exoplatform.platform.portlet.juzu.calendar.CalendarPortletController.java

@Ajax
@Resource/*from  w w w.j a  v a 2 s.c  o m*/
public Response.Content calendarHome() throws Exception {

    displayedCalendar.clear();
    displayedCalendarMap.clear();
    tasksDisplayedList.clear();
    eventsDisplayedList.clear();
    String date_act = null;
    String username = CalendarPortletUtils.getCurrentUser();
    Locale locale = Util.getPortalRequestContext().getLocale();
    String dp = formatDate(locale);
    DateFormat d = new SimpleDateFormat(dp);
    DateFormat dTimezone = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    dTimezone.setCalendar(CalendarPortletUtils.getCurrentCalendar());
    Long date = new Date().getTime();
    int clickNumber = Integer.parseInt(nbclick);
    if (clickNumber != 0)
        date = incDecJour(date, clickNumber);
    Date currentTime = new Date(date);
    // Get Calendar object set to the date and time of the given Date object
    Calendar cal = CalendarPortletUtils.getCurrentCalendar();
    cal.setTime(currentTime);

    // Set time fields to zero
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    // Put it back in the Date object
    currentTime = cal.getTime();
    date_act = d.format(currentTime);
    Date comp = currentTime;
    String defaultCalendarLabel = "Default";
    String dateLabel = "";
    try {
        ResourceBundle rs = ResourceBundle.getBundle("locale/portlet/calendar/calendar", locale);
        defaultCalendarLabel = EntityEncoder.FULL.encode(rs.getString("UICalendars.label.defaultCalendarId"));
        if (clickNumber == 0)
            dateLabel = rs.getString("today.label") + ": ";
        else if (clickNumber == -1)
            dateLabel = rs.getString("yesterday.label") + ": ";
        else if (clickNumber == 1)
            dateLabel = rs.getString("tomorrow.label") + ": ";
        else
            dateLabel = "";
    } catch (MissingResourceException ex) {
        if (clickNumber == 0)
            dateLabel = "today.label" + ": ";
        else if (clickNumber == -1)
            dateLabel = "yesterday.label" + ": ";
        else if (clickNumber == 1)
            dateLabel = "tomorrow.label" + ": ";
        else
            dateLabel = "";
    }

    EntityEncoder.FULL.encode(dateLabel);
    dateLabel = new StringBuffer(dateLabel).append(date_act).toString();
    if (nonDisplayedCalendarList == null) {
        SettingValue settingNode = settingService_.get(Context.USER, Scope.APPLICATION,
                CalendarPortletUtils.HOME_PAGE_CALENDAR_SETTINGS);
        if ((settingNode != null) && (settingNode.getValue().toString().split(":").length == 2)) {
            nonDisplayedCalendarList = settingNode.getValue().toString().split(":")[1].split(",");
        }
    }
    List<CalendarEvent> userEvents = getEvents(username, cal);
    if ((userEvents != null) && (!userEvents.isEmpty())) {
        Iterator itr = userEvents.iterator();
        while (itr.hasNext()) {
            CalendarEvent event = (CalendarEvent) itr.next();
            Date from = d.parse(dTimezone.format(event.getFromDateTime()));
            Date to = d.parse(dTimezone.format(event.getToDateTime()));
            if ((event.getEventType().equals(CalendarEvent.TYPE_EVENT))
                    && (from.compareTo(d.parse(dTimezone.format(comp))) <= 0)
                    && (to.compareTo(d.parse(dTimezone.format(comp))) >= 0)) {
                if (!CalendarPortletUtils.contains(nonDisplayedCalendarList, event.getCalendarId())) {
                    org.exoplatform.calendar.service.Calendar calendar = calendarService_
                            .getUserCalendar(username, event.getCalendarId());
                    if (calendar == null) {
                        calendar = calendarService_.getGroupCalendar(event.getCalendarId());
                    }
                    if (calendar.getGroups() == null) {
                        if (calendar.getId().equals(Utils.getDefaultCalendarId(username))
                                && calendar.getName().equals(NewUserListener.defaultCalendarName)) {
                            calendar.setName(defaultCalendarLabel);
                        }
                    }
                    eventsDisplayedList.add(event);
                    if (!displayedCalendarMap.containsKey(calendar.getId())) {
                        displayedCalendarMap.put(calendar.getId(), calendar);
                        displayedCalendar.add(calendar);
                    }
                }
            } else if ((event.getEventType().equals(CalendarEvent.TYPE_TASK))
                    && (((from.compareTo(comp) <= 0) && (to.compareTo(comp) >= 0))
                            || ((event.getEventState().equals(CalendarEvent.NEEDS_ACTION))
                                    && (to.compareTo(comp) < 0)))) {
                tasksDisplayedList.add(event);
            }
        }
        Collections.sort(eventsDisplayedList, eventsComparator);
        Collections.sort(tasksDisplayedList, tasksComparator);
    }
    return calendar.with().set("displayedCalendar", displayedCalendar)
            .set("calendarDisplayedMap", displayedCalendarMap).set("eventsDisplayedList", eventsDisplayedList)
            .set("tasksDisplayedList", tasksDisplayedList).set("date_act", dateLabel).ok()
            .withCharset(Tools.UTF_8);
}

From source file:org.fornax.utilities.xtendtools.lib.UtilsExtension.java

/**
 * Returns the current time stamp in ISO 8601 format and Z (Zulu) time zone.
 */// w  w  w.j a v a2 s. c o  m
public static String getTimeStamp() {
    final Calendar c = new GregorianCalendar(TimeZone.getTimeZone(TIMEZONE_UTC));
    final DateFormat df = new SimpleDateFormat(DATEFORMAT_ISO8601);
    df.setCalendar(c);
    return StringUtils.replace(df.format(c.getTime()), TIMEZONE_UTC, TIMEZONE_Z);
}

From source file:org.fornax.utilities.xtendtools.lib.UtilsExtension.java

/**
 * Returns the current time stamp.// w  ww .ja  v  a 2  s  .c om
 */
public static String getTimeStamp(final String pattern) {
    final Calendar c = new GregorianCalendar();
    final DateFormat df = new SimpleDateFormat(pattern, Locale.US);
    df.setCalendar(c);
    return df.format(c.getTime());
}