Flex DateFormatter格式化时间异常处理

Flex自带的mx:DateFormatter在格式化时间的时候有异常,我们看一简单demo。在application中定义dateformatterr如下:

<fx:Declarations>

<mx:DateFormatter id="format" formatString="YYYY-MM-DD HH:NN:SS"/>

</fx:Declarations>

假设,要格式化的日期时间为2010-12-2 0:48:46,调用format.format函数后,我们惊异地发现返回的格式化字串为:”2010-12-02 24:48:46”,看DateFormatter源码发现,只要小时为0都转换为24,用户看到”2010-12-02 24:48:46”非常不可思议。

当初因为没有好好看api,还费点周折去重新实现了一个DateFormatter,明显是简单问题复杂化(虽然知道了其格式化原理),实际上HH显示的1-24小时格式,而JJ显示的是0-23小时格式,因此只要设置formatString="YYYY-MM-DD JJ:NN:SS"即可。

解决:

新建一个DateFormatter继承mx: DateFormatter,重写format函数即可,假设新建的DateFormatterDateFormatterEX。由于format函数用到了StringFormatter类,而该类被标记为[ExcludeClass],即自己写的代码不能使用StringFormatter。没办法,新建一个StringFormatterEX类,把StringFormatter代码全部复制过来吧。

DateFormatterEX代码如下:

package com.marcus.flex.formatter

{

import mx.formatters.DateBase;

import mx.formatters.DateFormatter;

public class DateFormatterEX extends DateFormatter

{

private static const VALID_PATTERN_CHARS:String = "Y,M,D,A,E,H,J,K,L,N,S,Q";

public function DateFormatterEX()

{

}

private static function setValue(value:Object, key:int):String

{

var result:String = "";

var vLen:int = value.toString().length;

if (vLen < key)

{

var n:int = key - vLen;

for (var i:int = 0; i < n; i++)

{

result += "0"

}

}

result += value.toString();

return result;

}

private static function extractTokenDate(date:Date,

tokenInfo:Object):String

{

var result:String = "";

var key:int = int(tokenInfo.end) - int(tokenInfo.begin);

var day:int;

var hours:int;

switch (tokenInfo.token)

{

case "Y":

{

// year

var year:String = date.getFullYear().toString();

if (key < 3)

return year.substr(2);

else if (key > 4)

return setValue(Number(year), key);

else

return year;

}

case "M":

{

// month in year

var month:int = int(date.getMonth());

if (key < 3)

{

month++; // zero based

result += setValue(month, key);

return result;

}

else if (key == 3)

{

return DateBase.monthNamesShort[month];

}

else

{

return DateBase.monthNamesLong[month];

}

}

case "D":

{

// day in month

day = int(date.getDate());

result += setValue(day, key);

return result;

}

case "E":

{

// day in the week

day = int(date.getDay());

if (key < 3)

{

result += setValue(day, key);

return result;

}

else if (key == 3)

{

return DateBase.dayNamesShort[day];

}

else

{

return DateBase.dayNamesLong[day];

}

}

case "A":

{

// am/pm marker

hours = int(date.getHours());

if (hours < 12)

return DateBase.timeOfDay[0];

else

return DateBase.timeOfDay[1];

}

case "H":

{

// hour in day (1-24)

hours = int(date.getHours());

// if (hours == 0)

// hours = 24;

result += setValue(hours, key);

return result;

}

case "J":

{

// hour in day (0-23)

hours = int(date.getHours());

result += setValue(hours, key);

return result;

}

case "K":

{

// hour in am/pm (0-11)

hours = int(date.getHours());

if (hours >= 12)

hours = hours - 12;

result += setValue(hours, key);

return result;

}

case "L":

{

// hour in am/pm (1-12)

hours = int(date.getHours());

if (hours == 0)

hours = 12;

else if (hours > 12)

hours = hours - 12;

result += setValue(hours, key);

return result;

}

case "N":

{

// minutes in hour

var mins:int = int(date.getMinutes());

result += setValue(mins, key);

return result;

}

case "S":

{

// seconds in minute

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值