Jive Forums API (5.5.20.2-oracle) Developer Javadocs

com.jivesoftware.base.action.util
Class DateUtils

java.lang.Object
  extended by com.jivesoftware.base.action.util.DateUtils

public class DateUtils
extends java.lang.Object

A formatter for dates - uses a fast internal date formatter. Use this class when formatting dates in skins - it will honor user preferences for locales.


Field Summary
static java.lang.String DEFAULT_DATE_PATTERN
           
static java.lang.String DEFAULT_SHORT_DATE_PATTERN
           
static long ONE_DAY_IN_MILLIS
           
 
Constructor Summary
DateUtils(javax.servlet.http.HttpServletRequest request, User user)
           
 
Method Summary
protected static java.lang.String convertDateString(java.lang.String dateString, java.lang.String fromPattern, java.lang.String toPattern)
          Helper method to convert a date string from one pattern to another
static boolean equalsIgnoreTimestamp(java.util.Date first, java.util.Date second)
          Determines whether the dates are both on the same day
static java.util.Calendar findMidnight(java.util.Calendar input)
           
static long findMidnight(java.util.Date input)
          Helper method to return the input date at midnight.
 java.lang.String formatDate(java.util.Date date)
          Returns a date formatted using the page user's locale and timezone for correct date formatting of the passed in date.
static java.lang.String formatDate(java.util.Date date, java.util.Locale locale)
          Formats a date in the DateFormat pattern for the specified Locale
static java.lang.String genericizeDateString(java.lang.String dateString, java.util.Locale locale)
          Converts a localized date string to a generic date string (MM/dd/yyyy)
 FastDateFormat getDateFormat()
          Returns a date formatter using the page user's locale and timezone for correct date formatting.
static java.lang.String getDatePattern()
          Returns the date pattern used in all fields where the user can input text.
static java.util.Date getMaxDate()
          Returns the maximum date value allowed.
 FastDateFormat getMediumDateFormat()
          Returns a medium date formatter using the page user's locale and timezone for correct date formatting.
 java.lang.String getMediumFormatDate()
          Returns a medium date formatted using the page user's locale and timezone for correct date formatting of the current date and time.
 java.lang.String getMediumFormatDate(java.util.Date date)
          Returns a short date formatted using the page user's locale and timezone for correct date formatting of the passed in date.
 FastDateFormat getMediumTimeFormat()
          Returns a medium date formatter using the page user's locale and timezone for correct time formatting.
 FastDateFormat getShortDateFormat()
          Returns a short date formatter using the page user's locale and timezone for correct date formatting.
 java.lang.String getShortFormatDate()
          Returns a short date formatted using the page user's locale and timezone for correct date formatting of the current date and time.
 java.lang.String getShortFormatDate(java.util.Date date)
          Returns a short date formatted using the page user's locale and timezone for correct date formatting of the passed in date.
static java.lang.String getShortFormatDate(java.util.Date date, java.util.Locale locale)
           
 FastDateFormat getShortTimeFormat()
          Returns a short date formatter using the page user's locale and timezone for correct time formatting.
 FastDateFormat getTimeFormat()
          Returns a date formatter using the page user's locale and timezone for correct time formatting.
 java.util.TimeZone getTimeZone()
          Returns the page user's time zone.
static java.lang.String localizeDateString(java.lang.String dateString, java.util.Locale locale)
          Converts a generic date string (MM/dd/yyyy) to a localized date string
static java.util.Date parseLocalizedDateString(java.lang.String dateString, java.util.Locale locale)
          Parses a localized date string into a Date
static java.util.Date roundDate(java.util.Date date, int seconds)
          Rounds the given date down to the nearest specified second.
static long roundDate(long date, int seconds)
          Rounds the given date down to the nearest specfied second.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_DATE_PATTERN

public static final java.lang.String DEFAULT_DATE_PATTERN
See Also:
Constant Field Values

DEFAULT_SHORT_DATE_PATTERN

public static final java.lang.String DEFAULT_SHORT_DATE_PATTERN
See Also:
Constant Field Values

ONE_DAY_IN_MILLIS

public static final long ONE_DAY_IN_MILLIS
See Also:
Constant Field Values
Constructor Detail

DateUtils

public DateUtils(javax.servlet.http.HttpServletRequest request,
                 User user)
Method Detail

getTimeZone

public java.util.TimeZone getTimeZone()
Returns the page user's time zone.

Returns:
the page user's time zone.
See Also:
LocaleUtils.getTimeZone(javax.servlet.http.HttpServletRequest, com.jivesoftware.base.User)

formatDate

public java.lang.String formatDate(java.util.Date date)
Returns a date formatted using the page user's locale and timezone for correct date formatting of the passed in date. A DateFormat.MEDIUM date and DateFormat.SHORT time format is returned.

Returns:
a date formatter using the page user's locale and timezone.
See Also:
Locale, DateFormat

getMediumFormatDate

public java.lang.String getMediumFormatDate()
Returns a medium date formatted using the page user's locale and timezone for correct date formatting of the current date and time. A DateFormat.MEDIUM date is returned.

Returns:
a medium date formatter using the page user's locale and timezone.
See Also:
Locale, DateFormat

getMediumFormatDate

public java.lang.String getMediumFormatDate(java.util.Date date)
Returns a short date formatted using the page user's locale and timezone for correct date formatting of the passed in date. A DateFormat.MEDIUM date is returned.

Returns:
a medium date formatter using the page user's locale and timezone.
See Also:
Locale, DateFormat

getShortFormatDate

public java.lang.String getShortFormatDate()
Returns a short date formatted using the page user's locale and timezone for correct date formatting of the current date and time. A DateFormat.SHORT date is returned.

Returns:
a short date formatter using the page user's locale and timezone.
See Also:
Locale, DateFormat

getShortFormatDate

public java.lang.String getShortFormatDate(java.util.Date date)
Returns a short date formatted using the page user's locale and timezone for correct date formatting of the passed in date. A DateFormat.SHORT date is returned.

Returns:
a short date formatter using the page user's locale and timezone.
See Also:
Locale, DateFormat

getDatePattern

public static java.lang.String getDatePattern()
Returns the date pattern used in all fields where the user can input text. This is a valid date format which can be used by Java's SimpleDateFormat class. By default, DEFAULT_DATE_PATTERN is returned. You can override this behavior by setting the date.defaultPattern property in the Jive config file.

Returns:
the date pattern used for date fields in the UI.

getDateFormat

public FastDateFormat getDateFormat()
Returns a date formatter using the page user's locale and timezone for correct date formatting. A DateFormat.MEDIUM date and DateFormat.SHORT time format is returned.

Returns:
a date formatter using the page user's locale and timezone.
See Also:
Locale, DateFormat

getMediumDateFormat

public FastDateFormat getMediumDateFormat()
Returns a medium date formatter using the page user's locale and timezone for correct date formatting. A DateFormat.MEDIUM date formatter is returned.

Returns:
a medium date formatter using the page user's locale and timezone.
See Also:
DateFormat

getShortDateFormat

public FastDateFormat getShortDateFormat()
Returns a short date formatter using the page user's locale and timezone for correct date formatting. A DateFormat.SHORT date formatter is returned.

Returns:
a short date formatter using the page user's locale and timezone.
See Also:
DateFormat

getTimeFormat

public FastDateFormat getTimeFormat()
Returns a date formatter using the page user's locale and timezone for correct time formatting. A DateFormat.MEDIUM time format is returned.

Returns:
a date formatter using the page user's locale and timezone.
See Also:
Locale, DateFormat

getMediumTimeFormat

public FastDateFormat getMediumTimeFormat()
Returns a medium date formatter using the page user's locale and timezone for correct time formatting. A DateFormat.MEDIUM time formatter is returned.

Returns:
a medium time formatter using the page user's locale and timezone.
See Also:
DateFormat

getShortTimeFormat

public FastDateFormat getShortTimeFormat()
Returns a short date formatter using the page user's locale and timezone for correct time formatting. A DateFormat.SHORT time formatter is returned.

Returns:
a short time formatter using the page user's locale and timezone.
See Also:
DateFormat

roundDate

public static java.util.Date roundDate(java.util.Date date,
                                       int seconds)
Rounds the given date down to the nearest specified second. The following table shows sample input and expected output values: (Note, only the time portion of the date is shown for brevity)

DateSecondsResult
1:37.4851:37.45
1:37.48101:37.40
1:37.48301:37.30
1:37.48601:37.00
1:37.481201:36.00

This method is useful when calculating the last post in a forum or the number of new messages from a given date. Using a rounded date allows Jive to internally cache the results of the date query. Here's an example that shows the last posted message in a forum accurate to the last 5 minutess:

ResultFilter filter = new ResultFilter();
filter.setSortOrder(ResultFilter.DESCENDING);
filter.setSortField(JiveConstants.CREATION_DATE);
filter.setCreationDateRangeMin(ResultFilter.roundDate(forum.getModificationDate(), 5*60));
filter.setNumResults(1);
Iterator messages = forum.messages(filter);
ForumMessage lastPost = (ForumMessage)messages.next();

Parameters:
date - the Date we want to round.
seconds - the number of seconds we want to round the date to.
Returns:
the given date, rounded down to the nearest specified number of seconds.

roundDate

public static long roundDate(long date,
                             int seconds)
Rounds the given date down to the nearest specfied second.

Parameters:
date - the date (as a long) that we want to round.
seconds - the number of seconds we want to round the date to.
Returns:
the given date (as a long), rounded down to the nearest specified number of seconds.

getMaxDate

public static java.util.Date getMaxDate()
Returns the maximum date value allowed. Some classes need this to represent the same thing as a "null" date -- in those cases it's not possible to insert null's into the database usually for SQL reasons.

Returns:
new Date(999999999999999L);

genericizeDateString

public static java.lang.String genericizeDateString(java.lang.String dateString,
                                                    java.util.Locale locale)
                                             throws java.text.ParseException
Converts a localized date string to a generic date string (MM/dd/yyyy)

Parameters:
dateString - localized date string that adheres to the DateFormat pattern for a Locale
locale - locale to convert the dateString to
Returns:
dateString date string in the internal format MM/dd/yyyy
Throws:
java.text.ParseException - if the date string does not match the DateFormat pattern for the user's Locale

localizeDateString

public static java.lang.String localizeDateString(java.lang.String dateString,
                                                  java.util.Locale locale)
                                           throws java.text.ParseException
Converts a generic date string (MM/dd/yyyy) to a localized date string

Parameters:
dateString - date string in the internal format MM/dd/yyyy
locale - the locale to convert the dateString to
Returns:
localized date string that adheres to the DateFormat pattern for the user's Locale
Throws:
java.text.ParseException - if the dateString does not match the internal DateFormat pattern of MM/dd/yyyy

parseLocalizedDateString

public static java.util.Date parseLocalizedDateString(java.lang.String dateString,
                                                      java.util.Locale locale)
                                               throws java.text.ParseException
Parses a localized date string into a Date

Parameters:
dateString - localized date string that adheres to the DateFormat pattern for a Locale
locale - locale to determine the DateFormat pattern from
Returns:
date object
Throws:
java.text.ParseException - if the date string does not match the DateFormat pattern for the user's Locale

formatDate

public static java.lang.String formatDate(java.util.Date date,
                                          java.util.Locale locale)
Formats a date in the DateFormat pattern for the specified Locale

Parameters:
date - date to format
locale - locale to determine DateFormat pattern
Returns:
date string in the pattern of the given locale

getShortFormatDate

public static java.lang.String getShortFormatDate(java.util.Date date,
                                                  java.util.Locale locale)

convertDateString

protected static java.lang.String convertDateString(java.lang.String dateString,
                                                    java.lang.String fromPattern,
                                                    java.lang.String toPattern)
                                             throws java.text.ParseException
Helper method to convert a date string from one pattern to another

Parameters:
dateString - date string to convert
fromPattern - pattern of date string
toPattern - pattern to convert date string to
Returns:
converted date string
Throws:
java.text.ParseException - thrown if dateString doesn't match fromPattern

equalsIgnoreTimestamp

public static boolean equalsIgnoreTimestamp(java.util.Date first,
                                            java.util.Date second)
Determines whether the dates are both on the same day

Parameters:
first -
second -
Returns:

findMidnight

public static long findMidnight(java.util.Date input)
Helper method to return the input date at midnight. Useful for methods that need to

Parameters:
input -
Returns:
Milliseconds at midnight for the day passed in.

findMidnight

public static java.util.Calendar findMidnight(java.util.Calendar input)

Jive Forums Project Page

Copyright © 1999-2006 Jive Software.