+ Start a Discussion
Ken Koellner 1Ken Koellner 1 

date format for current locale

We have need to get the date format for the User's locale.  I wrote the Apex below.  It would return "MM/DD/YYYY" with locale set to US English and "DD/MM/YYYY" for Locale set to UK English.  Anyone have a more elegant solutin to get that string?  I didn't see any methods in Apex other than to get Locate like EN_US which would require a table lookup or case statement to handle every possible locale.
Date myDate = Date.newInstance(2018,12,31);

String myStr = myDate.format();
myStr = myStr.replace('2018','YYYY');
myStr = myStr.replace('12','MM');
myStr = myStr.replace('31','DD');


Ken Koellner 1Ken Koellner 1
I should state my use case.  I want to put a data-picker on a VF page and it takes a string that is the data format as the argument.  It needs to work in all locales.  If I can generate the string above based on the select Locale, I can pass it to the data picker.  Of course there may be other solutions to the problem which is why I mention it.