I am writing an application where i need to display the date in a locale-specific format, but something like dd/mm/yyyy or mm/dd/yyyy and not with month names in between etc.
I have checked out SimpleDateFormat etc and if i use DateFormat.getDateInstance(DateFormat.SMALL), i do get the info but i get the year as yy and not yyyy. if i use DateFormat.MEDIUM or DateFormat.DEFAULT, i get a date like Apr 28, 2005 or 28 Apr 2005.
Is there any way to get the date as dd/mm/yyyy or mm/dd/yyyy?
If you want to use DateFormat's locale specific formats, then I would stick with the defaults. Trying to convert from 2 digit to 4 digit years could potentially be tricky. However, if you are certain that the locale specific short date format will always return either date, month, year or month, date, year then you can put in a date, say January 2 of any year and see which number shows up first, then conditionally use
depending on the order of the integers in the test case. If a locale decides to throw you a curve and use year first then you may be in trouble.
But here is the issue... I will not know whether that specific locale would use dd/MM/yy or MM/dd/yy. For this reason, i am using the SimpleDateFormat.
If i knew the format beforehand, I could always store it in a properties file etc and read from it based on the locale or something like that. But the issue is that, i will not know the date format used by that specific locale. Even if i do a DateFormat.getLocalizedPattern(), it returns something like 'MMM d,yyyy' which is not the desired format and does not say anything about the MM/dd/yy.
If i could somehow figure out the MM/dd/yy ordering from the DateFormat or anywhere else, i could construct the date myself or just modify the date returned by DateFormat.SHORT to make it yyyy. But how do i find out the order???
Sorry Chris...had not read your post correctly. Yes, that should work. if i specify a unique value for MM/dd/yy, like 04/28/05 for 28th April 2005, i should be able to break it up and get the components.
Here is one more way with one more issue:
the output is :
which is exactly what i want. which mean the DateFormat.getLocalizedPattern() depends on what kind of DateFormat(SHORT, MEDIUM etc) you specify.
But the issue with that is: suppose i change the locale to 'France'. In that case the language also changes to french and then instead of M/d/yy, i see the equivalent in French jj/nn/aa which means i cant search for 'M' or 'd' or 'y' in the String returned by DateFormat.getLocalizedPattern()
Which means that Chris' way of doing it is still the best way out