publicclasssimpledateformatextendsdateformatsimpledateformat是一个针对每个国家以高度敏感的方式格式化和分析数据的具体类。 可以进行格式(date - text )、语法分析(text - date )和标准化。
使用SimpleDateFormat,可以选择和启动用户为日期和时间格式指定的方法。 但是,您希望使用日期格式的getTimeInstance、getDateInstance或getDateTimeInstance创建日期/时间格式器。 每个类方法返回以默认格式初始化的日期/时间格式器。 如果需要,可以使用applyPattern方法更改格式设置方法。
简单日期格式函数的继承关系:
java.lang.Object
|
----java.text.Format
|
----java.text.DateFormat
|
----java.text.SimpleDateFormat
下面是一个小例子:
导入Java.text.*;
import java.util.Date;
//*
简单日期格式函数语法:
g年代标识
y年
m月
第二天
h时为上午或下午(1~12 )。
h时一整天(0~23 ) ) ) ) ) )。
米分钟
秒
s毫秒
e周
d1年的第几天
是f1月的第几天
w1年的第几周
w1月的第几周
上午/下午标记
k时一整天((1~24 ) ) ) ) ) ) ) ) ) ) )。
k时为上午或下午(0~11 )。
z时区
*/
公共类格式数据时间{
publicstaticvoidmain (字符串[ ] args ) {
simpledateformatmyfmt=newsimpledateformat (yyyy年MM月dd日HH时MM分ss秒);
simpledateformatmyfmt1=newsimpledateformat (YY/mm/ddhh : mm );
simpledateformatmyfmt2=newsimpledateformat (yyyy-mm-ddhh 3360 mm : ss ); 与now.toLocaleString (等价
simpledateformatmyfmt3=newsimpledateformat (yyyy年MM月dd日HH时MM分ss秒e );
simpledateformatmyfmt4=newsimpledateformat (
' 1年第d天1年第w周1月第w周是1日第k时z时区';
日期now=new date (;
system.out.println (myfmt.format ) now );
系统. out.println (myfm t1.format (now ) );
系统. out.println (myfm T2.format (now ) );
系统. out.println (myfm T3.format (now ) );
system.out.println (myfmt4. format (now ) );
system.out.println (now.togmtstring ();
system.out.println (now.tolocalestring ();
system.out.println (now.tostring );
}
}
效果:
2004年12月16日17时24分27秒
04/12/16 17:24
2004-12-16 17:24:27
2004年12月16日17时24分27秒星期四
一年的第351天第一年的第51周一月的第三周位于一天的17点CST时区
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004
下面是JavaBean:
公共类格式数据时间{
publicstaticstringtolongdatestring {
simpledateformatmyfmt=newsimpledateformat (yyyy年MM月dd日HH时MM分ss秒e );
returnmyfmt.format(dt;
}
publicstaticstringtoshortdatestring {
simpledateformatmyfmt=newsimpledateformat (YY年MM月dd日HH时MM分);
returnmyfmt.format(dt;
}
publicstaticstringtolongtimestring {
simpledateformatmyfmt=newsimpledateformat (hhmmss SSSS );
returnmyfmt.format(dt;
}
publicstaticstringtoshorttimestring {
simpledateformatmyfmt=newsimpledateformat (YY/mm/ddhh : mm );
returnmyfmt.format(dt;
}
publicstaticvoidmain (字符串[ ] args ) {
日期now=new date (;
system.out.println (formatdatetime.to longdate string (now ) );
system.out.println (formatdatetime.toshortdatestring (now ) );
system.out.println (formatdatetime.tolongtimestring (now ) );
system.out.println (formatdatetime.toshorttimestring (now ) );
}
}
调用的main测试结果:
2004年12月16日17时38分26秒星期四
04年12月16日17时38分
17 38 26 0965
04/12/16 17:38