?
1
|
|
SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类。 它允许格式化 (date -> text)、语法分析 (text -> date)和标准化。
SimpleDateFormat 允许以为日期-时间格式化选择任何用户指定的方式启动。 但是,希望用 DateFormat 中的 getTimeInstance、 getDateInstance 或 getDateTimeInstance 创建一个日期-时间格式化程序。 每个类方法返回一个以缺省格式化方式初始化的日期/时间格式化程序。 可以根据需要用 applyPattern 方法修改格式化方式。
SimpleDateFormat函数的继承关系:
?
1
2
3
4
5
6
7
|
Java.lang.Object
|
+----java.text.Format
|
+----java.text.DateFormat
|
+----java.text.SimpleDateFormat
|
下面是个小例子:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
import java.text.*;
import java.util.Date;
/**
SimpleDateFormat函数语法:
G 年代标志符
y 年
M 月
d 日
h 时 在上午或下午 (1~12)
H 时 在一天中 (0~23)
m 分
s 秒
S 毫秒
E 星期
D 一年中的第几天
F 一月中第几个星期几
w 一年中第几个星期
W 一月中第几个星期
a 上午 / 下午 标记符
k 时 在一天中 (1~24)
K 时 在上午或下午 (0~11)
z 时区
*/
public class FormatDateTime {
public static 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时区" );
Date now= new Date();
System.out.println(myFmt.format(now));
System.out.println(myFmt1.format(now));
System.out.println(myFmt2.format(now));
System.out.println(myFmt3.format(now));
System.out.println(myFmt4.format(now));
System.out.println(now.toGMTString());
System.out.println(now.toLocaleString());
System.out.println(now.toString());
}
}
|
效果:
?
1
2
3
4
|
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个星期 一月中第3个星期 在一天中17时 CST时区
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004
下面是个JavaBean:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class FormatDateTime {
public static String toLongDateString(Date dt){
SimpleDateFormat myFmt= new SimpleDateFormat( "yyyy年MM月dd日 HH时mm分ss秒 E " );
return myFmt.format(dt);
}
public static String toShortDateString(Date dt){
SimpleDateFormat myFmt= new SimpleDateFormat( "yy年MM月dd日 HH时mm分" );
return myFmt.format(dt);
}
public static String toLongTimeString(Date dt){
SimpleDateFormat myFmt= new SimpleDateFormat( "HH mm ss SSSS" );
return myFmt.format(dt);
}
public static String toShortTimeString(Date dt){
SimpleDateFormat myFmt= new SimpleDateFormat( "yy/MM/dd HH:mm" );
return myFmt.format(dt);
}
public static void main(String[] args) {
Date now= new Date();
System.out.println(FormatDateTime.toLongDateString(now));
System.out.println(FormatDateTime.toShortDateString(now));
System.out.println(FormatDateTime.toLongTimeString(now));
System.out.println(FormatDateTime.toShortTimeString(now));
}
}
|
调用的main 测试结果:
?
1
2
3
4
|
2004 年 12 月 16 日 17 时 38 分 26 秒 星期四
04 年 12 月 16 日 17 时 38 分
17 38 26 0965
04 / 12 / 16 17 : 38
|
以上所述是小编给大家介绍的Java中SimpleDateFormat用法详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
原文链接:http://blog.csdn.net/gubaohua/article/details/575488
相关文章
猜你喜欢
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 104
-
2025-06-04 99
-
2025-05-25 89
-
2025-05-27 34
-
2025-05-29 70
热门评论