java string與date類(lèi)型轉(zhuǎn)換
Posted on 2007-11-09 12:27 hilor 閱讀(13904) 評(píng)論(1) 編輯 收藏 所屬分類(lèi): common
SimpleDateFormat函數(shù)語(yǔ)法: G 年代標(biāo)志符 y 年 M 月 d 日 h 時(shí) 在上午或下午 (1~12) H 時(shí) 在一天中 (0~23) m 分 s 秒 S 毫秒 E 星期 D 一年中的第幾天 F 一月中第幾個(gè)星期幾 w 一年中第幾個(gè)星期 W 一月中第幾個(gè)星期 a 上午 / 下午 標(biāo)記符 k 時(shí) 在一天中 (1~24) K 時(shí) 在上午或下午 (0~11) z 時(shí)區(qū) */ |
1、Date——>String
String sdate;
Date ddate;
……
sdate=(new SimpleDateFormat("yyyy-MM-dd")).format(ddate);
2、String——>Date
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
sdf.setLenient(false);
String sdate="2006-06-06";
Date ddate;
ddate=sdf.parse(sdate);
另外Integer、Double等數(shù)據(jù)包裝類(lèi)型與String類(lèi)型的相互轉(zhuǎn)換為:
(以Integer為例)
Integer——>String
Integer ii;
String si;
……
si=ii.toString;
String——>Intteger
Integer ii;
String si;
……
ii=Integer.valueOf(si);
附:
日期和時(shí)間模式(摘自JDK1.50 DOC)
日期和時(shí)間格式由日期和時(shí)間模式 字符串指定。在日期和時(shí)間模式字符串中,未加引號(hào)的字母 'A'
到 'Z'
和 'a'
到 'z'
被解釋為模式字母,用來(lái)表示日期或時(shí)間字符串元素。文本可以使用單引號(hào) ('
) 引起來(lái),以免進(jìn)行解釋。"''"
表示單引號(hào)。所有其他字符均不解釋?zhuān)恢皇窃诟袷交瘯r(shí)將它們簡(jiǎn)單復(fù)制到輸出字符串,或者在分析時(shí)與輸入字符串進(jìn)行匹配。
定義了以下模式字母(所有其他字符 'A'
到 'Z'
和 'a'
到 'z'
都被保留):
字母 日期或時(shí)間元素 表示 示例 G
Era 標(biāo)志符 Text AD
y
年 Year 1996
;96
M
年中的月份 Month July
;Jul
;07
w
年中的周數(shù) Number 27
W
月份中的周數(shù) Number 2
D
年中的天數(shù) Number 189
d
月份中的天數(shù) Number 10
F
月份中的星期 Number 2
E
星期中的天數(shù) Text Tuesday
;Tue
a
Am/pm 標(biāo)記 Text PM
H
一天中的小時(shí)數(shù)(0-23) Number 0
k
一天中的小時(shí)數(shù)(1-24) Number 24
K
am/pm 中的小時(shí)數(shù)(0-11) Number 0
h
am/pm 中的小時(shí)數(shù)(1-12) Number 12
m
小時(shí)中的分鐘數(shù) Number 30
s
分鐘中的秒數(shù) Number 55
S
毫秒數(shù) Number 978
z
時(shí)區(qū) General time zone Pacific Standard Time
;PST
;GMT-08:00
Z
時(shí)區(qū) RFC 822 time zone -0800