标签:des style blog color java 使用 os io
项目过程中,经常遇到需要字符串格式的日期和Date类型的日期之间的相互转换。使用SimpleDateFormat类,可以方便完成想要的转换。
SimpleDateFormat能够实现本地化的时间格式化及转换。从选定一个自定义的模式(pattren)开始,模式由已经定义好的 ‘A‘
to ‘Z‘
及 ‘a‘
to ‘z‘字母组成,也可以在模式中引入文本,但要使用’(单括号)括住。下图就是已经定义好的模式字母表:
Letter | Date or Time Component | Presentation | Examples |
---|---|---|---|
G |
Era designator | Text | AD |
y |
Year | Year | 1996 ; 96 |
Y |
Week year | Year | 2009 ; 09 |
M |
Month in year | Month | July ; Jul ; 07 |
w |
Week in year | Number | 27 |
W |
Week in month | Number | 2 |
D |
Day in year | Number | 189 |
d |
Day in month | Number | 10 |
F |
Day of week in month | Number | 2 |
E |
Day name in week | Text | Tuesday ; Tue |
u |
Day number of week (1 = Monday, ..., 7 = Sunday) | Number | 1 |
a |
Am/pm marker | Text | PM |
H |
Hour in day (0-23) | Number | 0 |
k |
Hour in day (1-24) | Number | 24 |
K |
Hour in am/pm (0-11) | Number | 0 |
h |
Hour in am/pm (1-12) | Number | 12 |
m |
Minute in hour | Number | 30 |
s |
Second in minute | Number | 55 |
S |
Millisecond | Number | 978 |
z |
Time zone | General time zone | Pacific Standard Time ; PST ; GMT-08:00 |
Z |
Time zone | RFC 822 time zone | -0800 |
X |
Time zone | ISO 8601 time zone | -08 ; -0800 ; -08:00 |
在一个模式中,模式字母可以重复,不同数量的字母代表不同的意思:
下面看看几个应用:
Long now = System.currentTimeMillis(); SimpleDateFormat sdf2 = new SimpleDateFormat("MM ddddddddddd,yyyy EEEE"); System.out.println(sdf2.format(now));
输出:
07 00000000031,2014 星期四 //日数的格式化模式过长,高位用0填充,星期为中文表示,默认情况下,
Long now = System.currentTimeMillis();
SimpleDateFormat sdf2 = new SimpleDateFormat("MMMM dd,yyyy EEEE",Locale.US);
System.out.println(sdf2.format(now));
输出:
July 31,2014 Thursday //指定地区为US,则以US格式输出,MMMM表示月份为完整文本类型
转换:
String dateString = "2000,October 22 10:10:11";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy,MMMM dddd HH:mm:ss",Locale.ENGLISH);
Date d = sdf.parse(dateString);
System.out.println(d.toString());
输出:
Sun Oct 22 10:10:11 CST 2000
String dateString = "2000,October 22 10:10:11";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy,MM dddd HH:mm:ss",Locale.ENGLISH);
Date d = sdf.parse(dateString);
System.out.println(d.toString());
输出:不能转换,因为被转换的字符串的月份为完整文本类型,模式也要为"MMMM"
String dateString = "2000,10 22 10:10:11";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy,MM dddd HH:mm:ss",Locale.ENGLISH);
Date d = sdf.parse(dateString);
System.out.println(d.toString());
输出:
Sun Oct 22 10:10:11 CST 2000 //月份为数字,模式对应为少于3为的”MM“,
String dateString = "2000,10 22 10:10";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy,MM dddd HH:mm:ss",Locale.ENGLISH);
Date d = sdf.parse(dateString);
System.out.println(d.toString());
输出:不能转换,因为被转换的字符串没有秒的数字,但模式包含了秒,无法确定秒为多少。
String dateString = "2000,10 22 10:10:11";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy,MM dddd HH:mm",Locale.ENGLISH);
Date d = sdf.parse(dateString);
System.out.println(d.toString());
输出:
Sun Oct 22 10:10:00 CST 2000 //模式没有指定秒,但是转换字符串有,但不影响转换,只是结果的秒为0;
String dateString = "2000,十月 22 10:10:11";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy,MMM dddd HH:mm",Locale.PRC);
Date d = sdf.parse(dateString);
System.out.println(d.toString());
输出:
Sun Oct 22 10:10:00 CST 2000 //注意被转换的字符串包含了中国的月份表示,SimpleDateFormat实例也要指明地区为中国,否则转换失败
String dateString = "2000,十月 22 10:10:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm",Locale.PRC);
Date d = sdf.parse(dateString);
System.out.println(d.toString());
输出:
不能转换,不转换的字符串日期格式和模式不匹配,而且月份模式字母重复次数要大于或等于3
String dateString = "2000/十月/22 10:10:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MMM/dd HH:mm",Locale.PRC);
Date d = sdf.parse(dateString);
System.out.println(d.toString());
输出:
Sun Oct 22 10:10:00 CST 2000
Java中字符串与日期之间的转换,布布扣,bubuko.com
标签:des style blog color java 使用 os io
原文地址:http://www.cnblogs.com/lauyu/p/3879709.html