Convert date from default locale to english locale

user2442638 picture user2442638 · Feb 11, 2014 · Viewed 8.6k times · Source

I have saved dates by formatting them using SimpleDateFormat.

DateFormat dateForm = new SimpleDateFormat("HH mm ss dd MMM ''yy"); 
            String dateOutput = dateForm.format(new Date());

This uses the default Locale of the device, for example French or Spanish.

How do I convert this string (formatted for a different Locale) back to a Date object formatted with Locale.ENGLISH?

Currently when I try to convert the string back into a date, I get an unparseable date exception. This is caused by the fact the the date was saved using a different Locale

Answer

BigT picture BigT · Feb 11, 2014

This can work for you

SimpleDateFormat sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.ENGLISH);
String dateOutput = sdf.format(new Date());

EDIT

The way I am thinking is to break it down with Calendar object and formatting it in another language. Try this

SimpleDateFormat sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.getDefault());
Calendar cal = Calendar.getInstance();
int day = 0, month = 0, year = 0, hour = 0, minute = 0, sec = 0;
String loc = Locale.getDefault().getDisplayLanguage();
try {
    Date testDate = sdf.parse(date);
    cal.setTime(testDate);
    sec = cal.get(Calendar.SECOND);
    minute = cal.get(Calendar.MINUTE);
    hour = cal.get(Calendar.HOUR);
    day = cal.get(Calendar.DATE);
    month = cal.get(Calendar.MONTH);
    year = cal.get(Calendar.YEAR);
} catch (ParseException e) {
    if(loc.equals(Locale.ENGLISH.getDisplayLanguage())){
        sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.FRENCH);
        try {
            Date testDate = sdf.parse(date);
            cal.setTime(testDate);
            sec = cal.get(Calendar.SECOND);
            minute = cal.get(Calendar.MINUTE);
            hour = cal.get(Calendar.HOUR);
            day = cal.get(Calendar.DATE);
            month = cal.get(Calendar.MONTH);
            year = cal.get(Calendar.YEAR);
        } catch (ParseException ee) {
            sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.FRENCH);
            try {
                Date testDate = sdf.parse(date);
                cal.setTime(testDate);
                sec = cal.get(Calendar.SECOND);
                minute = cal.get(Calendar.MINUTE);
                hour = cal.get(Calendar.HOUR);
                day = cal.get(Calendar.DATE);
                month = cal.get(Calendar.MONTH);
                year = cal.get(Calendar.YEAR);
            } catch (ParseException eex) {

            }
        }
    }
    else if (loc.equals(Locale.FRENCH.getDisplayLanguage())){
        sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.ENGLISH);
        try {
            Date testDate = sdf.parse(date);
            cal.setTime(testDate);
            sec = cal.get(Calendar.SECOND);
            minute = cal.get(Calendar.MINUTE);
            hour = cal.get(Calendar.HOUR);
            day = cal.get(Calendar.DATE);
            month = cal.get(Calendar.MONTH);
            year = cal.get(Calendar.YEAR);
        } catch (ParseException fe) {

        }
    }
}
cal.set(Calendar.SECOND, sec);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.HOUR, hour);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
if(loc.equals(Locale.ENGLISH.getDisplayLanguage()))
        sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.getDefault());
else if(loc.equals(Locale.FRENCH.getDisplayLanguage()))
    sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.getDefault());
Date convertedLangDate = cal.getTime();
String newDate = sdf.format(convertedLangDate);

This does work for me however it is not the most elegant of solutions. Tweek it for your code.