/** * Same as {@link #formatDate(Context context, String string, boolean longForm)}, with * longForm set to {@code true} by default. * * @param context Valid context * @param string String representation of a date to parse * @return Returns the same date in a cleaned up format. If the supplied string does not look * like a date, return it unchanged. */ public static String formatDate(Context context, String string) { return formatDate(context, string, true); }
/** * Given a call date and the current date, determine which date group the call belongs in. * * @param date The call date. * @param now The current date. * @return The date group the call belongs in. */ private int getDayGroup(long date, long now) { int days = DateUtils.getDayDifference(TIME, date, now); if (days == 0) { return DAY_GROUP_TODAY; } else if (days == 1) { return DAY_GROUP_YESTERDAY; } else { return DAY_GROUP_OTHER; } }
return string; final Calendar cal = parseDate(string, false); final boolean isYearSet = isYearSet(cal); final java.text.DateFormat outFormat; if (!isYearSet) { outFormat = getLocalizedDateFormatWithoutYear(context); } else { outFormat =
return getUtcDate(0, Calendar.FEBRUARY, 29); } else { synchronized (CommonDateUtils.NO_YEAR_DATE_FORMAT) { return getUtcDate(date, true); date = f.parse(string, parsePosition); if (parsePosition.getIndex() == string.length()) { return getUtcDate(date, false);
/** * Returns a SimpleDateFormat object without the year fields by using a regular expression * to eliminate the year in the string pattern. In the rare occurence that the resulting * pattern cannot be reconverted into a SimpleDateFormat, it uses the provided context to * determine whether the month field should be displayed before the day field, and returns * either "MMMM dd" or "dd MMMM" converted into a SimpleDateFormat. */ public static java.text.DateFormat getLocalizedDateFormatWithoutYear(Context context) { final String pattern = ((SimpleDateFormat) SimpleDateFormat.getDateInstance( java.text.DateFormat.LONG)).toPattern(); // Determine the correct regex pattern for year. // Special case handling for Spanish locale by checking for "de" final String yearPattern = pattern.contains( "de") ? "[^Mm]*[Yy]+[^Mm]*" : "[^DdMm]*[Yy]+[^DdMm]*"; try { // Eliminate the substring in pattern that matches the format for that of year return new SimpleDateFormat(pattern.replaceAll(yearPattern, "")); } catch (IllegalArgumentException e) { return new SimpleDateFormat( DateUtils.isMonthBeforeDay(context) ? "MMMM dd" : "dd MMMM"); } }
today.set(Calendar.MILLISECOND, 0); final boolean isYearSet = isYearSet(target); final int targetYear = target.get(Calendar.YEAR); final int targetMonth = target.get(Calendar.MONTH);