TimeService
Sakai Framework provides a component named org.sakaiproject.time.api.TimeService that gives you user preferred TimeZone depending on User Preferences Tool settings. What TimeService does is the following (see BasicTimeService.java):
protected String\[\] getUserTimezoneLocale() { // Check if we already cached this user's timezone String userId = sessionManager.getCurrentSessionUserId(); if (userId == null) return M_tz_locale_default; String\[\] timeZoneLocale = (String\[\]) M_userTzCache.get(userId); if (timeZoneLocale \!= null) return timeZoneLocale; // Otherwise, get the user's preferred time zone Preferences prefs = preferencesService.getPreferences(userId); ResourceProperties tzProps = prefs.getProperties(TimeService.APPLICATION_ID); String timeZone = tzProps.getProperty(TimeService.TIMEZONE_KEY); if (timeZone == null \|\| timeZone.equals("")) timeZone = TimeZone.getDefault().getID(); // Now, get user's prefered locale String localeId = rl.getLocale().toString(); timeZoneLocale = new String\[\]{timeZone, localeId}; M_userTzCache.put(userId, timeZoneLocale); return timeZoneLocale; }
As you can see, TimeService ask PreferencesService for user preferences and returns the right TimeZone. Once you have the TimeZone you can format dates using java.text.DateFormat.