Search code examples
javalocalizationdate-format

java local timeformat without year


I like to format the local timeformat into a string without the year. At the moment I am able to show the local format containing the year:

java.text.DateFormat df = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT);
String dateString = df.format(date);

therefore i receive an time string output like

12.03.2012
03/12/2012

for the different countries. Now i like to get a short form like

12.03.
03/12

how would i do this?

thanks for your help!


Solution

  • You can use SimpleDateFormat:

    Date date = new Date();
    java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("MM/dd");
    String dateString = df.format(date);
    

    Output:

    03/15
    

    EDIT:

    After researching locale formats further, and expanding on Peters answer, here's some code to demonstrator differences between toPattern() and toLocalizedPattern():

    import java.text.*
    import java.util.*
    
    ArrayList<Locale> locales = new ArrayList<Locale>();
    locales.add(Locale.US);
    locales.add(Locale.UK);
    locales.add(Locale.GERMANY);
    locales.add(Locale.CHINA);
    
    Date date = new Date();
    
    for(Locale l : locales)
    {
        SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateInstance(DateFormat.SHORT, l);
        String pattern = sdf.toPattern();
        String localizedPattern = sdf.toLocalizedPattern()
        println "country: " + l.getDisplayName();
        println "pattern: " + pattern;
        println "localizedPattern: " + localizedPattern;
    
        try {
            SimpleDateFormat temp = new SimpleDateFormat(localizedPattern, l);
            println "localized pattern re-parsed successfully"
        } catch (IllegalArgumentException e) {
            println "localized pattern re-parsed unsuccessfully: " + e.getMessage();
        }
        SimpleDateFormat df = new SimpleDateFormat(pattern, l);
        String dateString = df.format(date);
        println "resulting date: " + dateString
        String yearlessPattern = pattern.replaceAll("\\W?[Yy]+\\W?", "");
        println "yearlessPattern = " + yearlessPattern;
        SimpleDateFormat yearlessSDF = new SimpleDateFormat(yearlessPattern, l);
        println "resulting date without year: " + yearlessSDF.format(date) + "\n";
    }
    

    Produces following output:

    country: English (United States)
    pattern: M/d/yy
    localizedPattern: M/d/yy
    localized pattern re-parsed successfully
    resulting date: 3/15/12
    yearlessPattern = M/d
    resulting date without year: 3/15
    
    country: English (United Kingdom)
    pattern: dd/MM/yy
    localizedPattern: dd/MM/yy
    localized pattern re-parsed successfully
    resulting date: 15/03/12
    yearlessPattern = dd/MM
    resulting date without year: 15/03
    
    country: German (Germany)
    pattern: dd.MM.yy
    localizedPattern: tt.MM.uu
    localized pattern re-parsed unsuccessfully: Illegal pattern character 't'
    resulting date: 15.03.12
    yearlessPattern = dd.MM
    resulting date without year: 15.03
    
    country: Chinese (China)
    pattern: yy-M-d
    localizedPattern: aa-n-j
    localized pattern re-parsed unsuccessfully: Illegal pattern character 'n'
    resulting date: 12-3-15
    yearlessPattern = M-d
    resulting date without year: 3-15
    

    So in conclusion, to display a localized date without a year:

    String yearlessPattern = DateFormat.getDateInstance(DateFormat.SHORT).toPattern().replaceAll("\\W?[Yy]+\\W?", "");