Search code examples
javaandroiddategregorian-calendar

Date string from GregorianCalendar based on locale


I am creating an Android application. I have a GregorianCalendar variable. I want to get the date from this variable as MM-dd-yyyy if the locale is US or dd-MM-yyyy if the locale is EU and so on. I don't want to hard code each locale string, I want to make it depend on the phone's locale.

The problem is that after looking through tens of similar questions, I still couldn't get this to work. I am guessing it's two or three lines of code, perhaps using SimpleDateFormat or GregorianCalendar.get() or getDisplayName().

Thanks for the help.


Solution

  • try this

        GregorianCalendar gc = ...
        String str = DateFormat.getDateInstance(DateFormat.SHORT).format(gc.getTime());
    

    BTW default date format (SHORT) for US is dd/MM/yy eg 30/05/13