Help coderanch get a
new server
by contributing to the fundraiser
  • 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Getting date format from Locale

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

What I have in my DB are country and language in String form for each user.

What I need is whether a "DD/MM/YYYY" string will be better to display date to that user or would it be a "MM/DD/YYYY" string.

A Locale can take country and language as parameters for a constructor. But I looked around at the various formatting classes that use this but didn't find an API that would allow me to make the decision required above.

Is there any way to do this that I missed?
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Onkar,

I don't know whether I got your question right. If you want to format a date for a given locale you would perform the following steps:

But this is not your question, right? You want to retrieve the pattern that is used for formatting?
I think, there is a kind of dirty way to retrive the actual pattern. If you have a look at the runtime type of the DateFormat from above, this will usually be a SimpleDateFormat. And the SimpleDateFormat has a method toPattern(). Therefore, you can try to retrieve the pattern as follows:

In case your DateFormat instance is not a SimpleDateFormat you could create a fallback solution by defining some kind of dummy date, use the steps of the first code block to retrive a formatted instance and use this one to get the numbering order and separating characters.

Hope this helps,
Thomas
 
Oscar Johansson
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it helps!

My thoughts did wander in this direction but was trying to avoid this route since it is, like you mentioned, "kind of dirty". The code made things easier for me.

Thanks!
 
Oscar Johansson
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, and yes, you were right. I did want to retrieve the pattern.
 
Thomas Thevis
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My thoughts did wander in this direction but was trying to avoid this route since it is, like you mentioned, "kind of dirty". The code made things easier for me.

Thanks!


You're welcome
If you find a solution which is not as dirty, I'd be very interested in knowing about.

Regards,
Thomas
 
reply
    Bookmark Topic Watch Topic
  • New Topic