Alex
Alex

Reputation: 1486

Converting Non English Dates to Standard Java Date

I am working on project where I need to convert Non English dates to standard JAVA Calender/Date format. For example date like helmikuu 13, 2013 should be converted to 13 February, 2013.

I think its doable using Java Simple Date Format Locate Function. I tried to use this code but it throws Unparseable date: "helmikuu 13, 2013" error.

public static void main(String args[]) throws Exception {
        String dateInFin = "helmikuu 13, 2013";
        Locale localeFi = new Locale("fi", "FI");
        DateFormat dateFormatFin = new SimpleDateFormat("MMMMM dd, yyyy");
        dateFormatFin.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, localeFi);

        System.out.println("Locale: "+localeFi.getDisplayName());
        System.out.println(dateFormatFin.parse(dateInFin));

    }

Upvotes: 0

Views: 2752

Answers (4)

Ernestas Kardzys
Ernestas Kardzys

Reputation: 1727

For example you can do like this (example is how to convert from French locale to Lihuanian):

@Test
    public void testTestLocale() throws Exception {
        Date date = new Date();

        // Get a France locale
        Locale localeFR = Locale.FRANCE;

        // Create a Lithuanian Locale
        Locale localeLT = new Locale("lt", "LT");

        // Get a date time formatter for display in France
        DateFormat fullDateFormatFR =DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,localeFR);

        // Get a date time formatter for display in Lithuania
        DateFormat fullDateFormatLT = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, localeLT);

        System.out.println("Locale: "+localeFR.getDisplayName());
        System.out.println(fullDateFormatFR.format(date));
        System.out.println("Locale: "+localeLT.getDisplayName());
        System.out.println(fullDateFormatLT.format(date));
    }

EDIT: Working sample converting date from Finish to US format:

@Test
    public void testConvertDateFromFinToEn() throws Exception {
        String dateInFin = "helmikuu 13, 2013";
        Locale localeFi = new Locale("fi", "FI");

        DateFormat dateFormatFi = new SimpleDateFormat("MMMM dd, yyyy", localeFi);
        // Get Date object
        Date date = dateFormatFi.parse(dateInFin);

        // Convert to US date format
        Locale localeUS = new Locale("en", "US");
        DateFormat dateFormatUS = new SimpleDateFormat("dd MMMM, yyyy", localeUS);

        // Print in US format
        System.out.println(dateFormatUS.format(date));
    }

Upvotes: 4

Alex
Alex

Reputation: 1486

Worked! !

                Locale localeFi = new Locale("fi", "FI");
                SimpleDateFormat dateFormatFin = new SimpleDateFormat("MMMMM dd, yyyy", localeFi);
                System.out.println(dateFormatFin.parse(dateInFin));

Upvotes: 0

Hemant Metalia
Hemant Metalia

Reputation: 30638

you can use SimpleDateFormat with local

SimpleDateFormat

public SimpleDateFormat(String pattern,
                        Locale locale)

    Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the given locale. Note: This constructor may not support all locales. For full coverage, use the factory methods in the DateFormat class.

    Parameters:
        pattern - the pattern describing the date and time format
        locale - the locale whose date format symbols should be used 

Upvotes: 0

Peter Walser
Peter Walser

Reputation: 15696

Using java.text.DateFormat:

DateFormat format = new SimpleDateFormat("MMMM d, yyyy");

Use the parse(String) and format() methods to parse and format dates.

Upvotes: 0

Related Questions