Skip to content

Instantly share code, notes, and snippets.

@tomergoldst
Last active January 23, 2021 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomergoldst/22bc81c75b025443484c91546c2d95dc to your computer and use it in GitHub Desktop.
Save tomergoldst/22bc81c75b025443484c91546c2d95dc to your computer and use it in GitHub Desktop.
object ReadableDateUtils {
private const val flagsDateOnly = DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_ABBREV_MONTH
private const val flagsDateOnlyWithDayOfWeek =
DateUtils.FORMAT_SHOW_DATE or
DateUtils.FORMAT_ABBREV_MONTH or
DateUtils.FORMAT_ABBREV_WEEKDAY or
DateUtils.FORMAT_SHOW_WEEKDAY
private const val flagsDateTimeWithDayOfWeek =
DateUtils.FORMAT_SHOW_DATE or
DateUtils.FORMAT_ABBREV_MONTH or
DateUtils.FORMAT_ABBREV_WEEKDAY or
DateUtils.FORMAT_SHOW_WEEKDAY or
DateUtils.FORMAT_SHOW_TIME or
DateUtils.FORMAT_ABBREV_TIME
private const val flagsTimeOnly = DateUtils.FORMAT_SHOW_TIME
private const val flagsDatesRange =
DateUtils.FORMAT_SHOW_DATE or
DateUtils.FORMAT_SHOW_YEAR or
DateUtils.FORMAT_ABBREV_MONTH
private val timeSimpleDateFormat: SimpleDateFormat
get() = DateFormat.getTimeInstance(DateFormat.SHORT) as SimpleDateFormat
@JvmStatic
fun getReadableTimeText(time: Long): String {
return timeSimpleDateFormat.format(Date(time))
}
fun getReadableTimeText(context: Context, time: Long): String {
return DateUtils.formatDateTime(context, time, flagsTimeOnly)
}
fun getReadableTimeText(context: Context, date: Date): String {
return DateUtils.formatDateTime(context, date.time, flagsTimeOnly)
}
fun getReadableTimeText(context: Context, time: Calendar): String {
return DateUtils.formatDateTime(context, time.timeInMillis, flagsTimeOnly)
}
fun getDateOnlyFormat(context: Context, time: Long): String {
return DateUtils.formatDateTime(context, time, flagsDateOnly)
}
fun getDateOnlyFormat(context: Context, calendar: Calendar): String {
return getDateOnlyFormat(context, calendar.timeInMillis)
}
fun getDateOnlyFormat(context: Context, date: Date): String {
return getDateOnlyFormat(context, date.time)
}
fun getDateWithDayOfWeekFormat(context: Context, time: Long): String {
return DateUtils.formatDateTime(
context,
time,
flagsDateOnlyWithDayOfWeek
)
}
fun getDateWithDayOfWeekFormat(context: Context, calendar: Calendar): String {
return getDateWithDayOfWeekFormat(context, calendar.timeInMillis)
}
fun getDateWithDayOfWeekFormat(context: Context, date: Date): String {
return getDateWithDayOfWeekFormat(context, date.time)
}
fun getDateTimeWithDayOfWeekFormat(context: Context, timeInMillis: Long): String {
return DateUtils.formatDateTime(context, timeInMillis, flagsDateTimeWithDayOfWeek)
}
fun getDateTimeWithDayOfWeekFormat(context: Context, date: Date): String {
return getDateTimeWithDayOfWeekFormat(context, date.time)
}
fun getDateTimeWithDayOfWeekFormat(context: Context, calendar: Calendar): String {
return getDateTimeWithDayOfWeekFormat(context, calendar.timeInMillis)
}
fun getRelativeTimeSpanFormatInDays(
time: Long,
now: Long = System.currentTimeMillis()
): String {
return DateUtils.getRelativeTimeSpanString(
time,
now,
DateUtils.DAY_IN_MILLIS,
flagsDateOnly
) as String
}
fun getRelativeTimeSpanFormatInHours(
time: Long,
now: Long = System.currentTimeMillis()
): String {
return DateUtils.getRelativeTimeSpanString(
time,
now,
DateUtils.HOUR_IN_MILLIS
) as String
}
fun getRelativeTimeSpanFormatInMin(
time: Long,
now: Long = System.currentTimeMillis()
): String {
return DateUtils.getRelativeTimeSpanString(
time,
now,
DateUtils.MINUTE_IN_MILLIS
) as String
}
fun getDatesRangeFormat(
context: Context,
startTime: Long,
endTime: Long
): String {
return DateUtils.formatDateRange(
context,
startTime,
endTime,
flagsDatesRange
)
}
fun getTimeRangeFormat(
context: Context,
startTime: Long,
endTime: Long
): String {
val sb = StringBuilder()
sb.append(getReadableTimeText(context, startTime))
.append(" - ")
.append(getReadableTimeText(context, endTime))
return sb.toString()
}
fun getTimeRangeFormat(
context: Context,
startCalendar: Calendar,
endCalendar: Calendar
): String {
return getTimeRangeFormat(context, startCalendar.timeInMillis, endCalendar.timeInMillis)
}
fun getRawRelativeTimeSpanFormatInHours(time: Long): String {
val now = System.currentTimeMillis()
return DateUtils.getRelativeTimeSpanString(
time,
now,
DateUtils.HOUR_IN_MILLIS
) as String
}
fun getRawRelativeTimeSpanFormatInMin(time: Long): String {
val now = System.currentTimeMillis()
return DateUtils.getRelativeTimeSpanString(
time,
now,
DateUtils.MINUTE_IN_MILLIS
) as String
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment