Java Code Examples for android.text.format.DateUtils#FORMAT_ABBREV_ALL

The following examples show how to use android.text.format.DateUtils#FORMAT_ABBREV_ALL . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ListItemUtils.java    From mytracks with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the date and time as an array of two strings.
 * 
 * @param isRecording true if recording
 * @param context the context
 * @param time the start time
 */
private static String[] getDateTime(
    boolean isRecording, Context context, long time, boolean useRelativeTime) {
  if (isRecording || time == 0L) {
    return new String[] { null, null };
  }
  boolean isToday = DateUtils.isToday(time);
  int timeFlags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL;

  if (isToday) {
    if (useRelativeTime) {
      return new String[] { DateUtils.getRelativeTimeSpanString(
          time, System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS,
          DateUtils.FORMAT_ABBREV_RELATIVE).toString(), null };
    } else {
      return new String[] { DateUtils.formatDateTime(context, time, timeFlags), null };
    }
  }

  return new String[] { DateUtils.formatDateTime(
      context, time, DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL),
      DateUtils.formatDateTime(context, time, timeFlags) };
}
 
Example 2
Source File: DateUtil.java    From droidkaigi2016 with Apache License 2.0 5 votes vote down vote up
@NonNull
public static String getMonthDate(Date date, Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), FORMAT_MMDD);
        return new SimpleDateFormat(pattern).format(date);
    } else {
        int flag = DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_NO_YEAR;
        return DateUtils.formatDateTime(context, date.getTime(), flag);
    }
}
 
Example 3
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users. As classy and polished a user experience as "1474061664" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context               Context to use for resource localization
 * @param normalizedUtcMidnight The date in milliseconds (UTC midnight)
 * @param showFullDate          Used to show a fuller-version of the date, which always
 *                              contains either the day of the week, today, or tomorrow, in
 *                              addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long normalizedUtcMidnight, boolean showFullDate) {

    /*
     * NOTE: localDate should be localDateMidnightMillis and should be straight from the
     * database
     *
     * Since we normalized the date when we inserted it into the database, we need to take
     * that normalized date and produce a date (in UTC time) that represents the local time
     * zone at midnight.
     */
    long localDate = getLocalMidnightFromNormalizedUtcDate(normalizedUtcMidnight);

    /*
     * In order to determine which day of the week we are creating a date string for, we need
     * to compare the number of days that have passed since the epoch (January 1, 1970 at
     * 00:00 GMT)
     */
    long daysFromEpochToProvidedDate = elapsedDaysSinceEpoch(localDate);

    /*
     * As a basis for comparison, we use the number of days that have passed from the epoch
     * until today.
     */
    long daysFromEpochToToday = elapsedDaysSinceEpoch(System.currentTimeMillis());

    if (daysFromEpochToProvidedDate == daysFromEpochToToday || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (daysFromEpochToProvidedDate - daysFromEpochToToday < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (daysFromEpochToProvidedDate < daysFromEpochToToday + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}
 
Example 4
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users.  As classy and polished a user experience as "20140102" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow"
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context      Context to use for resource localization
 * @param dateInMillis The date in milliseconds (UTC)
 * @param showFullDate Used to show a fuller-version of the date, which always contains either
 *                     the day of the week, today, or tomorrow, in addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long dateInMillis, boolean showFullDate) {

    long localDate = getLocalDateFromUTC(dateInMillis);
    long dayNumber = getDayNumber(localDate);
    long currentDayNumber = getDayNumber(System.currentTimeMillis());

    if (dayNumber == currentDayNumber || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (dayNumber - currentDayNumber < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (dayNumber < currentDayNumber + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}
 
Example 5
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users. As classy and polished a user experience as "1474061664" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context               Context to use for resource localization
 * @param normalizedUtcMidnight The date in milliseconds (UTC midnight)
 * @param showFullDate          Used to show a fuller-version of the date, which always
 *                              contains either the day of the week, today, or tomorrow, in
 *                              addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long normalizedUtcMidnight, boolean showFullDate) {

    /*
     * NOTE: localDate should be localDateMidnightMillis and should be straight from the
     * database
     *
     * Since we normalized the date when we inserted it into the database, we need to take
     * that normalized date and produce a date (in UTC time) that represents the local time
     * zone at midnight.
     */
    long localDate = getLocalMidnightFromNormalizedUtcDate(normalizedUtcMidnight);

    /*
     * In order to determine which day of the week we are creating a date string for, we need
     * to compare the number of days that have passed since the epoch (January 1, 1970 at
     * 00:00 GMT)
     */
    long daysFromEpochToProvidedDate = elapsedDaysSinceEpoch(localDate);

    /*
     * As a basis for comparison, we use the number of days that have passed from the epoch
     * until today.
     */
    long daysFromEpochToToday = elapsedDaysSinceEpoch(System.currentTimeMillis());

    if (daysFromEpochToProvidedDate == daysFromEpochToToday || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (daysFromEpochToProvidedDate - daysFromEpochToToday < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (daysFromEpochToProvidedDate < daysFromEpochToToday + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}
 
Example 6
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users.  As classy and polished a user experience as "20140102" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow"
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context      Context to use for resource localization
 * @param dateInMillis The date in milliseconds (UTC)
 * @param showFullDate Used to show a fuller-version of the date, which always contains either
 *                     the day of the week, today, or tomorrow, in addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long dateInMillis, boolean showFullDate) {

    long localDate = getLocalDateFromUTC(dateInMillis);
    long dayNumber = getDayNumber(localDate);
    long currentDayNumber = getDayNumber(System.currentTimeMillis());

    if (dayNumber == currentDayNumber || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (dayNumber - currentDayNumber < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (dayNumber < currentDayNumber + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}
 
Example 7
Source File: MainActivity.java    From samples with MIT License 4 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    final long min;
    switch (item.getItemId()) {
        case R.id.min_0:
            min = 0l;
            break;
        case R.id.min_min:
            min = DateUtils.MINUTE_IN_MILLIS;
            break;
        case R.id.min_hour:
            min = DateUtils.HOUR_IN_MILLIS;
            break;
        case R.id.min_day:
            min = DateUtils.DAY_IN_MILLIS;
            break;
        default:
            min = -1;
            break;
    }

    if (min >= 0) {
        item.setChecked(true);
        mAdapter.setMinResolution(min);
        return true;
    }

    item.setChecked(!item.isChecked());
    final int flag;
    switch (item.getItemId()) {
        case R.id.FORMAT_SHOW_TIME:
            flag = DateUtils.FORMAT_SHOW_TIME;
            break;
        case R.id.FORMAT_SHOW_WEEKDAY:
            flag = DateUtils.FORMAT_SHOW_WEEKDAY;
            break;
        case R.id.FORMAT_SHOW_YEAR:
            flag = DateUtils.FORMAT_SHOW_YEAR;
            break;
        case R.id.FORMAT_NO_YEAR:
            flag = DateUtils.FORMAT_NO_YEAR;
            break;
        case R.id.FORMAT_SHOW_DATE:
            flag = DateUtils.FORMAT_SHOW_DATE;
            break;
        case R.id.FORMAT_NO_MONTH_DAY:
            flag = DateUtils.FORMAT_NO_MONTH_DAY;
            break;
        case R.id.FORMAT_NO_NOON:
            flag = DateUtils.FORMAT_NO_NOON;
            break;
        case R.id.FORMAT_NO_MIDNIGHT:
            flag = DateUtils.FORMAT_NO_MIDNIGHT;
            break;
        case R.id.FORMAT_ABBREV_TIME:
            flag = DateUtils.FORMAT_ABBREV_TIME;
            break;
        case R.id.FORMAT_ABBREV_WEEKDAY:
            flag = DateUtils.FORMAT_ABBREV_WEEKDAY;
            break;
        case R.id.FORMAT_ABBREV_MONTH:
            flag = DateUtils.FORMAT_ABBREV_MONTH;
            break;
        case R.id.FORMAT_NUMERIC_DATE:
            flag = DateUtils.FORMAT_NUMERIC_DATE;
            break;
        case R.id.FORMAT_ABBREV_RELATIVE:
            flag = DateUtils.FORMAT_ABBREV_RELATIVE;
            break;
        case R.id.FORMAT_ABBREV_ALL:
            flag = DateUtils.FORMAT_ABBREV_ALL;
            break;
        default:
            throw new UnsupportedOperationException("unknown id");
    }

    if (item.isChecked()) {
        mAdapter.addFlag(flag);
    } else {
        mAdapter.removeFlag(flag);
    }

    return true;
}
 
Example 8
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users. As classy and polished a user experience as "1474061664" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context               Context to use for resource localization
 * @param normalizedUtcMidnight The date in milliseconds (UTC midnight)
 * @param showFullDate          Used to show a fuller-version of the date, which always
 *                              contains either the day of the week, today, or tomorrow, in
 *                              addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long normalizedUtcMidnight, boolean showFullDate) {

    /*
     * NOTE: localDate should be localDateMidnightMillis and should be straight from the
     * database
     *
     * Since we normalized the date when we inserted it into the database, we need to take
     * that normalized date and produce a date (in UTC time) that represents the local time
     * zone at midnight.
     */
    long localDate = getLocalMidnightFromNormalizedUtcDate(normalizedUtcMidnight);

    /*
     * In order to determine which day of the week we are creating a date string for, we need
     * to compare the number of days that have passed since the epoch (January 1, 1970 at
     * 00:00 GMT)
     */
    long daysFromEpochToProvidedDate = elapsedDaysSinceEpoch(localDate);

    /*
     * As a basis for comparison, we use the number of days that have passed from the epoch
     * until today.
     */
    long daysFromEpochToToday = elapsedDaysSinceEpoch(System.currentTimeMillis());

    if (daysFromEpochToProvidedDate == daysFromEpochToToday || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (daysFromEpochToProvidedDate - daysFromEpochToToday < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (daysFromEpochToProvidedDate < daysFromEpochToToday + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}
 
Example 9
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users. As classy and polished a user experience as "1474061664" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context               Context to use for resource localization
 * @param normalizedUtcMidnight The date in milliseconds (UTC midnight)
 * @param showFullDate          Used to show a fuller-version of the date, which always
 *                              contains either the day of the week, today, or tomorrow, in
 *                              addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long normalizedUtcMidnight, boolean showFullDate) {

    /*
     * NOTE: localDate should be localDateMidnightMillis and should be straight from the
     * database
     *
     * Since we normalized the date when we inserted it into the database, we need to take
     * that normalized date and produce a date (in UTC time) that represents the local time
     * zone at midnight.
     */
    long localDate = getLocalMidnightFromNormalizedUtcDate(normalizedUtcMidnight);

    /*
     * In order to determine which day of the week we are creating a date string for, we need
     * to compare the number of days that have passed since the epoch (January 1, 1970 at
     * 00:00 GMT)
     */
    long daysFromEpochToProvidedDate = elapsedDaysSinceEpoch(localDate);

    /*
     * As a basis for comparison, we use the number of days that have passed from the epoch
     * until today.
     */
    long daysFromEpochToToday = elapsedDaysSinceEpoch(System.currentTimeMillis());

    if (daysFromEpochToProvidedDate == daysFromEpochToToday || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (daysFromEpochToProvidedDate - daysFromEpochToToday < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (daysFromEpochToProvidedDate < daysFromEpochToToday + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}
 
Example 10
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users. As classy and polished a user experience as "1474061664" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context               Context to use for resource localization
 * @param normalizedUtcMidnight The date in milliseconds (UTC midnight)
 * @param showFullDate          Used to show a fuller-version of the date, which always
 *                              contains either the day of the week, today, or tomorrow, in
 *                              addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long normalizedUtcMidnight, boolean showFullDate) {

    /*
     * NOTE: localDate should be localDateMidnightMillis and should be straight from the
     * database
     *
     * Since we normalized the date when we inserted it into the database, we need to take
     * that normalized date and produce a date (in UTC time) that represents the local time
     * zone at midnight.
     */
    long localDate = getLocalMidnightFromNormalizedUtcDate(normalizedUtcMidnight);

    /*
     * In order to determine which day of the week we are creating a date string for, we need
     * to compare the number of days that have passed since the epoch (January 1, 1970 at
     * 00:00 GMT)
     */
    long daysFromEpochToProvidedDate = elapsedDaysSinceEpoch(localDate);

    /*
     * As a basis for comparison, we use the number of days that have passed from the epoch
     * until today.
     */
    long daysFromEpochToToday = elapsedDaysSinceEpoch(System.currentTimeMillis());

    if (daysFromEpochToProvidedDate == daysFromEpochToToday || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (daysFromEpochToProvidedDate - daysFromEpochToToday < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (daysFromEpochToProvidedDate < daysFromEpochToToday + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}
 
Example 11
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users. As classy and polished a user experience as "1474061664" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context               Context to use for resource localization
 * @param normalizedUtcMidnight The date in milliseconds (UTC midnight)
 * @param showFullDate          Used to show a fuller-version of the date, which always
 *                              contains either the day of the week, today, or tomorrow, in
 *                              addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long normalizedUtcMidnight, boolean showFullDate) {

    /*
     * NOTE: localDate should be localDateMidnightMillis and should be straight from the
     * database
     *
     * Since we normalized the date when we inserted it into the database, we need to take
     * that normalized date and produce a date (in UTC time) that represents the local time
     * zone at midnight.
     */
    long localDate = getLocalMidnightFromNormalizedUtcDate(normalizedUtcMidnight);

    /*
     * In order to determine which day of the week we are creating a date string for, we need
     * to compare the number of days that have passed since the epoch (January 1, 1970 at
     * 00:00 GMT)
     */
    long daysFromEpochToProvidedDate = elapsedDaysSinceEpoch(localDate);

    /*
     * As a basis for comparison, we use the number of days that have passed from the epoch
     * until today.
     */
    long daysFromEpochToToday = elapsedDaysSinceEpoch(System.currentTimeMillis());

    if (daysFromEpochToProvidedDate == daysFromEpochToToday || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (daysFromEpochToProvidedDate - daysFromEpochToToday < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (daysFromEpochToProvidedDate < daysFromEpochToToday + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}
 
Example 12
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users.  As classy and polished a user experience as "20140102" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow"
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context      Context to use for resource localization
 * @param dateInMillis The date in milliseconds (UTC)
 * @param showFullDate Used to show a fuller-version of the date, which always contains either
 *                     the day of the week, today, or tomorrow, in addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long dateInMillis, boolean showFullDate) {

    long localDate = getLocalDateFromUTC(dateInMillis);
    long dayNumber = getDayNumber(localDate);
    long currentDayNumber = getDayNumber(System.currentTimeMillis());

    if (dayNumber == currentDayNumber || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (dayNumber - currentDayNumber < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (dayNumber < currentDayNumber + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}
 
Example 13
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users.  As classy and polished a user experience as "20140102" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow"
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context      Context to use for resource localization
 * @param dateInMillis The date in milliseconds (UTC)
 * @param showFullDate Used to show a fuller-version of the date, which always contains either
 *                     the day of the week, today, or tomorrow, in addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long dateInMillis, boolean showFullDate) {

    long localDate = getLocalDateFromUTC(dateInMillis);
    long dayNumber = getDayNumber(localDate);
    long currentDayNumber = getDayNumber(System.currentTimeMillis());

    if (dayNumber == currentDayNumber || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (dayNumber - currentDayNumber < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (dayNumber < currentDayNumber + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}
 
Example 14
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users. As classy and polished a user experience as "1474061664" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context               Context to use for resource localization
 * @param normalizedUtcMidnight The date in milliseconds (UTC midnight)
 * @param showFullDate          Used to show a fuller-version of the date, which always
 *                              contains either the day of the week, today, or tomorrow, in
 *                              addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long normalizedUtcMidnight, boolean showFullDate) {

    /*
     * NOTE: localDate should be localDateMidnightMillis and should be straight from the
     * database
     *
     * Since we normalized the date when we inserted it into the database, we need to take
     * that normalized date and produce a date (in UTC time) that represents the local time
     * zone at midnight.
     */
    long localDate = getLocalMidnightFromNormalizedUtcDate(normalizedUtcMidnight);

    /*
     * In order to determine which day of the week we are creating a date string for, we need
     * to compare the number of days that have passed since the epoch (January 1, 1970 at
     * 00:00 GMT)
     */
    long daysFromEpochToProvidedDate = elapsedDaysSinceEpoch(localDate);

    /*
     * As a basis for comparison, we use the number of days that have passed from the epoch
     * until today.
     */
    long daysFromEpochToToday = elapsedDaysSinceEpoch(System.currentTimeMillis());

    if (daysFromEpochToProvidedDate == daysFromEpochToToday || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (daysFromEpochToProvidedDate - daysFromEpochToToday < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (daysFromEpochToProvidedDate < daysFromEpochToToday + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}
 
Example 15
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users. As classy and polished a user experience as "1474061664" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context               Context to use for resource localization
 * @param normalizedUtcMidnight The date in milliseconds (UTC midnight)
 * @param showFullDate          Used to show a fuller-version of the date, which always
 *                              contains either the day of the week, today, or tomorrow, in
 *                              addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long normalizedUtcMidnight, boolean showFullDate) {

    /*
     * NOTE: localDate should be localDateMidnightMillis and should be straight from the
     * database
     *
     * Since we normalized the date when we inserted it into the database, we need to take
     * that normalized date and produce a date (in UTC time) that represents the local time
     * zone at midnight.
     */
    long localDate = getLocalMidnightFromNormalizedUtcDate(normalizedUtcMidnight);

    /*
     * In order to determine which day of the week we are creating a date string for, we need
     * to compare the number of days that have passed since the epoch (January 1, 1970 at
     * 00:00 GMT)
     */
    long daysFromEpochToProvidedDate = elapsedDaysSinceEpoch(localDate);

    /*
     * As a basis for comparison, we use the number of days that have passed from the epoch
     * until today.
     */
    long daysFromEpochToToday = elapsedDaysSinceEpoch(System.currentTimeMillis());

    if (daysFromEpochToProvidedDate == daysFromEpochToToday || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (daysFromEpochToProvidedDate - daysFromEpochToToday < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (daysFromEpochToProvidedDate < daysFromEpochToToday + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}
 
Example 16
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users. As classy and polished a user experience as "1474061664" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context               Context to use for resource localization
 * @param normalizedUtcMidnight The date in milliseconds (UTC midnight)
 * @param showFullDate          Used to show a fuller-version of the date, which always
 *                              contains either the day of the week, today, or tomorrow, in
 *                              addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long normalizedUtcMidnight, boolean showFullDate) {

    /*
     * NOTE: localDate should be localDateMidnightMillis and should be straight from the
     * database
     *
     * Since we normalized the date when we inserted it into the database, we need to take
     * that normalized date and produce a date (in UTC time) that represents the local time
     * zone at midnight.
     */
    long localDate = getLocalMidnightFromNormalizedUtcDate(normalizedUtcMidnight);

    /*
     * In order to determine which day of the week we are creating a date string for, we need
     * to compare the number of days that have passed since the epoch (January 1, 1970 at
     * 00:00 GMT)
     */
    long daysFromEpochToProvidedDate = elapsedDaysSinceEpoch(localDate);

    /*
     * As a basis for comparison, we use the number of days that have passed from the epoch
     * until today.
     */
    long daysFromEpochToToday = elapsedDaysSinceEpoch(System.currentTimeMillis());

    if (daysFromEpochToProvidedDate == daysFromEpochToToday || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (daysFromEpochToProvidedDate - daysFromEpochToToday < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (daysFromEpochToProvidedDate < daysFromEpochToToday + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}
 
Example 17
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users.  As classy and polished a user experience as "20140102" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow"
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context      Context to use for resource localization
 * @param dateInMillis The date in milliseconds (UTC)
 * @param showFullDate Used to show a fuller-version of the date, which always contains either
 *                     the day of the week, today, or tomorrow, in addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long dateInMillis, boolean showFullDate) {

    long localDate = getLocalDateFromUTC(dateInMillis);
    long dayNumber = getDayNumber(localDate);
    long currentDayNumber = getDayNumber(System.currentTimeMillis());

    if (dayNumber == currentDayNumber || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (dayNumber - currentDayNumber < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (dayNumber < currentDayNumber + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}
 
Example 18
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users.  As classy and polished a user experience as "20140102" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow"
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context      Context to use for resource localization
 * @param dateInMillis The date in milliseconds (UTC)
 * @param showFullDate Used to show a fuller-version of the date, which always contains either
 *                     the day of the week, today, or tomorrow, in addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long dateInMillis, boolean showFullDate) {

    long localDate = getLocalDateFromUTC(dateInMillis);
    long dayNumber = getDayNumber(localDate);
    long currentDayNumber = getDayNumber(System.currentTimeMillis());

    if (dayNumber == currentDayNumber || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (dayNumber - currentDayNumber < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (dayNumber < currentDayNumber + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}
 
Example 19
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users.  As classy and polished a user experience as "20140102" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow"
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context      Context to use for resource localization
 * @param dateInMillis The date in milliseconds (UTC)
 * @param showFullDate Used to show a fuller-version of the date, which always contains either
 *                     the day of the week, today, or tomorrow, in addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long dateInMillis, boolean showFullDate) {

    long localDate = getLocalDateFromUTC(dateInMillis);
    long dayNumber = getDayNumber(localDate);
    long currentDayNumber = getDayNumber(System.currentTimeMillis());

    if (dayNumber == currentDayNumber || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (dayNumber - currentDayNumber < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (dayNumber < currentDayNumber + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}
 
Example 20
Source File: SunshineDateUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to convert the database representation of the date into something to display
 * to users. As classy and polished a user experience as "1474061664" is, we can do better.
 * <p/>
 * The day string for forecast uses the following logic:
 * For today: "Today, June 8"
 * For tomorrow:  "Tomorrow
 * For the next 5 days: "Wednesday" (just the day name)
 * For all days after that: "Mon, Jun 8" (Mon, 8 Jun in UK, for example)
 *
 * @param context               Context to use for resource localization
 * @param normalizedUtcMidnight The date in milliseconds (UTC midnight)
 * @param showFullDate          Used to show a fuller-version of the date, which always
 *                              contains either the day of the week, today, or tomorrow, in
 *                              addition to the date.
 *
 * @return A user-friendly representation of the date such as "Today, June 8", "Tomorrow",
 * or "Friday"
 */
public static String getFriendlyDateString(Context context, long normalizedUtcMidnight, boolean showFullDate) {

    /*
     * NOTE: localDate should be localDateMidnightMillis and should be straight from the
     * database
     *
     * Since we normalized the date when we inserted it into the database, we need to take
     * that normalized date and produce a date (in UTC time) that represents the local time
     * zone at midnight.
     */
    long localDate = getLocalMidnightFromNormalizedUtcDate(normalizedUtcMidnight);

    /*
     * In order to determine which day of the week we are creating a date string for, we need
     * to compare the number of days that have passed since the epoch (January 1, 1970 at
     * 00:00 GMT)
     */
    long daysFromEpochToProvidedDate = elapsedDaysSinceEpoch(localDate);

    /*
     * As a basis for comparison, we use the number of days that have passed from the epoch
     * until today.
     */
    long daysFromEpochToToday = elapsedDaysSinceEpoch(System.currentTimeMillis());

    if (daysFromEpochToProvidedDate == daysFromEpochToToday || showFullDate) {
        /*
         * If the date we're building the String for is today's date, the format
         * is "Today, June 24"
         */
        String dayName = getDayName(context, localDate);
        String readableDate = getReadableDateString(context, localDate);
        if (daysFromEpochToProvidedDate - daysFromEpochToToday < 2) {
            /*
             * Since there is no localized format that returns "Today" or "Tomorrow" in the API
             * levels we have to support, we take the name of the day (from SimpleDateFormat)
             * and use it to replace the date from DateUtils. This isn't guaranteed to work,
             * but our testing so far has been conclusively positive.
             *
             * For information on a simpler API to use (on API > 18), please check out the
             * documentation on DateFormat#getBestDateTimePattern(Locale, String)
             * https://developer.android.com/reference/android/text/format/DateFormat.html#getBestDateTimePattern
             */
            String localizedDayName = new SimpleDateFormat("EEEE").format(localDate);
            return readableDate.replace(localizedDayName, dayName);
        } else {
            return readableDate;
        }
    } else if (daysFromEpochToProvidedDate < daysFromEpochToToday + 7) {
        /* If the input date is less than a week in the future, just return the day name. */
        return getDayName(context, localDate);
    } else {
        int flags = DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_NO_YEAR
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_WEEKDAY;

        return DateUtils.formatDateTime(context, localDate, flags);
    }
}