开发者

Is there an easy way to automatically format dates according to locale in Flex 4?

I understand that one can use local-specific ResourceBundles in combination with DateFormatters to format dates according to locale. However this is a manual process - is there an automatic way to do this or to set a default for the application?

In Java for example, all your dates will automatically appear in dd/mm/yy or m开发者_如何学运维m/dd/yy format simply by setting the locale. In Flex, a default date output will always be in US format unless manually formatted otherwise. I'm looking for a way to get closer to the Java functionality.


I did this recently using flah.globalization classes: see its very informative about getting locale etc.. http://www.adobe.com/devnet/flashplayer/articles/flash_globalization_package.html here's my code:

remember to call init(); on creation complete !

<fx:Script>
    <![CDATA[

        import flash.globalization.DateTimeFormatter;
        import flash.globalization.DateTimeStyle;
        import flash.globalization.StringTools;

        import mx.collections.ArrayCollection;

        import spark.events.IndexChangeEvent;


        [Bindable]
        private var listColl:ArrayCollection;

        private var localeList:Array = new Array("en-US", "fr-FR", "es-ES","ja-JP", "hi-IN","ru-RU");


        private var country:String;


        private function init():void{

            // set the dp for drop down;
            listColl = new ArrayCollection(localeList);
            country = localeList[0];
        }


    private function doDateLabel(item:Date):String {

    trace("input = " + item);

    if(item != null) {


        var locale:String = country;
        if(locale != null){

        var dtf:DateTimeFormatter = new DateTimeFormatter(locale);

            dtf.setDateTimeStyles(DateTimeStyle.SHORT, DateTimeStyle.NONE);
        /*
            DateTimeSyle.MEDIUM
        DateTimeSyle.LONG   

            */


            var shortDate:String = dtf.format(item);
            trace(shortDate + " (" + dtf.getDateTimePattern() + ")");

            }    
        }
    return shortDate;
    }

        protected function dropDownList_changeHandler(evt:IndexChangeEvent):void {
            country = countryList.selectedItem;
        }




    ]]>
</fx:Script>


<s:HGroup width="100%" height="100%" gap="20" top="50"  left="50">

    Hope that's what you were after 
    <mx:DateField id="begin" width="200"
                  showToday="true"
                  labelFunction="doDateLabel" 
                  parseFunction="null"/>

    <s:DropDownList id="countryList"
                    requireSelection="true" prompt="Please select an Country" 
                    horizontalCenter="0" top="20" dataProvider="{listColl}" 
                    change="dropDownList_changeHandler(event);">

    </s:DropDownList>
</s:HGroup>


Look at toLocaleString and toLocaleTimeString

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Date.html#toLocaleString()

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Date.html#toLocaleTimeString()


It is a feature of Flash Player 10.1 placed in flash.globalization package

The flash.globalization package in Flash Player: Cultural diversity without complexity

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜