字符串轉(zhuǎn)化為日期格式 如何將日期格式轉(zhuǎn)換成字符串?
如何將日期格式轉(zhuǎn)換成字符串?如果您想將日期輸出為2005年6月9日,我們可以這樣寫:simpledateformat SDF=new simpledateformat(“yyy-MM-DD”)stri
如何將日期格式轉(zhuǎn)換成字符串?
如果您想將日期輸出為2005年6月9日,我們可以這樣寫:simpledateformat SDF=new simpledateformat(“yyy-MM-DD”)string str=sdf.format格式(parse(“2005-06-09 00:00:00”)str將以我們設(shè)置的格式輸出。使用的API是SimpleDataFormat,它屬于java.text.SimpleDataFormat所以請記住導(dǎo)入!用法:simpledateformat SDF=new simpledateformat(“yyy-MM-DD HH:MM:SS”)是最重要的一行,它建立了轉(zhuǎn)換的格式。Yyyy是一年中完整的一年,mm是月份,DD是日期。
怎樣把日期字符串轉(zhuǎn)換成日期格式?
在Java中,要將字符串轉(zhuǎn)換為日期格式,需要使用SimpleDataFormat類中的parse方法。format方法傳入的參數(shù)的格式含義是:yyyy:表示四位數(shù)的年,mm:表示月,DD:表示日,HH:表示24小時,HH:表示12小時,mm:表示分鐘,SS:表示秒。有關(guān)詳細信息,請參見示例:simpledateformat SDF=new simpledateformat(“yyyy-MM-dd”)Date=解析sdf.parse("2016-06-06") 系統(tǒng)輸出打印( sdf.format格式(date))輸出結(jié)果為:2016-06-simpledateformat SDF=new simpledateformat(“yyyy-MM-DD HH:MM:SS”)date日期=解析sdf.parse("2016-06-06 16:24:50") 系統(tǒng)輸出打印( sdf.format格式(日期)輸出結(jié)果:2016-06-06 16:24:50