
正文
Java Date SimpleDateFormat
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
public static void main(String[] args) {
long millis = 1492741275301L;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millis);
Date time = calendar.getTime();
System.out.println(time);
System.out.println(DateFormatUtils.format(time, "yyyy-MM-dd HH:mm:ss"));
System.out.println(DateFormatUtils.format(millis, "yyyy-MM-dd HH:mm:ss"));
}
24小时制时间显示:
public class Datetime {
public static void main(String args[]){
java.util.Date current=new java.util.Date();
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String c=sdf.format(current);
System.out.println(c);
}
}
12小时制时间显示:
public class Datetime {
public static void main(String args[]){
java.util.Date current=new java.util.Date();
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String c=sdf.format(current);
System.out.println(c);
}
}
两者区别:yyyy-MM-dd HH :mm:ss ; yyyy-MM-dd hh :mm:ss
public class Datetime {
public static void main(String args[]){
java.util.Date current=new java.util.Date();
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss S");
String c=sdf.format(current);
System.out.println(c);
}
}
yyyy-MM-dd hh:mm:ss S 年-月-日 小时(12小时制):分钟:秒 毫秒
| 字母 | 日期或时间元素 | 表示 | 示例 |
|---|---|---|---|
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
|
org.apache.commons.lang.time.DateFormatUtils
常用日期格式的格式化操作:
例1: 以 yyyy-MM-dd 格式化:
DateFormatUtils.ISO_DATE_FORMAT.format(new Date()): 2009-03-20
例2: 以 yyyy-MM-ddZZ 格式化:
DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.format(new Date()): 2009-03-20+08:00
例3: 以 yyyy-MM-dd'T'HH:mm:ss 格式化:
DateFormatUtils.ISO_DATETIME_FORMAT.format(new Date()): 2009-03-20T22:07:01
例4: 以 yyyy-MM-dd'T'HH:mm:ssZZ 格式化:
DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(new Date()): 2009-03-20T22:07:01+08:00
例5: 以 'T'HH:mm:ss 格式化:
DateFormatUtils.ISO_TIME_FORMAT.format(new Date()): T22:07:01
例6: 以 HH:mm:ss 格式化:
DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(new Date()): 22:07:01
例7: 以 HH:mm:ssZZ 格式化:
DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.format(new Date()): 22:07:01+08:00
例8: 以 'T'HH:mm:ssZZ 格式化:
DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.format(new Date()): T22:07:01+08:00
自定义日期格式的格式化操作:
例1: 以 yyyy-MM-dd HH:mm:ss 格式化
Date对象
:
DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"): 2009-03-20 22:24:30
例2: 以 yyyy-MM-dd HH:mm:ss 格式化
Calendar对象
:
DateFormatUtils.format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss"): 2009-03-20 22:24:30
例3: 以 yyyy-MM-dd HH:mm:ss 格式化
TimeInMillis
:
DateFormatUtils.format(Calendar.getInstance().getTimeInMillis(), "yyyy-MM-dd HH:mm:ss"): 2009-03-20 22:24:30
日期中带的 T 是什么意思
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }






