介绍
今天就跟大家聊聊有关SimpleDateFormat方法如何在Java项目中使用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
公共类SimpleDateFormat DateFormat
SimpleDateFormat延伸是一个以国别敏感的方式格式化和分析数据的具体类。它允许格式化(日期→文本),语法分析(文本→日期)和标准化。
SimpleDateFormat允许以为日期,时间格式化选择任何用户指定的方式启动。但是,希望用DateFormat中的getTimeInstance, getDateInstance或getDateTimeInstance创建一个日期,时间格式化程序。每个类方法返回一个以缺省格式化方式初始化的日期/时间格式化程序。可以根据需要用applyPattern方法修改格式化方式。
<强> SimpleDateFormat函数的继承关系:强>
java . lang . object | + - - - - - java.text.Format | + - - - - - java.text.DateFormat | + - - - - - text。SimpleDateFormat
下面是个小例子:
进口text . *; 进口java.util.Date;/* * SimpleDateFormat函数语法: G年代标志符 y年 米月 d日 h时在上午或下午(1 ~ 12) H时在一天中(0 ~ 23) 米分 年代秒 年代毫秒 E星期 D一年中的第几天 F一月中第几个星期几 w一年中第几个星期 W一月中第几个星期 一个上午/下午标记符 k时在一天中(1 ~ 24) K时在上午或下午(0 ~ 11) z时区 */公开课FormatDateTime { 公共静态void main (String [] args) { SimpleDateFormat myFmt=new SimpleDateFormat (“yyyy年MM月dd日HH时MM分ss秒“); SimpleDateFormat myFmt1=new SimpleDateFormat (“yy/MM/dd HH: mm"); SimpleDateFormat myFmt2=new SimpleDateFormat (“yyyy-MM-dd HH: mm: ss");//等价于now.toLocaleString () SimpleDateFormat myFmt3=new SimpleDateFormat (“yyyy年MM月dd日HH时MM分ss秒E“); SimpleDateFormat myFmt4=new SimpleDateFormat ( “一年中的第D天一年中第w个星期一月中第w个星期在一天中k时z时区“); 日期现在=新的日期(); System.out.println (myFmt.format(现在)); System.out.println (myFmt1.format(现在)); System.out.println (myFmt2.format(现在)); System.out.println (myFmt3.format(现在)); System.out.println (myFmt4.format(现在)); System.out.println (now.toGMTString ()); System.out.println (now.toLocaleString ()); System.out.println (now.toString ()); } }
效果:
2004年12月16日17时24分27秒 04/12/16十七24 2004-12-16 17:24:27 2004年12月16日17时24分27秒星期四
一年中的第351天一年中第51个星期一月中第3个星期在一天中17时中科时区
2004-12-16格林尼治时间2004年12月16日09:24:27 17:24:27
清华2004年12月16日17:24:27 CST
下面是个JavaBean:
公共类FormatDateTime { 公共静态字符串toLongDateString(日期dt) { SimpleDateFormat myFmt=new SimpleDateFormat (“yyyy年MM月dd日HH时MM分ss秒E“); 返回myFmt.format (dt); } 公共静态字符串toShortDateString(日期dt) { SimpleDateFormat myFmt=new SimpleDateFormat (“yy年MM月dd日分“HH时毫米); 返回myFmt.format (dt); } 公共静态字符串toLongTimeString(日期dt) { SimpleDateFormat myFmt=new SimpleDateFormat (“HH mm ss SSSS"); 返回myFmt.format (dt); } 公共静态字符串toShortTimeString(日期dt) { SimpleDateFormat myFmt=new SimpleDateFormat (“yy/MM/dd HH: mm"); 返回myFmt.format (dt); } 公共静态void main (String [] args) { 日期现在=新的日期(); System.out.println (FormatDateTime.toLongDateString(现在)); System.out.println (FormatDateTime.toShortDateString(现在)); System.out.println (FormatDateTime.toLongTimeString(现在)); System.out.println (FormatDateTime.toShortTimeString(现在)); } }
调用的主要测试结果:
2004年12月16日17时38分26秒星期四 04年12月16日17时38分 17 0965年38 26日 04/12/16 17:38
看完上述内容,你们对SimpleDateFormat方法如何在Java项目中使用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。