This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes DateFormat getPattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "DateFormat getPattern" Watch "DateFormat getPattern" New topic
Author

DateFormat getPattern

Steve Wood
Ranch Hand

Joined: Jan 08, 2003
Posts: 137
Hi guys,

I'm looking for a way to get the date pattern from the DateFormat class. They have a number of localized styles, but I can't seem to work out how to get the actual pattern string. This seems to be supported in the SimpleDateFormat class, but not DateFormat.

So, for example, what is the DateFormat.MEDIUM pattern for Japan? I'd like to do this so my application can display the pattern it is going to apply to the user.

Any help is much appreciated.

Cheers,

Steve
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
If you look at the source for DateFormat calling DataFormat.getInstance()/getDateInstance()/getDateTimeInstance() calls SimpleDateFormat internally. So you can check to make sure its an instance of SimpleDateFormat and cast it and make the call to get the pattern.

Also all the pattern format strings are stored in the LocaleData

Steve Wood
Ranch Hand

Joined: Jan 08, 2003
Posts: 137
Thank you very much - that's superb.

Cheers,

Steve
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Just a warning:

Note that Tim's solution is not guaranteed to work on future versions of Java. The API documentation (which is the spec) does not say that DateFormat.getInstance() returns a SimpleDateFormat. In fact, in a future version of Java the implementation might be changed and your program won't work anymore if it does change.
[ December 08, 2006: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
Consider Paul's rocket mass heater.
 
subject: DateFormat getPattern
 
Similar Threads
Get the Difference between 2 dates
Findbug continues to report the same bugs
What is the DateFormat.MEDIUM format's format?
Date issue
Date format