Class DateHelper


  • public class DateHelper
    extends java.lang.Object
    Helper class for formatting and converting Date and Calendar objects.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Date attemptParseDate​(java.lang.String userDateString, android.content.Context context)
      Parse String from the Server using any of the following Android Date Formats: DateFormat from Settings, if able to get one Short DateFormat Medium DateFormat Long DateFormat
      static float computeAge​(java.lang.String date)
      Computes the date difference between today and the date given (Formatted String: yyyy-mm-dd)
      static int daysLeft​(java.util.Date date)
      Returns the number of days left from start to end Date.
      static int daysLeft​(java.util.Date startDate, java.util.Date endDate)  
      static int daysLeft​(java.util.Date startDate, java.util.Date endDate, java.lang.String timezoneId)  
      static java.lang.String formatValidityString​(android.content.Context context, java.util.Date startDate, java.util.Date endDate)
      Format two dates and add a 'Valid:' string before these two date
      static java.lang.String getDateStringFromServerDateString​(java.lang.String dateStringFromServer, android.content.Context context)
      Gets the Formatted User Date String from the Server Date String.
      static java.lang.String getDayOfWeek​(android.content.Context context, java.util.Date date)
      Returns the day of week of the given Date
      static java.lang.String getDayOfWeek​(android.content.Context context, java.util.Date date, java.lang.String timezoneId)
      Returns the day of week of the given Date and TimeZone id
      static int getDaysFromNow​(java.util.Date date)
      Returns the days between two Date
      static java.lang.String getDeviceDateFormat​(android.content.Context context, java.util.Date date)
      Formats the date according to the device's settings
      static java.lang.String getDeviceDateFormat​(android.content.Context context, java.util.Date date, java.lang.String timezoneId)
      Formats the date according to the device's settings and console's timezone
      static java.lang.String getDisplayDateString​(java.util.Date date, android.content.Context context)
      Attempt to Format the Date String using the Current Settings DateFormat.
      static java.lang.String getDuration​(android.content.Context context, long time)
      Uses DateUtils.getRelativeTimeSpanString(long, long, long) with some tweaks.
      static java.text.DateFormat getMediumDateFormat​(android.content.Context context)
      Gets the Medium DateFormat (e.g.
      static long getMillisInDay​(java.util.Calendar calendar)
      Returns the total milliseconds of day time.
      static java.lang.String getNumericDashedDate​(java.util.Date date)
      Formats a Date with "yyyy-MM-dd" and returns the String value.
      static java.lang.String getServerDateStringFromDisplayString​(java.lang.String displayDateString, android.content.Context context)
      Gets the Server Date String from the Displayable Date String.
      static java.text.DateFormat getSettingsDateFormat​(android.content.Context context)
      Attempts to get the currently set DateFormat from Settings.
      static java.lang.String getShortMonth​(int month)  
      static java.lang.String getShortMonthYearFromIndex​(int startMonth, int startYear, int selectedIndex)  
      static java.lang.String getTime​(android.content.Context context, java.util.Date date)
      Returns the time format of the device
      static java.lang.String getTime​(android.content.Context context, java.util.Date date, java.lang.String timezoneId)
      Returns the time format of the device
      static java.lang.String getTimeZoneDateFormat​(android.content.Context context, java.util.Date date, java.lang.String timeZoneID)
      Formats the date according to the provided by device's date format and timeZone ID.
      static java.util.Date parseGmtDate​(java.lang.String gmtDateString)
      Parse String from the GMT Date following the format "EEE, dd MMM yyyy HH:mm:ss 'GMT'".
      static java.util.Date parseServerDate​(java.lang.String serverDateString)
      Parse String from the Server following the format SLDateFormat_Numeric_Dashed.
      static void showDatePicker​(android.content.Context context, int style, int year, int month, int dayOfMonth, long maxDateInMillis, android.app.DatePickerDialog.OnDateSetListener listener)
      Shows date picker dialog using android DatePickerDialog.
      static void showDatePicker​(android.content.Context context, int year, int month, int dayOfMonth, long maxDateInMillis, android.app.DatePickerDialog.OnDateSetListener listener)
      Shows date picker dialog using android DatePickerDialog.
      static void showDatePicker​(android.content.Context context, int year, int month, int dayOfMonth, android.app.DatePickerDialog.OnDateSetListener listener)
      Shows date picker dialog using android DatePickerDialog.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SLDateFormat_Standard_DateTime

        public static final java.lang.String SLDateFormat_Standard_DateTime
        See Also:
        Constant Field Values
      • SLDateFormat_Numeric_Dashed

        public static final java.lang.String SLDateFormat_Numeric_Dashed
        See Also:
        Constant Field Values
      • SLDateFormat_Short

        public static final java.lang.String SLDateFormat_Short
        See Also:
        Constant Field Values
      • PDT_TimeZone

        public static final java.util.TimeZone PDT_TimeZone
    • Method Detail

      • getShortMonth

        public static java.lang.String getShortMonth​(int month)
      • getShortMonthYearFromIndex

        public static java.lang.String getShortMonthYearFromIndex​(int startMonth,
                                                                  int startYear,
                                                                  int selectedIndex)
      • getDeviceDateFormat

        public static java.lang.String getDeviceDateFormat​(android.content.Context context,
                                                           java.util.Date date)

        Formats the date according to the device's settings

        Parameters:
        context - application context
        date - the Date object to be formatted
        Returns:
        formatted object
      • getDeviceDateFormat

        public static java.lang.String getDeviceDateFormat​(android.content.Context context,
                                                           java.util.Date date,
                                                           java.lang.String timezoneId)

        Formats the date according to the device's settings and console's timezone

        Parameters:
        context - app context
        date - the Date object to be formatted
        timezoneId - timezone id
        Returns:
        Date formatted using the current device format
      • getDayOfWeek

        public static java.lang.String getDayOfWeek​(android.content.Context context,
                                                    java.util.Date date)
        Returns the day of week of the given Date
        Parameters:
        context - use to get resources
        date - Date object to extract current day of the week
        Returns:
        String day of week
      • getDayOfWeek

        public static java.lang.String getDayOfWeek​(android.content.Context context,
                                                    java.util.Date date,
                                                    java.lang.String timezoneId)
        Returns the day of week of the given Date and TimeZone id
        Parameters:
        context - use to get resources
        date - Date object to extract current day of the week
        timezoneId - Timezone to use when formatting date
        Returns:
        day of week of the given Date and TimeZone id
      • getTime

        public static java.lang.String getTime​(android.content.Context context,
                                               java.util.Date date)
        Returns the time format of the device
        Parameters:
        context - use to get resources
        date - Date object to extract
        Returns:
        the time format of the device
      • getTime

        public static java.lang.String getTime​(android.content.Context context,
                                               java.util.Date date,
                                               java.lang.String timezoneId)
        Returns the time format of the device
        Parameters:
        context - use to get resources
        date - Date object to extract
        timezoneId - Timezone to use when formatting date
        Returns:
        the time format of the device
      • computeAge

        public static float computeAge​(java.lang.String date)
        Computes the date difference between today and the date given (Formatted String: yyyy-mm-dd)
        Parameters:
        date - date difference in years
        Returns:
        date difference between today and the date given
      • getNumericDashedDate

        public static java.lang.String getNumericDashedDate​(java.util.Date date)

        Formats a Date with "yyyy-MM-dd" and returns the String value.

        Ex: Dec 13, 2014 to 2014-12-13

        Parameters:
        date - the Date object.
        Returns:
        the String value of the formatted date
      • getServerDateStringFromDisplayString

        public static java.lang.String getServerDateStringFromDisplayString​(java.lang.String displayDateString,
                                                                            android.content.Context context)
        Gets the Server Date String from the Displayable Date String.
        Parameters:
        displayDateString - the Displayable Date String
        context - context of the application
        Returns:
        Server Date String from the Displayable Date String
      • getDateStringFromServerDateString

        public static java.lang.String getDateStringFromServerDateString​(java.lang.String dateStringFromServer,
                                                                         android.content.Context context)
        Gets the Formatted User Date String from the Server Date String. The User Date String may be one of the following:
        1. the DateFormat from the Settings
        2. Medium DateFormat
        Parameters:
        dateStringFromServer - the Date String from Server which follows the format from SLDateFormat_Numeric_Dashed
        context - the Android Context to get the current DateFormat from
        Returns:
        the formatted Date String, or null if there was a problem while parsing the input
      • getDisplayDateString

        public static java.lang.String getDisplayDateString​(java.util.Date date,
                                                            android.content.Context context)
        Attempt to Format the Date String using the Current Settings DateFormat. Else, just use Medium Format.
        Parameters:
        date - input Date to be converted
        context - the Android context
        Returns:
        the Displayable Date String, either using the Current Settings' Format or Medium DateFormat
      • attemptParseDate

        public static java.util.Date attemptParseDate​(java.lang.String userDateString,
                                                      android.content.Context context)
        Parse String from the Server using any of the following Android Date Formats:
        • DateFormat from Settings, if able to get one
        • Short DateFormat
        • Medium DateFormat
        • Long DateFormat
        Parameters:
        userDateString - the User Date String
        context - the Android Context
        Returns:
        the Date object if successfully parsed, else null
      • parseServerDate

        public static java.util.Date parseServerDate​(java.lang.String serverDateString)
                                              throws java.text.ParseException
        Parse String from the Server following the format SLDateFormat_Numeric_Dashed.
        Parameters:
        serverDateString - the Server formatted Date String
        Returns:
        the parsed Date Object
        Throws:
        java.text.ParseException - failed parsing the current string
      • parseGmtDate

        public static java.util.Date parseGmtDate​(java.lang.String gmtDateString)
                                           throws java.text.ParseException
        Parse String from the GMT Date following the format "EEE, dd MMM yyyy HH:mm:ss 'GMT'".
        Parameters:
        gmtDateString - the Server formatted Date String
        Returns:
        the parsed Date Object
        Throws:
        java.text.ParseException - failed parsing the current string
      • getSettingsDateFormat

        public static java.text.DateFormat getSettingsDateFormat​(android.content.Context context)
        Attempts to get the currently set DateFormat from Settings.
        Parameters:
        context - the Android context to get the Settings from
        Returns:
        the current DateFormat from Settings
      • getMediumDateFormat

        public static java.text.DateFormat getMediumDateFormat​(android.content.Context context)
        Gets the Medium DateFormat (e.g. Jan 3, 2000).
        Parameters:
        context - the Android context
        Returns:
        the Medium DateFormat
      • getDuration

        public static java.lang.String getDuration​(android.content.Context context,
                                                   long time)
        Uses DateUtils.getRelativeTimeSpanString(long, long, long) with some tweaks.
        Parameters:
        context - use to get resources
        time - long value for the time
        Returns:
        the String that tells the duration of the time.
      • formatValidityString

        public static java.lang.String formatValidityString​(android.content.Context context,
                                                            java.util.Date startDate,
                                                            java.util.Date endDate)

        Format two dates and add a 'Valid:' string before these two date

        Parameters:
        context - use to get resources
        startDate - the start date
        endDate - the end date
        Returns:
        String in the format Valid: Start Date - End Date if dates are invalid an empty string will be returned
      • getTimeZoneDateFormat

        public static java.lang.String getTimeZoneDateFormat​(android.content.Context context,
                                                             java.util.Date date,
                                                             java.lang.String timeZoneID)
        Formats the date according to the provided by device's date format and timeZone ID. If the timezone ID is empty, the returned format will depend on the Locale of the device.
        Parameters:
        date - the Date object to be formatted
        context - the application context
        timeZoneID - timeZone ID of the date
        Returns:
        date in pattern with the given timezon id or the locale of the device
      • showDatePicker

        public static void showDatePicker​(android.content.Context context,
                                          int year,
                                          int month,
                                          int dayOfMonth,
                                          android.app.DatePickerDialog.OnDateSetListener listener)
        Shows date picker dialog using android DatePickerDialog.
        Parameters:
        context - used for getting resources
        year - value of the year
        month - value of the month
        dayOfMonth - value for day of month
        listener - callback for dialog picker behavior
      • showDatePicker

        public static void showDatePicker​(android.content.Context context,
                                          int year,
                                          int month,
                                          int dayOfMonth,
                                          long maxDateInMillis,
                                          android.app.DatePickerDialog.OnDateSetListener listener)
        Shows date picker dialog using android DatePickerDialog.
        Parameters:
        context - used for getting resources
        year - value of the year
        month - value of the month
        dayOfMonth - value for day of month
        maxDateInMillis - long value for max limit of date
        listener - callback for dialog picker behavior
      • showDatePicker

        public static void showDatePicker​(android.content.Context context,
                                          @StyleRes
                                          int style,
                                          int year,
                                          int month,
                                          int dayOfMonth,
                                          long maxDateInMillis,
                                          android.app.DatePickerDialog.OnDateSetListener listener)
        Shows date picker dialog using android DatePickerDialog.
        Parameters:
        context - used for getting resources
        style - default style for the dialog picker
        year - value of the year
        month - value of the month
        dayOfMonth - value for day of month
        maxDateInMillis - long value for max limit of date
        listener - callback for dialog picker behavior
      • daysLeft

        public static int daysLeft​(java.util.Date date)
        Returns the number of days left from start to end Date.
        Parameters:
        date - the deadline date.
        Returns:
        number of days left.
      • daysLeft

        public static int daysLeft​(java.util.Date startDate,
                                   java.util.Date endDate)
      • daysLeft

        public static int daysLeft​(java.util.Date startDate,
                                   java.util.Date endDate,
                                   java.lang.String timezoneId)
      • getMillisInDay

        public static long getMillisInDay​(java.util.Calendar calendar)
        Returns the total milliseconds of day time.
        Parameters:
        calendar - Calendar instance to be used as basis of time
        Returns:
        millisecond value of the equivalent calendar date time
      • getDaysFromNow

        public static int getDaysFromNow​(java.util.Date date)
        Returns the days between two Date
        Parameters:
        date - target date to check
        Returns:
        the day/s between the date now