2010-02-01 36 views
1

如何在Flex/ActionScript 3中檢索特定於語言環境的日期格式字符串?我無法找到一種方法來返回基於當前語言環境的實際格式字符串(指定日期格式)。我問這個問題是因爲我希望找到一種方法來根據當前的區域設置的短日期格式將字符串轉換爲日期。 Java允許一個打電話:如何在Flex/ActionScript 3中檢索特定於語言環境的日期格式字符串?

DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, locale) 

檢索根據基於該區域設置的短格式格式日期格式的一個實例。

Adob​​e Flex(ActionScript 3)3中是否存在類似的功能?如果沒有,是否有一個可靠的第三方庫存在這個?

回答

0

是的,我不得不說,Java是更好的日期 - 你設置語言環境,並自動輸出正確的日期!我似乎無法在Flex中找到這樣的設施。

爲了正確輸出每個地區的日期,我認爲您必須完成以下內容:http://livedocs.adobe.com/flex/3/html/help.html?content=l10n_1.html。也許你應該這樣做,然後在同一個班級中,將這些字符串從語言環境文件中提取到應用程序的其餘部分,然後您就可以對它們進行操作。

否則這傢伙的圖書館會幫助你?我不確定。 http://flexoop.com/2008/12/flex-date-utils-date-and-time-format-part-ii/

2

我剛剛發現this包做的工作。這裏描述類DateTimeFormatter:

var formatter:DateTimeFormatter = new DateTimeFormatter(LocaleID.DEFAULT, DateTimeStyle.LONG, DateTimeStyle.SHORT); 
var result:String = formatter.format(date); 

只是很酷。

1

延長古棧的回答是:

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; 
} 
相關問題