List of usage examples for org.joda.time DateTimeZone forTimeZone
public static DateTimeZone forTimeZone(TimeZone zone)
From source file:se.vgregion.portal.notes.calendar.controllers.NotesCalendarViewController.java
License:Open Source License
/** * Displays the calendar events for the logged in user. * * @param model the model/*from w w w . j av a 2 s. c om*/ * @param request the portletRequest * @param response the portletResponse * @return the view to display */ @RenderMapping public String displayCalendarEvents(ModelMap model, RenderRequest request, RenderResponse response) { // It would seem that Joda Times DateTimeZone does not use Sun:s TimeZone.getDefault (user.timezone) as its // often claims but defaults to timezone UTC. So therefor we do this: DateTimeZone.setDefault(DateTimeZone.forTimeZone(TimeZone.getDefault())); // ... getting the value set in the java_opt - setting it as default for JT. String userId = portletData.getUserId(request); LOGGER.debug("Userid: {}", userId); CalendarEventsPeriod displayPeriod = (CalendarEventsPeriod) model.get(displayPeriodKey); if (displayPeriod == null) { DateTime startDate = new DateTime().withDayOfWeek(DateTimeConstants.MONDAY).withHourOfDay(0) .withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0); displayPeriod = new CalendarEventsPeriod(startDate, CalendarEventsPeriod.DEFAULT_PERIOD_LENGTH); model.put(displayPeriodKey, displayPeriod); } try { Map<String, Future<CalendarEvents>> futureCalendarEvents = new HashMap<String, Future<CalendarEvents>>(); // Initialize CalendarEvents CalendarEvents events = new CalendarEvents(); events.setCalendarItems(new ArrayList<CalendarItem>()); // Retrieve asynchronously futureCalendarEvents.put("VGR", calendarService.getFutureCalendarEvents(userId, displayPeriod)); // Get from Google, asynchronously // String selectedCalendars = request.getPreferences().getValue(this.SELECTED_GOOGLE_CALENDARS, ""); // List<String> selectedCalendarsList = Arrays.asList(stringToArray(selectedCalendars)); // futureCalendarEvents.put("Google", googleCalendarService.getFutureCalendarEvents(userId, displayPeriod, // selectedCalendarsList)); // Get from other sources, asynchronously. Map<String, String> externalSources = getExternalSources(request.getPreferences()); for (Map.Entry<String, String> externalSource : externalSources.entrySet()) { futureCalendarEvents.put(externalSource.getKey(), calendarService.getFutureCalendarEventsFromIcalUrl(externalSource.getValue(), displayPeriod, externalSource.getKey())); } // Now that we have a list of Future objects which all are processed concurrently we start to "get()" them. List<String> failedRetrievals = new ArrayList<String>(); for (Map.Entry<String, Future<CalendarEvents>> futureCalendarEvent : futureCalendarEvents.entrySet()) { try { Future<CalendarEvents> value = futureCalendarEvent.getValue(); CalendarEvents calendarEvents = value.get(15, TimeUnit.SECONDS); if (calendarEvents != null) { List<CalendarItem> calendarItems = calendarEvents.getCalendarItems(); if (calendarItems != null) { events.getCalendarItems().addAll(calendarItems); } } } catch (Exception ex) { if (userId.equals("lifra1")) { LOGGER.warn("Failed to get a calendar for user " + userId + ". " + ex.getMessage()); } else { LOGGER.warn("Failed to get a calendar for user " + userId + ". " + ex.getMessage(), ex); } failedRetrievals.add(futureCalendarEvent.getKey()); } } if (failedRetrievals.size() > 0) { String errorMessage = "Fljande hmtningar misslyckades: " + StringUtils.arrayToCommaDelimitedString(failedRetrievals.toArray()) + "."; model.addAttribute("errorMessage", errorMessage); } List<List<CalendarItem>> calendarItems = events.getCalendarItemsGroupedByStartDate(); model.put("displayPeriodText", getFormattedDateIntervalToTitle(displayPeriod, locale)); model.put("calendarItems", calendarItems); model.put("randomNumber", random.nextInt()); return VIEW; } catch (RuntimeException ex) { LOGGER.error(ex.getMessage(), ex); return NO_CALENDAR_VIEW; } catch (Exception ex) { LOGGER.error(ex.getMessage(), ex); return NO_CALENDAR_VIEW; } }
From source file:ucar.unidata.idv.ui.JythonShell.java
License:Open Source License
/** * Generate a timestamp that is formatted according to the user's * preferences.//from w w w . j a v a 2 s . c o m * * <p><b>NOTE</b>: if the preferred date and time format is * {@link IdvConstants#DEFAULT_DATE_FORMAT} and the preferred time zone * is {@link IdvConstants#DEFAULT_TIMEZONE}, the resulting timestamp will * end in {@literal "Z"}, rather than {@literal "GMT"}, in an effort to * obey the IDV's behavior. Example result: {@code 2014-08-11 21:16:29Z}</p> * * @return Timestamp that conforms to the user's date, time, and time zone * preferences. */ private String getTimestamp() { String format = idv.getPreferenceManager().getDefaultDateFormat(); TimeZone javaTz = idv.getPreferenceManager().getDefaultTimeZone(); DateTimeZone tz = DateTimeZone.forTimeZone(javaTz); DateTime now = DateTime.now(tz); String timestamp; if (IdvConstants.DEFAULT_DATE_FORMAT.equals(format) && IdvConstants.DEFAULT_TIMEZONE.equals(javaTz.getID())) { format = format.substring(0, format.length() - 2); timestamp = now.toString(format).trim() + 'Z'; } else { timestamp = now.toString(format); } return timestamp; }
From source file:web.StreamMeetupComTask.java
License:Apache License
private String getUpfrontReservationTime(Long dateInMillis) { DateTime now = new DateTime(DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC"))); DateTime event = new DateTime(dateInMillis, DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC"))); Duration duration = new Duration(now, event); if (duration.getMillis() < 0) { return "-1"; } else if (duration.getStandardSeconds() < 86400) { return "1d"; } else if (duration.getStandardDays() < 4) { return "4d"; } else if (duration.getStandardDays() < 7) { return "1w"; } else if (duration.getStandardDays() < 14) { return "2w"; } else if (duration.getStandardDays() < 28) { return "4w"; } else if (duration.getStandardDays() < 56) { return "8w"; } else {// w w w. j ava 2 s . c o m return "-"; } }