Package com.cheetahdigital.uikit.helper
Class DateHelper
- java.lang.Object
-
- com.cheetahdigital.uikit.helper.DateHelper
-
public class DateHelper extends java.lang.Object
Helper class for formatting and convertingDate
andCalendar
objects.
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.TimeZone
PDT_TimeZone
static java.lang.String
SLDateFormat_Numeric_Dashed
static java.lang.String
SLDateFormat_Short
static java.lang.String
SLDateFormat_Standard_DateTime
-
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 DateFormatstatic 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 datestatic 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 givenDate
static java.lang.String
getDayOfWeek(android.content.Context context, java.util.Date date, java.lang.String timezoneId)
Returns the day of week of the givenDate
andTimeZone
idstatic int
getDaysFromNow(java.util.Date date)
Returns the days between twoDate
static java.lang.String
getDeviceDateFormat(android.content.Context context, java.util.Date date)
Formats the date according to the device's settingsstatic 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 timezonestatic 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)
UsesDateUtils.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 aDate
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 setDateFormat
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 devicestatic java.lang.String
getTime(android.content.Context context, java.util.Date date, java.lang.String timezoneId)
Returns the time format of the devicestatic 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 formatSLDateFormat_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.
-
-
-
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
-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 givenDate
andTimeZone
id- Parameters:
context
- use to get resourcesdate
-Date
object to extract current day of the weektimezoneId
- Timezone to use when formatting date- Returns:
- day of week of the given
Date
andTimeZone
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 resourcesdate
-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 resourcesdate
-Date
object 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
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 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_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 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
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 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.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 setDateFormat
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)
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
-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 twoDate
- Parameters:
date
- target date to check- Returns:
- the day/s between the date now
-
-