Package com.cheetahdigital.uikit.helper
Class DateHelper
- java.lang.Object
-
- com.cheetahdigital.uikit.helper.DateHelper
-
public class DateHelper extends java.lang.ObjectHelper class for formatting and convertingDateandCalendarobjects.
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.TimeZonePDT_TimeZonestatic java.lang.StringSLDateFormat_Numeric_Dashedstatic java.lang.StringSLDateFormat_Shortstatic java.lang.StringSLDateFormat_Standard_DateTime
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.DateattemptParseDate(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 DateFormatstatic floatcomputeAge(java.lang.String date)Computes the date difference between today and the date given (Formatted String: yyyy-mm-dd)static intdaysLeft(java.util.Date date)Returns the number of days left from start to end Date.static intdaysLeft(java.util.Date startDate, java.util.Date endDate)static intdaysLeft(java.util.Date startDate, java.util.Date endDate, java.lang.String timezoneId)static java.lang.StringformatValidityString(android.content.Context context, java.util.Date startDate, java.util.Date endDate)Format two dates and add a 'Valid:' string before these two datestatic java.lang.StringgetDateStringFromServerDateString(java.lang.String dateStringFromServer, android.content.Context context)Gets the Formatted User Date String from the Server Date String.static java.lang.StringgetDayOfWeek(android.content.Context context, java.util.Date date)Returns the day of week of the givenDatestatic java.lang.StringgetDayOfWeek(android.content.Context context, java.util.Date date, java.lang.String timezoneId)Returns the day of week of the givenDateandTimeZoneidstatic intgetDaysFromNow(java.util.Date date)Returns the days between twoDatestatic java.lang.StringgetDeviceDateFormat(android.content.Context context, java.util.Date date)Formats the date according to the device's settingsstatic java.lang.StringgetDeviceDateFormat(android.content.Context context, java.util.Date date, java.lang.String timezoneId)Formats the date according to the device's settings and console's timezonestatic java.lang.StringgetDisplayDateString(java.util.Date date, android.content.Context context)Attempt to Format the Date String using the Current Settings DateFormat.static java.lang.StringgetDuration(android.content.Context context, long time)UsesDateUtils.getRelativeTimeSpanString(long, long, long)with some tweaks.static java.text.DateFormatgetMediumDateFormat(android.content.Context context)Gets the Medium DateFormat (e.g.static longgetMillisInDay(java.util.Calendar calendar)Returns the total milliseconds of day time.static java.lang.StringgetNumericDashedDate(java.util.Date date)Formats aDatewith "yyyy-MM-dd" and returns the String value.static java.lang.StringgetServerDateStringFromDisplayString(java.lang.String displayDateString, android.content.Context context)Gets the Server Date String from the Displayable Date String.static java.text.DateFormatgetSettingsDateFormat(android.content.Context context)Attempts to get the currently setDateFormatfrom Settings.static java.lang.StringgetShortMonth(int month)static java.lang.StringgetShortMonthYearFromIndex(int startMonth, int startYear, int selectedIndex)static java.lang.StringgetTime(android.content.Context context, java.util.Date date)Returns the time format of the devicestatic java.lang.StringgetTime(android.content.Context context, java.util.Date date, java.lang.String timezoneId)Returns the time format of the devicestatic java.lang.StringgetTimeZoneDateFormat(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.DateparseGmtDate(java.lang.String gmtDateString)Parse String from the GMT Date following the format "EEE, dd MMM yyyy HH:mm:ss 'GMT'".static java.util.DateparseServerDate(java.lang.String serverDateString)Parse String from the Server following the formatSLDateFormat_Numeric_Dashed.static voidshowDatePicker(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 voidshowDatePicker(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 voidshowDatePicker(android.content.Context context, int year, int month, int dayOfMonth, android.app.DatePickerDialog.OnDateSetListener listener)Shows date picker dialog using android DatePickerDialog.
-
-
-
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 contextdate- 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 contextdate- the Date object to be formattedtimezoneId- 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 givenDate- Parameters:
context- use to get resourcesdate-Dateobject to extract current day of the week- Returns:
Stringday 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 givenDateandTimeZoneid- Parameters:
context- use to get resourcesdate-Dateobject to extract current day of the weektimezoneId- Timezone to use when formatting date- Returns:
- day of week of the given
DateandTimeZoneid
-
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 resourcesdate-Dateobject 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 resourcesdate-Dateobject to extracttimezoneId- 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
Datewith "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 Stringcontext- 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:- the DateFormat from the Settings
- Medium DateFormat
- Parameters:
dateStringFromServer- the Date String from Server which follows the format fromSLDateFormat_Numeric_Dashedcontext- 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 convertedcontext- 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 Stringcontext- the Android Context- Returns:
- the
Dateobject if successfully parsed, else null
-
parseServerDate
public static java.util.Date parseServerDate(java.lang.String serverDateString) throws java.text.ParseExceptionParse String from the Server following the formatSLDateFormat_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.ParseExceptionParse 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 setDateFormatfrom Settings.- Parameters:
context- the Android context to get the Settings from- Returns:
- the current
DateFormatfrom 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)UsesDateUtils.getRelativeTimeSpanString(long, long, long)with some tweaks.- Parameters:
context- use to get resourcestime- 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 resourcesstartDate- the start dateendDate- 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 formattedcontext- the application contexttimeZoneID- 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 resourcesyear- value of the yearmonth- value of the monthdayOfMonth- value for day of monthlistener- 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 resourcesyear- value of the yearmonth- value of the monthdayOfMonth- value for day of monthmaxDateInMillis- long value for max limit of datelistener- 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 resourcesstyle- default style for the dialog pickeryear- value of the yearmonth- value of the monthdayOfMonth- value for day of monthmaxDateInMillis- long value for max limit of datelistener- 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-Calendarinstance 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 twoDate- Parameters:
date- target date to check- Returns:
- the day/s between the date now
-
-