Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting date format from Locale

 
Onkar Joshi
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • 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?
 
Thomas Thevis
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • 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
 
Onkar Joshi
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • 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!
 
Onkar Joshi
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic