Java的日期格式化常用方法
| 字母 | 日期或时间元素 | 表示 | 示例 |
|---|---|---|---|
G |
Era 标志符 | Text | AD |
y |
年 | Year | 1996; 96 |
M |
年中的月份 | Month | July; Jul; 07 |
w |
年中的周数 | Number | 27 |
W |
月份中的周数 | Number | 2 |
D |
年中的天数 | Number | 189 |
d |
月份中的天数 | Number | 10 |
F |
月份中的星期 | Number | 2 |
E |
星期中的天数 | Text | Tuesday; Tue |
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 Time; PST; GMT-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());
3.示例代码 (这里使用了log4j,只需将代码中的log.info改成相应的System.out.println,置于main()方法中运行即可)
public void testCalendar(){
Calendar c1 = Calendar.getInstance();
c1.setTime(new Date());
//当Calendar中设置的时间超过每项的最大值时,会以减去最大值后的值设置时间,例如月份设置13,最后会变成13-11=02
Calendar c2 = Calendar.getInstance();
c2.set(1920, 13, 24, 22, 32, 22);
//使用pattern
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");
SimpleDateFormat format2 = new SimpleDateFormat("yy-MM-dd H:m:s");
SimpleDateFormat format3 = new SimpleDateFormat("y-M-d H:m:s");
//使用约定格式
//获取Calendar中各个属性字段的方法
log.info("The year now time is " + c1.get(c1.YEAR));
log.info("The month now time is " + c1.get(c1.MONTH));
log.info("The day_of_month now time is " + c1.get(c1.DAY_OF_MONTH));
log.info("The day_of_week now time is " + c1.get(c1.DAY_OF_WEEK));
log.info("今天是在这个月的第几个星期: " + c1.get(c1.DAY_OF_WEEK_IN_MONTH));
log.info("The day_of_year now time is " + c1.get(c1.DAY_OF_YEAR));
//不同模式对应的格式略有不同,有时间可以测试多一点模式
log.info("yyyy-MM-dd H:m:s-->" + format.format(c1.getTime()));
log.info("yy-MM-dd H:m:s-->" + format2.format(c1.getTime()));
log.info("y-M-d H:m:s-->" + format3.format(c1.getTime()));
log.info("DateFormat.FULL-->" + dateFormat.fomat(c1.getTime()));
Calendar c1 = Calendar.getInstance();
c1.setTime(new Date());
//当Calendar中设置的时间超过每项的最大值时,会以减去最大值后的值设置时间,例如月份设置13,最后会变成13-11=02
Calendar c2 = Calendar.getInstance();
c2.set(1920, 13, 24, 22, 32, 22);
//使用pattern
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");
SimpleDateFormat format2 = new SimpleDateFormat("yy-MM-dd H:m:s");
SimpleDateFormat format3 = new SimpleDateFormat("y-M-d H:m:s");
//使用约定格式
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, Locale.getDefault());
log.info("The year now time is " + c1.get(c1.YEAR));
log.info("The month now time is " + c1.get(c1.MONTH));
log.info("The day_of_month now time is " + c1.get(c1.DAY_OF_MONTH));
log.info("The day_of_week now time is " + c1.get(c1.DAY_OF_WEEK));
log.info("今天是在这个月的第几个星期: " + c1.get(c1.DAY_OF_WEEK_IN_MONTH));
log.info("The day_of_year now time is " + c1.get(c1.DAY_OF_YEAR));
//不同模式对应的格式略有不同,有时间可以测试多一点模式
log.info("yyyy-MM-dd H:m:s-->" + format.format(c1.getTime()));
log.info("yy-MM-dd H:m:s-->" + format2.format(c1.getTime()));
log.info("y-M-d H:m:s-->" + format3.format(c1.getTime()));
log.info("DateFormat.FULL-->" + dateFormat.fomat(c1.getTime()));
log.info(format.format(c2.getTime()));
}


浙公网安备 33010602011771号