Android Utililty Methods Date Relative Format

List of utility methods to do Date Relative Format

Description

The list of methods to do Date Relative Format are organized into topic(s).

Method

StringformatRelativeTime(String fullRelativeDate)
format Relative Time
final String relativeDateFormat1 = "(\\d+)\\s(\\w+)\\s\\w+"; 
final Pattern relativeDatePattern1 = Pattern
        .compile(relativeDateFormat1);
final String relativeDateFormat2 = "(\\w+)\\s(\\d+)\\s(\\w+)"; 
final Pattern relativeDatePattern2 = Pattern
        .compile(relativeDateFormat2);
final Matcher matcher1 = relativeDatePattern1
        .matcher(fullRelativeDate);
...
StringgetRelativeTimeofTweet(String tweetCreatedAt)
get Relative Timeof Tweet
final SimpleDateFormat sdf = new SimpleDateFormat(
        TWITTER_TIMESTAMP_FORMAT, Locale.ENGLISH);
String relativeTime = "";
try {
    long dateInMillis = sdf.parse(tweetCreatedAt).getTime();
    relativeTime = getRelativeTimeFromMilliSeconds(dateInMillis);
} catch (ParseException e) {
    Log.d("info", "Couldn't parse relative timestamp for: "
...
StringrelativeDate(String dateString)
relative Date
String dayMonthYear = dateString.substring(0,
        dateString.indexOf(" "));
int month = Integer.parseInt(dayMonthYear.substring(0, 2));
int day = Integer.parseInt(dayMonthYear.substring(3, 5));
int year = Integer.parseInt(dayMonthYear.substring(6, 8));
Calendar cal = Calendar.getInstance();
int currentDay = cal.get(Calendar.DAY_OF_MONTH);
int currentMonth = cal.get(Calendar.MONTH) + 1;
...
StringgetRelativeDate(Date date)
get Relative Date
return DateUtils.getRelativeTimeSpanString(date.getTime(),
        new Date().getTime(), DateUtils.DAY_IN_MILLIS,
        DateUtils.FORMAT_ABBREV_RELATIVE).toString();
StringhumanFriendlyDate(Date date)
human Friendly Date
return humanFriendlyDate(date.getTime());
booleandateIsBetweenTwoDates(String begin, String end)
date Is Between Two Dates
String[] splitBeginStr = begin.split(":");
int hrBegin = Integer.parseInt(splitBeginStr[0]);
int minBegin = Integer.parseInt(splitBeginStr[1]);
String[] splitEndStr = end.split(":");
int hrEnd = Integer.parseInt(splitEndStr[0]);
int minEnd = Integer.parseInt(splitEndStr[1]);
Calendar beginCal = Calendar.getInstance();
beginCal.set(Calendar.HOUR_OF_DAY, hrBegin);
...
StringgetHumanReadableDateDiff(Date pastDate, boolean unknownIfNull)
Returns some kind of human readable representation of a past date, e.g.
if (pastDate == null || pastDate.getTime() == 0) {
    return unknownIfNull ? "Unknown" : "Never";
Date currentDate = new Date();
long timeDiff = currentDate.getTime() - pastDate.getTime();
if (timeDiff < TimeUnit.SECONDS.toMillis(60)) { 
    return "<1 min ago";
} else if (timeDiff < TimeUnit.SECONDS.toMillis(60 * 60)) { 
...