38

How can create text representation of some date, that takes locale into account and contains only day and month (no year)?

Following code gives me something like 23/09/2010

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

I want to get 23/09

fhucho
  • 34,062
  • 40
  • 136
  • 186
  • how are you declaring date? Are you using SimpleDateFormat? – Albinoswordfish Sep 24 '10 at 21:02
  • I have number milliseconds since epoch and want to convert this to something like "23/09". – fhucho Sep 24 '10 at 21:05
  • Ah, so you want the date formatted according to the default format for the given or default locale, but with the year portion removed. So `September 23` would look like `09/23` in one locale, but `23/09` in another. I realize that's essentially what you say in your question, but I guess the example threw me off. – Bert F Sep 24 '10 at 21:46

10 Answers10

56

This will work for Android, in case someone needs it:

int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR;
String monthAndDayText = DateUtils.formatDateTime(context, date, flags);
unify
  • 6,161
  • 4
  • 33
  • 34
  • however the no_year option seems to get ignored at least on a recent Sony d203 android 4.3..... What a shame – hotzen Sep 18 '14 at 21:33
  • Unfortunately, this method uses default date time format preferences (order and format) and does not to obey Android's date format setting. – Rich Ehmer Jan 05 '15 at 17:12
  • Plus: where's the timezone coming in? A time in millis will never work without a time zone. Why do I always get the bad feeling that even in Android, all date/time things a badly broken?? – Zordid May 08 '15 at 19:45
  • actually it's behaves strange, for example I have CZ locale (date separator is . like 1.2.2000) and flags setting `DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NO_MONTH_DAY | DateUtils.FORMAT_NUMERIC_DATE` gets slash separator like 2/2000 – kubo Jan 21 '16 at 13:58
  • When you use flags without `FORMAT_NO_MONTH_DAY` you gets correct dot separator like 1.2.2000. – kubo Jan 21 '16 at 14:06
  • I tested this on a lot of devices, doesn't work consistently. – Hades Feb 05 '16 at 00:25
  • it returns "21 February", but I want "21 Feb" – user924 Mar 04 '18 at 19:11
  • I wanted **FEB 21** and got it using next flags: `DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_MONTH;` also I don't think we need to use `DateUtils.FORMAT_SHOW_DATE` it doesn't effect anything – user924 Mar 04 '18 at 19:17
26

You could use regex to trim off all y's and any non-alphabetic characters before and after, if any. Here's a kickoff example:

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;
}

You see that this snippet tests it for all locales as well. It looks to work fine for all locales here.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 4
    @Gro: this isn't what the OP is asking. Replace the method by `return new SimpleDateFormat("dd/MM", locale);` and run yourself. You'll see that the locale is only used to represent day/month in localespecific language (in my case, it's only applied in Thai). Try using `MMMM` instead of `MM` and you'll see that month names are translated accordingly. It won't rearrange the pattern. – BalusC Sep 25 '10 at 14:19
  • 1
    You are right, sorry. I misinterpreted the question. I'm deleting my answer and upvoting yours. – Grodriguez Sep 25 '10 at 14:28
  • I did it similarly, see my answer below. – fhucho Sep 25 '10 at 15:51
  • 3
    I tried this approach to solve a bug found when using Japanese as Locale. We just want MMMM and Year, and we want them in the correct order according to Locale. The unmodified sdf pattern looks like this: y年M月d日, and the regexped pattern looks like this: 年M月d日. The 年-character (year) still remains. It seems impossible to get a flexible but still Locale controlled date formatting. :( – Mattias Jun 13 '12 at 12:03
  • 8
    This does NOT work fine for all locales. For example, the correct pattern for the german locale would be `dd.MM.`, not `dd.MM`. I assume this is similar for other locales that use dots to separate day/month/year. So, the regex should probably be changed to `[^\\p{Alpha}\\.]*y+[^\\p{Alpha}]*` – Stefan Frye Sep 02 '13 at 15:46
  • 1
    This crashes if the locale contains a quote. I.e. "d 'de' MMMM 'de' y" becomes "d 'de' MMMM 'de" which crashes due to mismatched quotes. – Learn OpenGL ES Dec 17 '13 at 20:06
  • 1
    this does not work for Spanish locale :) locale pattern is [d' de 'MMMM' de 'yyyy] and after replacing it turns to [d' de 'MMMM' de '] - and here I have "Unterminated quote" exception – Vladimir Kishlaly Sep 29 '14 at 14:07
  • @Vladimir (and @Learn): as asked and answered, use `DateFormat.getDateInstance(DateFormat.SHORT, locale)`. – BalusC Sep 29 '14 at 14:56
  • @BalusC could you alter you answer to include the same for DateFormat.Short ? and remove the date as opposed to the year as well please – Hades Feb 05 '16 at 01:46
  • Please, take a look at my solution where I also take into account locales which influences on day/month 's oeder. http://stackoverflow.com/a/38521141/1808829 – Ayaz Alifov Jul 22 '16 at 09:08
  • to get "**FEB 20**" use next solution https://stackoverflow.com/questions/3790918/format-date-without-year#comment85204004_23304148 – user924 Mar 04 '18 at 19:19
  • If all you want to do is remove years, `String newPattern = sdf.toLocalizedPattern().replaceAll("y", "");` should be good enough – Matt Jenje Dec 21 '20 at 13:00
10

Just wanted to contribute another modification removing year from the pattern, which works well for DateFormat.MEDIUM, even in locales such as pt_PT (d 'de' MMM 'de' yyyy) or 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;
 }
Andrzej Pronobis
  • 33,828
  • 17
  • 76
  • 92
4

The following is a utility class which gives you the date format without year. I list all the date formats known to Java using the following code.

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

And then manually remove the year part. It returns two flavors of date format, normal and abbreviated, ie Monday, March 18 vs Mon, Mar 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");
    }
}
Don
  • 221
  • 1
  • 3
  • 3
    You are aware of the fact, the `String.replaceAll` methods use regular expressions to find matches AND work on the replacement string? For "E+" that is okay, but when replacing MMMM with MMM you really want a "simple" `String.replace(CharSequence, CharSequence)` because you don't use any regular expressions! – Zordid Feb 26 '14 at 10:57
  • omg you don't need to do it, java/android already have needed classes/utils for such tasks.. – user924 Mar 04 '18 at 21:49
4

I did it this way:

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
}
fhucho
  • 34,062
  • 40
  • 136
  • 186
3

The main difficulty here is that in some locales the order of month and day is different. I solved it in a way which is different from those which presented here.

    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;
}
Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
2

Although Andrzej Pronobis's answer is very good, it doesn't work for instance with ZH local. I ended up with manual removing year from localized pattern. This function was tested for all locals for SHORT, MEDIUM, LONG and FULL formats.

public String removeYearFromPattern(String pattern) throws IllegalArgumentException {
    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();
}

Function above can be tested by running:

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()));
}

I know it is not as elegant as regex, but it is a little bit faster and works for all locals (if I am able to recognize).

zelig74
  • 482
  • 1
  • 6
  • 15
  • This actually works. I need to remove the year from DateFormat.LONG and the regex pattern solutions seem all to have problems with removing points (e.g. German locale) or not removing enough e.g. the last 'de' from the Portuguese local or removing just the last ' from 'de', thus leaving an unterminated quote in the pattern... But this one seems to work. – Andy Jul 25 '19 at 19:37
1

Using com.ibm.icu library:

import com.ibm.icu.text.DateFormat;

DateFormat.getPatternInstance(DateFormat.MONTH_DAY, locale).format(date);
Maxim Kachurovskiy
  • 2,992
  • 2
  • 21
  • 24
1

If you use Android, you might be able to use getBestDateTimePattern , and just put there all the fields you want to allow. It should automatically put the correct order and special characters according to the current locale.

Sadly, it requires API 18+ .

EDIT: This seems like a not-so-good choice, sadly. I've written about it here: http://code.google.com/p/android/issues/detail?id=171591


So, this is what I've done:

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;
}

This code works on any locale available, on Android 5.0.2 .

Here's a sample of how to check it on on locales:

    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);
    }
android developer
  • 114,585
  • 152
  • 739
  • 1,270
-4

or

   SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM d');
    dateFormat.format(date);
Puran
  • 984
  • 6
  • 15