[SalesForce] How to get current user date/time format in apex, e.g. MM/dd/YYYY hh:mm:ss a z

We need to figure out current user date/time format in apex code to send it to our mobile app in order to display data in same way as it is in Salesforce itself.
In other words, it's about getting template like YYYY-MM-dd or whatever based on current user's locale. And we are interested specifically in template, having just formatted date/time doesn't work – need a general template which would dictate rule how to format data for our mobile app.

Any help would be appreciated. Considering also non-apex-code solution as worst case scenario.

So far the only approach we've come up with is like this – tremendously ugly:

DateTime.newInstance(1999, 11, 10, 18, 49, 37).formatLong().replace('1999', 'YYYY').replace('11', 'MM').replace('10', 'dd').replace('18', 'HH').replace('06', 'hh').replace('6', 'hh').replace('49', 'mm').replace('37', 'ss').replaceFirst('((PM|pm|AM|am)\\\\s+)(.+)', '$1z').replaceFirst('AM|am|PM|pm', 'a');

Best Answer

In Apex Code, DateTime.format without parameters will return the current date and time, in the user's locale formatting, in the user's current time zone, and Date.format without parameters will return the current date, in the user's locale formatting, in the user's current time zone. You may want to read the documentation for more information. If you're using something like JSON, you may want to use your mobile apps' default locale formatting, which may differ from Salesforce's settings (e.g. JavaScript's "toLocaleString" methods). There's no way to get a template string in Apex Code for the user's locale.