java-使用SImpleDateFormat格式化时间输出

之前有篇博客是使用NSDateFormatter来对时间进行格式化输出,但使用起来有点繁琐,今天介绍下最近刚刚使用的SimpleDateFormat.

1 public class SimpleDateFormat extends DateFormat

日期和时间模式

日期和时间格式由日期和时间模式 字符串指定。在日期和时间模式字符串中,未加引号的字母 'A' 到 'Z' 和 'a' 到 'z' 被解释为模式字母,用来表示日期或时间字符串元素。文本可以使用单引号 (') 引起来,以免进行解释。"''" 表示单引号。所有其他字符均不解释;只是在格式化时将它们简单复制到输出字符串,或者在解析时与输入字符串进行匹配。

定义了以下模式字母(所有其他字符 'A' 到 'Z' 和 'a' 到 'z' 都被保留):

字母日期或时间元素表示示例
G Era 标志符 Text AD
y Year 199696
M 年中的月份 Month JulyJul07
w 年中的周数 Number 27
W 月份中的周数 Number 2
D 年中的天数 Number 189
d 月份中的天数 Number 10
F 月份中的星期 Number 2
E 星期中的天数 Text TuesdayTue
a Am/pm 标记 Text PM
H 一天中的小时数(0-23) Number 0
k 一天中的小时数(1-24) Number 24
K am/pm 中的小时数(0-11) Number 0
h am/pm 中的小时数(1-12) Number 12
m 小时中的分钟数 Number 30
s 分钟中的秒数 Number 55
S 毫秒数 Number 978
z 时区 General time zone Pacific Standard TimePSTGMT-08:00
Z 时区 RFC 822 time zone -0800

示例

以下示例显示了如何在美国语言环境中解释日期和时间模式。给定的日期和时间为美国太平洋时区的本地时间 2001-07-04 12:08:56。
日期和时间模式结果
"yyyy.MM.dd G 'at' HH:mm:ss z" 2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy" Wed, Jul 4, '01
"h:mm a" 12:08 PM
"hh 'o''clock' a, zzzz" 12 o'clock PM, Pacific Daylight Time
"K:mm a, z" 0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa" 02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z" Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ" 010704120856-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSZ" 2001-07-04T12:08:56.235-0700

常用构造方法 :

 SimpleDateFormat sFormat = new SimpleDateFormat(String pattern);

或者

SimpleDateFormat sFormat = new SimpleDateFormat();

sFormat.applyPattern(String pattern); 

或者

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, Locale.getDefault());

//具体可取DateFormat.SHORT,DEFAULT,LONG,FULL等

示例代码

 1 public void testCalendar(){  
 2         Calendar c1 = Calendar.getInstance();  
 3         c1.setTime(new Date());  
 4           
 5         //当Calendar中设置的时间超过每项的最大值时,会以减去最大值后的值设置时间,例如月份设置13,最后会变成13-11=02  
 6         Calendar c2 = Calendar.getInstance();  
 7         c2.set(1920, 13, 24, 22, 32, 22);  
 8         //使用pattern  
 9         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");  
10         SimpleDateFormat format2 = new SimpleDateFormat("yy-MM-dd H:m:s");  
11         SimpleDateFormat format3 = new SimpleDateFormat("y-M-d H:m:s");  
12         //使用约定格式   
13   
14 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, Locale.getDefault());  
15          //获取Calendar中各个属性字段的方法  
16         log.info("The year now time is " + c1.get(c1.YEAR));  
17         log.info("The month now time is " + c1.get(c1.MONTH));  
18         log.info("The day_of_month now time is " + c1.get(c1.DAY_OF_MONTH));  
19         log.info("The day_of_week now time is " + c1.get(c1.DAY_OF_WEEK));  
20         log.info("今天是在这个月的第几个星期: " + c1.get(c1.DAY_OF_WEEK_IN_MONTH));  
21         log.info("The day_of_year now time is " + c1.get(c1.DAY_OF_YEAR));  
22         //不同模式对应的格式略有不同,有时间可以测试多一点模式  
23         log.info("yyyy-MM-dd H:m:s-->" + format.format(c1.getTime()));  
24         log.info("yy-MM-dd H:m:s-->" + format2.format(c1.getTime()));  
25         log.info("y-M-d H:m:s-->" + format3.format(c1.getTime()));  
26   log.info("DateFormat.FULL-->" + dateFormat.fomat(c1.getTime()));  
27         log.info(format.format(c2.getTime()));  
28 }  
原文地址:https://www.cnblogs.com/feiling/p/4805253.html