Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

internationalization - date format

 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

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?
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Call the SimpleDateFormat constructor and specify the format yourself. This should do it:

Note the arguments in the format are case sensitive. The SimpleDateFormat API docs has a big list all all possible characters you can use in the format.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


or


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.

Chris
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys!

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???
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :


4/29/05
M/d/yy



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
 
reply
    Bookmark Topic Watch Topic
  • New Topic