如何在 Flex / ActionScript 3 中检索特定于区域设置的日期格式字符串?

发布于 2024-08-19 17:37:26 字数 364 浏览 3 评论 0原文

如何在 Flex / ActionScript 3 中检索特定于区域设置的日期格式字符串?我无法找到一种方法来根据当前区域设置返回实际格式字符串(指定日期格式)。我问这个问题是因为我希望找到一种方法,根据区域设置的当前短日期格式将字符串转换为日期。 Java 允许调用:

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

检索 DateFormat 的实例,该实例根据基于区域设置的 SHORT 格式进行格式化。

Adobe Flex (ActionScript 3) 3 中是否存在类似的功能?如果没有,是否有可靠的第三方库可用?

How do I retrieve the locale-specific date format string in Flex / ActionScript 3? 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南街九尾狐 2024-08-26 17:37:26

我刚刚发现这个包可以完成这项工作。这里描述了 DateTimeFormatter 类:

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

太酷了。

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.

幸福还没到 2024-08-26 17:37:26

扩展Gojan的答案:

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;
}

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;
}
丢了幸福的猪 2024-08-26 17:37:26

是的,我不得不说 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/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文