DateFormat是一個格式化和解析日期或時間的抽象類. 它的子類, 如SimpleDateFormat, 允許進行格式化(即date -> text), 分析(text -> date), 規範化. 該日期以Date對象表示, 或自1970年1月1日, 格林尼治標準時間(GMT)00:00:00起以毫秒表示.
SimpleDateFormat是一個用於本土(locale-sensitive)格式化和解析日期方式的具體類.
Example
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy hh:mm:ss a");
String now = formatter.format(new Date());
時間格式語法
要指定的時間格式, 使用時間模式字符串. 在這個模式中, 所有的ASCII字母被保留為模式字母, 其中定義如下:
Symbol | Meaning | Presentation | Example |
---|---|---|---|
G | era designator | (Text) | AD |
y | year | (Number) | 2013 |
M | month in year | (Text & Number) | July & 07 |
d | day in month | (Number) | 13 |
h | hour in am/pm (1˜12) | (Number) | 12 |
H | hour in day (0˜23) | (Number) | 0 |
m | minute in hour | (Number) | 30 |
s | second in minute | (Number) | 55 |
S | fractional second | (Number) | 978 |
E | day of week | (Text) | Tuesday |
D | day in year | (Number) | 289 |
F | day of week in month | (Number) | 2 (2nd Wed in July) |
w | week in year | (Number) | 28 |
W | week in month | (Number) | 2 |
a | am/pm marker | (Text) | PM |
k | hour in day (1˜24) | (Number) | 24 |
K | hour in am/pm (0˜11) | (Number) | 0 |
z | time zone | (Text) | Pacific Standard Time |
Z | time zone (RFC 822) | (Number) | -0800 |
v | time zone (generic) | (Text) | Pacific Time |
V | time zone (location) | (Text) | United States (Los Angeles) |
' | escape for text | ((Delimiter) | 'Date=' |
'' | single quote | (Literal) | 'o''clock' |
沒有留言:
張貼留言