aspose file tools*
The moose likes Beginning Java and the fly likes Getting date format from Locale Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting date format from Locale" Watch "Getting date format from Locale" New topic
Author

Getting date format from Locale

Onkar Joshi
Ranch Hand

Joined: Mar 01, 2007
Posts: 116
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?


SCJP 5 - 95% | SCWCD 1.4 - 88% | SCBCD 5 - 93%
Onkar Joshi's blog | LinkedIn profile
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
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


SCJP 5.0, SCJD in progress
Onkar Joshi
Ranch Hand

Joined: Mar 01, 2007
Posts: 116
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

Joined: Mar 01, 2007
Posts: 116
Oh, and yes, you were right. I did want to retrieve the pattern.
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
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
 
jQuery in Action, 2nd edition
 
subject: Getting date format from Locale