格式化日期不含年份

时间:2010-09-24 20:56:15

标签: java date

如何创建某个日期的文本表示,考虑区域设置并且仅包含日期和月份(无年份)?

以下代码为我提供了类似 23/09/2010

的内容
DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault()).format(date);

我想 23/09

10 个答案:

答案 0 :(得分:43)

这适用于 Android ,以防有人需要它:

int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR;
String monthAndDayText = DateUtils.formatDateTime(context, date, flags);

答案 1 :(得分:23)

您可以使用正则表达式修剪所有y以及之前和之后的任何非字母字符(如果有)。这是一个启动示例:

public static void main(String[] args) throws Exception {
    for (Locale locale : Locale.getAvailableLocales()) {
        DateFormat df = getShortDateInstanceWithoutYears(locale);
        System.out.println(locale + ": " + df.format(new Date()));      
    }
}

public static DateFormat getShortDateInstanceWithoutYears(Locale locale) {
    SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale);
    sdf.applyPattern(sdf.toPattern().replaceAll("[^\\p{Alpha}]*y+[^\\p{Alpha}]*", ""));
    return sdf;
}

您会看到此代码段也会针对所有区域设置对其进行测试。它看起来适用于所有语言环境。

答案 2 :(得分:8)

只想在模式中提供另一个修改删除年份,这适用于DateFormat.MEDIUM,即使在诸如pt_PT(d'de'MMM'de'yyyy)或lv_LV(y。'gada'd。 MMM)。

public static DateFormat getMediumDateInstanceWithoutYears()
 {
   SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.MEDIUM);
   sdf.applyPattern(sdf.toPattern().replaceAll(
       "([^\\p{Alpha}']|('[\\p{Alpha}]+'))*y+([^\\p{Alpha}']|('[\\p{Alpha}]+'))*",
       ""));
   return sdf;
 }

答案 3 :(得分:4)

我是这样做的:

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
if (dateFormat instanceof SimpleDateFormat) {
    SimpleDateFormat simpleDateFormat = (SimpleDateFormat) dateFormat;
    String pattern = simpleDateFormat.toPattern();

    // I modified the pattern here so that dd.MM.yyyy would result to dd.MM

    simpleDateFormat.applyPattern(modifiedPattern);

    ... etc
}

答案 4 :(得分:4)

以下是一个实用工具类,它为您提供不带年份的日期格式。我使用以下代码列出了Java已知的所有日期格式。

Locale[] locales = SimpleDateFormat.getAvailableLocales();
for (int i = 0; i < locales.length; i++) {
    Locale locale = locales[i];
    DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale);
}

然后手动删除年份部分。它返回两种日期格式,正常和缩写,即3月18日星期一与3月18日星期一。

public class DateFormatWithoutYear {

    private static Map<String, String> formats = new HashMap<String, String>();
    private static String DEFAULT_FORMAT = "EEEE, d MMMM";
    static {
        formats.put("_af", "EEEE dd MMMM");
        formats.put("_am", "EEEE, d MMMM");
        formats.put("_az", "EEEE, d, MMMM");
        formats.put("_be", "EEEE, d MMMM");
        formats.put("_bg", "dd MMMM, EEEE");
        formats.put("BG_bg", "dd MMMM, EEEE");
        formats.put("_ca", "EEEE d MMMM");
        formats.put("ES_ca", "EEEE d MMMM");
        formats.put("_cs", "EEEE, d. MMMM");
        formats.put("CZ_cs", "EEEE, d. MMMM");
        formats.put("_da", "EEEE 'den' d. MMMM");
        formats.put("DK_da", "EEEE 'den' d. MMMM");
        formats.put("_de", "EEEE, d. MMMM");
        formats.put("AT_de", "EEEE, dd. MMMM");
        formats.put("BE_de", "EEEE, d. MMMM");
        formats.put("CH_de", "EEEE, d. MMMM");
        formats.put("DE_de", "EEEE, d. MMMM");
        formats.put("LI_de", "EEEE, d. MMMM");
        formats.put("LU_de", "EEEE, d. MMMM");
        formats.put("_el", "EEEE, d MMMM");
        formats.put("GR_el", "EEEE, d MMMM");
        formats.put("_en", "EEEE, MMMM d");
        formats.put("AU_en", "EEEE, d MMMM");
        formats.put("BE_en", "EEEE d MMMM");
        formats.put("BW_en", "EEEE dd MMMM");
        formats.put("BZ_en", "EEEE, MMMM d");
        formats.put("CA_en", "EEEE, d MMMM");
        formats.put("GB_en", "EEEE, d MMMM");
        formats.put("HK_en", "EEEE, d MMMM");
        formats.put("IE_en", "EEEE d MMMM");
        formats.put("IN_en", "EEEE d MMMM");
        formats.put("JM_en", "EEEE, MMMM d");
        formats.put("MH_en", "EEEE, MMMM d");
        formats.put("MT_en", "EEEE, d MMMM");
        formats.put("NA_en", "EEEE, MMMM d");
        formats.put("NZ_en", "EEEE, d MMMM");
        formats.put("PH_en", "EEEE, MMMM d");
        formats.put("PK_en", "EEEE, MMMM d");
        formats.put("RH_en", "EEEE dd MMMM");
        formats.put("SG_en", "EEEE, d MMMM");
        formats.put("TT_en", "EEEE, MMMM d");
        formats.put("US_en", "EEEE, MMMM d");
        formats.put("VI_en", "EEEE, MMMM d");
        formats.put("ZA_en", "EEEE dd MMMM");
        formats.put("ZW_en", "EEEE dd MMMM");
        formats.put("_es", "EEEE d 'de' MMMM");
        formats.put("AR_es", "EEEE d 'de' MMMM");
        formats.put("BO_es", "EEEE d 'de' MMMM");
        formats.put("CL_es", "EEEE d 'de' MMMM");
        formats.put("CO_es", "EEEE d 'de' MMMM");
        formats.put("CR_es", "EEEE d 'de' MMMM");
        formats.put("DO_es", "EEEE d 'de' MMMM");
        formats.put("EC_es", "EEEE d 'de' MMMM");
        formats.put("ES_es", "EEEE d 'de' MMMM");
        formats.put("GT_es", "EEEE d 'de' MMMM");
        formats.put("HN_es", "EEEE dd 'de' MMMM");
        formats.put("MX_es", "EEEE d 'de' MMMM");
        formats.put("NI_es", "EEEE d 'de' MMMM");
        formats.put("PA_es", "EEEE d 'de' MMMM");
        formats.put("PE_es", "EEEE d 'de' MMMM");
        formats.put("PR_es", "EEEE d 'de' MMMM");
        formats.put("PY_es", "EEEE d 'de' MMMM");
        formats.put("SV_es", "EEEE d 'de' MMMM");
        formats.put("US_es", "EEEE d 'de' MMMM");
        formats.put("UY_es", "EEEE d 'de' MMMM");
        formats.put("VE_es", "EEEE d 'de' MMMM");
        formats.put("_et", "EEEE, d. MMMM");
        formats.put("_eu", "EEEE, MMMM'ren' dd'a'");
        formats.put("_fi", "cccc, d. MMMM");
        formats.put("FI_fi", "cccc, d. MMMM");
        formats.put("_fil", "EEEE, MMMM dd");
        formats.put("PH_fil", "EEEE, MMMM dd");
        formats.put("_fr", "EEEE d MMMM");
        formats.put("BE_fr", "EEEE d MMMM");
        formats.put("CA_fr", "EEEE d MMMM");
        formats.put("CH_fr", "EEEE, d MMMM");
        formats.put("FR_fr", "EEEE d MMMM");
        formats.put("LU_fr", "EEEE d MMMM");
        formats.put("MC_fr", "EEEE d MMMM");
        formats.put("_gl", "EEEE dd MMMM");
        formats.put("_iw", "EEEE, d בMMMM");
        formats.put("IL_iw", "EEEE, d בMMMM");
        formats.put("_hi", "EEEE, d MMMM");
        formats.put("IN_hi", "EEEE, d MMMM");
        formats.put("_hr", "EEEE, d. MMMM");
        formats.put("HR_hr", "EEEE, d. MMMM");
        formats.put("_hu", "MMMM d., EEEE");
        formats.put("HU_hu", "MMMM d., EEEE");
        formats.put("_hy", "EEEE, MMMM d");
        formats.put("_in", "EEEE, dd MMMM");
        formats.put("ID_in", "EEEE, dd MMMM");
        formats.put("_it", "EEEE d MMMM");
        formats.put("CH_it", "EEEE, d MMMM");
        formats.put("IT_it", "EEEE d MMMM");
        formats.put("_ja", "M月d日EEEE");
        formats.put("JP_ja", "M月d日EEEE");
        formats.put("_ka", "EEEE, MMMM dd");
        formats.put("_kk", "EEEE, d MMMM");
        formats.put("_ko", "M월 d일 EEEE");
        formats.put("KR_ko", "M월 d일 EEEE");
        formats.put("_lt", "'m'. MMMM d 'd'., EEEE");
        formats.put("LT_lt", "'m'. MMMM d 'd'., EEEE");
        formats.put("_lv", "EEEE, d. MMMM");
        formats.put("LV_lv", "EEEE, d. MMMM");
        formats.put("_mk", "EEEE, dd MMMM");
        formats.put("_ms", "EEEE, d MMMM");
        formats.put("_nb", "EEEE d. MMMM");
        formats.put("NO_nb", "EEEE d. MMMM");
        formats.put("_nl", "EEEE d MMMM");
        formats.put("BE_nl", "EEEE d MMMM");
        formats.put("NL_nl", "EEEE d MMMM");
        formats.put("_pl", "EEEE, d MMMM");
        formats.put("PL_pl", "EEEE, d MMMM");
        formats.put("_ps", "EEEE د MMMM d");
        formats.put("_pt", "EEEE, d 'de' MMMM");
        formats.put("BR_pt", "EEEE, d 'de' MMMM");
        formats.put("PT_pt", "EEEE, d 'de' MMMM");
        formats.put("_rm", "EEEE, d. MMMM");
        formats.put("_ro", "EEEE, d MMMM");
        formats.put("RO_ro", "EEEE, d MMMM");
        formats.put("_ru", "EEEE, d MMMM");
        formats.put("RU_ru", "EEEE, d MMMM");
        formats.put("UA_ru", "EEEE, d MMMM");
        formats.put("_sk", "EEEE, d. MMMM");
        formats.put("SK_sk", "EEEE, d. MMMM");
        formats.put("_sl", "EEEE, dd. MMMM");
        formats.put("SI_sl", "EEEE, dd. MMMM");
        formats.put("_sr", "EEEE, dd. MMMM");
        formats.put("BA_sr", "EEEE, dd. MMMM");
        formats.put("CS_sr", "EEEE, dd. MMMM");
        formats.put("CYRL_sr", "EEEE, dd. MMMM");
        formats.put("CYRL_sr", "EEEE, dd. MMMM");
        formats.put("CYRL_sr", "EEEE, dd. MMMM");
        formats.put("CYRL_sr", "EEEE, dd. MMMM");
        formats.put("CYRL_sr", "EEEE, dd. MMMM");
        formats.put("CYRL_sr", "EEEE, dd. MMMM");
        formats.put("LATN_sr", "EEEE, dd. MMMM");
        formats.put("LATN_sr", "EEEE, dd. MMMM");
        formats.put("LATN_sr", "EEEE, dd. MMMM");
        formats.put("LATN_sr", "EEEE, dd. MMMM");
        formats.put("LATN_sr", "EEEE, dd. MMMM");
        formats.put("LATN_sr", "EEEE, dd. MMMM");
        formats.put("ME_sr", "EEEE, dd. MMMM");
        formats.put("RS_sr", "EEEE, dd. MMMM");
        formats.put("YU_sr", "EEEE, dd. MMMM");
        formats.put("_sv", "EEEE'en' 'den' d:'e' MMMM");
        formats.put("FI_sv", "EEEE'en' 'den' d:'e' MMMM");
        formats.put("SE_sv", "EEEE'en' 'den' d:'e' MMMM");
        formats.put("_sw", "EEEE, d MMMM");
        formats.put("_th", "EEEEที่ d MMMM G");
        formats.put("TH_th", "EEEEที่ d MMMM G");
        formats.put("_tr", "d MMMM EEEE");
        formats.put("TR_tr", "d MMMM EEEE");
        formats.put("_uk", "EEEE, d MMMM");
        formats.put("UA_uk", "EEEE, d MMMM");
        formats.put("_uz", "EEEE, MMMM dd");
        formats.put("_vi", "EEEE, 'ngày' dd MMMM");
        formats.put("VN_vi", "EEEE, 'ngày' dd MMMM");
        formats.put("_zh", "M月d日EEEE");
        formats.put("CN_zh", "M月d日EEEE");
        formats.put("HK_zh", "M月d日EEEE");
        formats.put("HANS_zh", "M月d日EEEE");
        formats.put("HANS_zh", "M月d日EEEE");
        formats.put("HANS_zh", "M月d日EEEE");
        formats.put("HANS_zh", "M月d日EEEE");
        formats.put("HANT_zh", "M月d日EEEE");
        formats.put("HANT_zh", "M月d日EEEE");
        formats.put("HANT_zh", "MM月dd日EEEE");
        formats.put("HANT_zh", "M月d日EEEE");
        formats.put("MO_zh", "MM月dd日EEEE");
        formats.put("SG_zh", "M月d日EEEE");
        formats.put("TW_zh", "M月d日EEEE");
        formats.put("_zu", "EEEE dd MMMM");
    }

    public static String getLongFormatWithoutYear(Locale locale) {
        if (locale != null) {
            String key = locale.getCountry() + "_" + locale.getLanguage();
            String format = formats.get(key);
            if (format != null) {
                return format;
            }
        }
        return DEFAULT_FORMAT;
    }

    public static String getShortFormatWithoutYear(Locale locale) {
        String longFormat = getLongFormatWithoutYear(locale);
        return longFormat.replaceAll("E+", "E").replaceAll("MMMM", "MMM");
    }
}

答案 5 :(得分:3)

这里的主要困难是在某些区域设置中,月份和日期的顺序不同。我解决它的方式与此处提供的方式不同,并希望它适用于所有情况。

    Date dateObject = ...;

    String dayMonthDateString = getDayMonthDateString(dateObject, Locale.GERMANY);
    Log.i("customDate", "dayMonthDateString = " + dayMonthDateString);

private String getDayMonthDateString(Date date, Locale locale)
{
    try
    {
        boolean dayBeforeMonth = defineDayMonthOrder(locale);

        SimpleDateFormat newDateFormat;

        if (dayBeforeMonth)
        {
            newDateFormat = new SimpleDateFormat("dd/MM", locale);
        }
        else
        {
            newDateFormat = new SimpleDateFormat("MM/dd", locale);
        }

        return newDateFormat.format(date);
    }
    catch (ParseException e)
    {
        e.printStackTrace();
    }

    return null;
}


private boolean defineDayMonthOrder(Locale locale) throws ParseException
{
    String day = "10";
    String month = "11";
    String year = "12";

    String calendarDate = day + "." + month + "." + year;

    SimpleDateFormat format = new SimpleDateFormat("dd.MM.yy");
    Date date = format.parse(calendarDate);

    String localizedDate = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT, locale).format(date);

    int indexOfDay = localizedDate.indexOf(day);
    int indexOfMonth = localizedDate.indexOf(month);

    return indexOfDay < indexOfMonth;
}

答案 6 :(得分:1)

使用com.ibm.icu库:

import com.ibm.icu.text.DateFormat;

DateFormat.getPatternInstance(DateFormat.MONTH_DAY, locale).format(date);

答案 7 :(得分:1)

如果您使用Android,则可以使用getBestDateTimePattern,并将所有要包含的字段放在那里。它应该根据当前的语言环境自动输入正确的顺序和特殊字符。

可悲的是,它需要API 18+。

编辑:遗憾的是,这似乎是一个不太好的选择。我在这里写过: http://code.google.com/p/android/issues/detail?id=171591

所以,这就是我所做的:

public static String convertDateToString(Context context, Locale locale, final Date date, boolean alsoShowYearIfPossible) {
    if (date == null)
        return "";
    String defaultDateFormat;
    if (locale != null) {
        defaultDateFormat = ((SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale)).toLocalizedPattern();
    } else defaultDateFormat = getDefaultDateFormat(context);
    if (alsoShowYearIfPossible)
        return new SimpleDateFormat(defaultDateFormat, Locale.getDefault()).format(date);
    //need removal of year
    String removedYearFormat = defaultDateFormat.replaceAll("[^{mdMD}]*y+[^{mdMD}]*", "");
    String result = new SimpleDateFormat(removedYearFormat, Locale.getDefault()).format(date);
    //Log.d("AppLog", locale + ": \"" + defaultDateFormat + "\" => \"" + removedYearFormat + "\" =>" + result);
    return result;
}

private static String getDefaultDateFormat(final Context context) {
    String dateFormatString = Settings.System.getString(context.getContentResolver(), Settings.System.DATE_FORMAT);
    if (TextUtils.isEmpty(dateFormatString)) {
        // if device date format is available , use device date order,and if not available ,use default
        final char[] dateFormatOrder = android.text.format.DateFormat.getDateFormatOrder(context);
        if (dateFormatOrder.length == 0)
            dateFormatString = DEFAULT_DATE_FORMAT;
        else {
            // construct the date format based on the device date order
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < dateFormatOrder.length; ++i) {
                final char c = dateFormatOrder[i];
                switch (Character.toLowerCase(c)) {
                    case 'y':
                        sb.append("yyyy");
                        break;
                    case 'm':
                        sb.append("MM");
                        break;
                    case 'd':
                        sb.append("dd");
                        break;
                }
                if (i != dateFormatOrder.length - 1)
                    sb.append('-');
            }
            dateFormatString = sb.toString();
        }
    }
    return dateFormatString;
}

此代码适用于Android 5.0.2上可用的任何区域设置。

以下是如何在区域设置上进行检查的示例:

    Date date = new Date();
    for (Locale locale : Locale.getAvailableLocales()) {
        SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale);
        String localizedDatePattern = sdf.toLocalizedPattern();
        convertDateToString(this,locale,date,false);
    }

答案 8 :(得分:1)

虽然Andrzej Pronobis的答案非常好,但是对于ZH本地而言它并不起作用。我最终手动从本地化模式中删除了一年。此功能已针对SHORT,MEDIUM,LONG和FULL格式的所有本地人进行了测试。

public String removeYearFromPattern(String pattern) throws SCException {
    int yPos = 0;
    while (yPos < pattern.length() && pattern.charAt(yPos) != 'y' && pattern.charAt(yPos) != 'Y') {
        if (pattern.charAt(yPos) == '\'') {
            yPos++;
            while (yPos < pattern.length() && pattern.charAt(yPos) != '\'') yPos++;
        }
        yPos++;
    }
    if (yPos >= pattern.length()) throw new IllegalArgumentException("Did not find year in pattern");
    String validPatternLetters = "EMd";
    // go forward
    int endPos = yPos;
    while (endPos < pattern.length() && validPatternLetters.indexOf(pattern.charAt(endPos)) == -1) {
        endPos++;
        if (endPos < pattern.length() && pattern.charAt(endPos) == '\'') {
            endPos++;
            while (endPos < pattern.length() && pattern.charAt(endPos) != '\'')
                endPos++;
        }
    }
    if (endPos != pattern.length()) validPatternLetters += ',';
    // go backward
    int startPos = yPos;
    while (startPos >= 0 && validPatternLetters.indexOf(pattern.charAt(startPos)) == -1) {
        startPos--;
        if (startPos >= 0 && pattern.charAt(startPos) == '\'') {
            startPos--;
            while (startPos >= 0 && pattern.charAt(startPos) != '\'') startPos--;
        }
    }
    startPos++;
    String yLetters = pattern.substring(startPos, endPos);
    return pattern.replace(yLetters, " ").trim();
}

上面的功能可以通过运行来测试:

for (Locale locale : Locale.getAvailableLocales()) {
    SimpleDateFormat df = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.FULL, locale);
    String dfPattern = "?";
    String newPattern = "?";
    try {
        dfPattern = df.toPattern();
        newPattern = removeYearFromPattern(dfPattern);
        df.applyPattern(newPattern);
    } catch (IllegalArgumentException e) {
        Log.e(TAG, "Error removing year for " + locale + ": " + e.getMessage());
    }
    Log.d(TAG, locale + ": old  " + dfPattern + "; new " + newPattern + "; result " + df.format(new Date()));
}

我知道它不像正则表达式那么优雅,但它更快一点,适用于所有当地人(如果我能够识别)。

答案 9 :(得分:-4)

   SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM d');
    dateFormat.format(date);