Flex DateFormatter格式化时间异常处理

来源:互联网 发布:java中线程同步的方法 编辑:程序博客网 时间:2024/05/17 05:50

       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

                  var sec:int = int(date.getSeconds());

                  result += setValue(sec, key);

                  return result;

              }

                 

              case "Q":

              {

                  // milliseconds in second

                  var ms:int = int(date.getMilliseconds());

                  result += setValue(ms, key);

                  return result;

              }

           }

          

           return result;

       }

      

       /**

        * 重写format,解决将时间0:12:12格式化24:12:12问题

        */

       override public function format(value:Object):String

       {      

           // Reset any previous errors.

           if (error)

              error = null;

          

           // If value is null, or empty String just return ""

           // but treat it as an error for consistency.

           // Users will ignore it anyway.

           if (!value || (value is String && value == ""))       

           {

              error = defaultInvalidValueError;

              return "";

           }

          

           // -- value --

          

           if (value is String)

           {

              value = DateFormatter.parseDateString(String(value));

              if (!value)

              {

                  error = defaultInvalidValueError;

                  return "";

              }

           }

           else if (!(value is Date))

           {

              error = defaultInvalidValueError;

              return "";

           }

          

           // -- format --

          

           var letter:String;

           var nTokens:int = 0;

           var tokens:String = "";

          

           var n:int = formatString.length;

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

           {

              letter = formatString.charAt(i);

              if (VALID_PATTERN_CHARS.indexOf(letter) != -1 && letter != ",")

              {

                  nTokens++;

                  if (tokens.indexOf(letter) == -1)

                  {

                     tokens += letter;

                  }

                  else

                  {

                     if (letter != formatString.charAt(Math.max(i - 1, 0)))

                     {

                         error = defaultInvalidFormatError;

                         return "";

                     }

                  }

              }

           }

          

           if (nTokens < 1)

           {

              error = defaultInvalidFormatError;

              return "";

           }

          

           var dataFormatter:StringFormatterEX = new StringFormatterEX(

              formatString, VALID_PATTERN_CHARS,

              extractTokenDate);

          

           return dataFormatter.formatValue(value);

        }

    }

}