java 中String ,Date,long 和Timestamp类型的转换
一、String與Date(java.util.Date)的轉(zhuǎn)換
? 1、String--->Date
String str="2014/1/11 12:34:25";
Date date=new Date();
DateFormat?sdf?=?new?SimpleDateFormat("yyyy/MM/dd?HH:mm:ss");??//這里規(guī)定時(shí)間的格式要與String時(shí)間類型的格式相同
date=sdf.parse(str);//date類型
2、Date--->String
Date date=new Date();
DateFormat?sdf?=?new?SimpleDateFormat("yyyy/MM/dd?HH:mm:ss");??//Date轉(zhuǎn)String時(shí),這里的格式隨意
String tr=sdf.format(date);
二、String與Timestamp的轉(zhuǎn)換
1、String--->Timestamp
String str="2014/1/11 12:34:25";
Timestamp ts=new Timestamp();
ts=Timestamp.valueOf(str);
2、Timestamp--->String
方法一:
Timestamp ts=new Timestamp(System.currentTimeMillis());
String str=ts.toString();
方法二:
DateFormat?sdf?=?new?SimpleDateFormat("yyyy/MM/dd?HH:mm:ss");??
String str=sdf.format(ts);
三、Date(java.util.Date)與Timestamp的轉(zhuǎn)換
1、Timestamp--->Date
Timestamp ts=new Timestamp(System.currentTimeMillis());
Date date=new Date();
date=ts;
2、Date--->Timestamp
父類不能直接轉(zhuǎn)換成子類,可以先轉(zhuǎn)成String后,在轉(zhuǎn)Timestamp
Date date=new Date();
Timestamp ts=new Timestamp(date.getTime());
四、long與Timestamp的轉(zhuǎn)換
1、long--->Timestamp
long l="";
new Timestamp(l);
2、Timestamp--->long
Timestamp ts=new Timestamp();
long now=ts.getDateTime();
轉(zhuǎn)載于:https://www.cnblogs.com/hujunli90/p/3515185.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的java 中String ,Date,long 和Timestamp类型的转换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。