Ext grid中日期显示为NaN-NaN-NaN 的原因及解决方法

来源:互联网 发布:helloworld代码java 编辑:程序博客网 时间:2024/05/29 21:28
 

1.       对于时间的格式化分为两种:后台java类中对Date类的格式化;前台javascript中,对时间属性的格式化。

2.       java类中使用Java DateFormat 类 对时间格式化,具体使用方法如下:

 

import java.text.DateFormat;

import java.util.Date;

 

/**

* 格式化时间类

* DateFormat.FULL = 0

* DateFormat.DEFAULT = 2

* DateFormat.LONG = 1

* DateFormat.MEDIUM = 2

* DateFormat.SHORT = 3

* @author Michael

* @version 1.0, 2007/03/09

*/

public class TestDate {

    public static void main(String []args){

      

       /**

        * 输出当前时刻

        */

       Date d = new Date();

       String s;

      

       /** Date 类的格式: Thu Nov 03 10:29:56 CST 2011 */

       System.out.println(d);

      

      

       /** getDateInstance() */

       /** 输出格式: 2011-11-3 */

       s = DateFormat.getDateInstance().format(d);

       System.out.println(s);

      

       /** 输出格式: 2011-11-3 */

       s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);

       System.out.println(s);     

      

       /** 输出格式: 2011-11-3 */

       s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);

       System.out.println(s);

 

       /** 输出格式: 2011年11月3日 星期四 */

       s = DateFormat.getDateInstance(DateFormat.FULL).format(d);

       System.out.println(s);

      

       /** 输出格式: 11-11-3 */

       s = DateFormat.getDateInstance(DateFormat.SHORT).format(d);

       System.out.println(s);

      

       /** 输出格式: 2011-11-03 10:52:05 */

       java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

       s = format1.format(new Date());

       System.out.println(s);

      

       /** 输出格式: 2011-11-03 10:55:40 */

       System.out.println((new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new

       Date()));

      

       /** 输出格式: 2011-11-03 10:55:40

        * 只有H部分大小写,其他yyyy换成YYYY时程序出错,MM,dd,mm,ss若改变大小写,将不能获得正确日期

       */

       System.out.println((new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new

       Date()));

      

       /** 输出格式: 2011-11-03 10:55 */

       System.out.println((new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm")).format(new

       Date()));

      

       /** 输出格式: 20111103105540***/

       java.text.DateFormat format2 = new java.text.SimpleDateFormat("yyyyMMddhhmmss");

       s = format2.format(new Date());

       System.out.println(s);

    }

}

 

 

运行后输出结果如下:

2011-11-3

2011年11月3日 星期四

11-11-3

2011-11-03 11:04:30

2011-11-03 11:04:30

2011-11-03 11:04:30

2011-11-03 11:04

20111103110430

 

格式化成对应格式的时间,直接在前台输出即可,不需要在前台再次格式化。

 

3.       若数据不是从后台传递过来,直接在前台脚本代码里面写的日期,则在cm中使用

renderer:Ext.util.Format.dateRenderer('Y-m-d') 格式化时间即可。

       'Y-m-d' 可以换为任何有效格式的日期字符串,就可以输出不同格式的日期时间。

 

4.       若出现日期显示为NaN-NaN-NaN,这是由于后台传过来的日期在前台使用renderer:Ext.util.Format.dateRenderer('Y-m-d')再次格式化导致,因为java类的日期格式化与javascript的日期格式化有一些冲突。

处理办法:去掉renderer:Ext.util.Format.dateRenderer('Y-m-d'),只在后台格式化即可。

 

 

 

附:Ext的日期格式说明

格式

说明

例子

d

月里面的日期,2位,不足前面补0

01-31

D

星期里面的每天的缩写

Mon - Sun

j

月里面的日期,前面不补0

1-31

l

星期的完整写法

Sunday to Saturday

N

ISO-8601的星期表示法

1=星期一,7是星期日

S

英语习惯的月内每天的2位的前缀

st, nd, rd or th.

w

星期的数字表示

0=星期日,6等星期六

Z

在年内的第几天,从0开始

0-364,闰年是365

W

ISO-8601格式的,年内的星期数,从星期一开始

01-53

F

月份的完整文字表示

January to December

m

月份的2位数字表示,不足前面补零

01-12

M

月份的缩写文字表示

Jan to Dec

n

月份的数字表示,不补零

1-12

t

月份的最大天数

28-31

L

是否为闰年

1是闰年,0则不是

o

ISO-8601的年份表示,和Y相同,但是如果星期(W)输入前一年或者后一年,则替换为那个年

1998,2004

Y

4位的年份表示

2008

y

2位的年份表示

98,08

a

小写的上下午表示

am pm

A

大写的上下午

AM PM

g

12小时制,不补零

1-12

G

24小时制,不补零

0-23

h

12小时制,2位,不足补零

01-12

H

24小时制,2位,不足补零

00-23

i

分钟,2位,不足补零

00-59

s

秒,2位,不足补零

00-59

u

毫秒,前面补零

001 999

O

Difference to Greenwich time (GMT) in hours and minutes

+1030

P

Difference to Greenwich time (GMT) with colon between hours and minutes

-08:00

T

Timezone abbreviation of the machine running the code

EST, MDT, PD

Z

Timezone offset in seconds (negative if west of UTC, positive if east)

-43200 to 50400

c

ISO 8601 date (note: milliseconds, if present, must be specified with at least 1 digit. There is no limit to how many digits the millisecond value may contain. see http://www.w3.org/TR/NOTE-datetime for more info)

2007-04-17T15:19:21+08:002008-03-16T16:18:22Z

U

Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

1193432466 -2138434463

 

原创粉丝点击