How do I retrieve the locale-specific date format string in Flex / ActionScript 开发者_如何学Go3? I am unable to find a method to return the actual format string (that which specifies the date format) based on the current locale. I am asking this question because I was hoping to find a way to convert a String to a Date based on the current SHORT date format for the locale. Java allows one to call:
DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, locale)
to retrieve an instance of DateFormat that formats according to the SHORT format based on the locale.
Does similar functionality exist in Adobe Flex (ActionScript 3) 3? If not, is there a reliable third party library that exists for this?
I'm just found this package that do the job. Here describe the class DateTimeFormatter:
var formatter:DateTimeFormatter = new DateTimeFormatter(LocaleID.DEFAULT, DateTimeStyle.LONG, DateTimeStyle.SHORT);
var result:String = formatter.format(date);
Just cool.
Extending Gojan's answer:
private function cc(event:FlexEvent):void {
var formatter:DateTimeFormatter = new DateTimeFormatter(LocaleID.DEFAULT, DateTimeStyle.SHORT, DateTimeStyle.NONE);
//now if publishDate is a mx:DateField, the formatString of spark and mx components are slightly different.
//So, we replace all d with D and y with Y
publishDate.formatString=replaceAll(formatter.getDateTimePattern(), ["d", "y"], ["D", "Y"]);
}
private function replaceAll(text:String, searchArray:Array, replArray:Array):String {
for (var i:int=0; i<searchArray.length; i++) {
var s:String=searchArray[i];
var d:String=replArray[i];
text=text.split(s).join(d);
}
return text;
}
Yeah I have to say Java is better with dates - you set the locale and automatically your dates are outputted correctly! I can't seem to find such a facility in Flex.
In order to output your dates correctly for each locale I think you have to do what is written in this article: http://livedocs.adobe.com/flex/3/html/help.html?content=l10n_1.html. Maybe you should do this, and in the same class just make these strings which you've pulled from the locale file available to the rest of your app, then you'll be able to operate on them.
Otherwise perhaps this guy's library will help you? I'm not sure. http://flexoop.com/2008/12/flex-date-utils-date-and-time-format-part-ii/
精彩评论