java中String,Date,long和Timestamp类型的转换
2014-11-07 20:20
886 查看
一、String与Date(java.util.Date)的转换
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"); //这里规定时间的格式要与String时间类型的格式相同
date=sdf.parse(str);//date类型
2、Date--->String
Date date=new Date();
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //Date转String时,这里的格式随意
String tr=sdf.format(date);
二、String与Timestamp的转换
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的转换
1、Timestamp--->Date
Timestamp ts=new Timestamp(System.currentTimeMillis());
Date date=new Date();
date=ts;
2、Date--->Timestamp
父类不能直接转换成子类,可以先转成String后,在转Timestamp
Date date=new Date();
Timestamp ts=new Timestamp(date.getTime());
四、long与Timestamp的转换
1、long--->Timestamp
long l="";
new Timestamp(l);
2、Timestamp--->long
Timestamp ts=new Timestamp();
long now=ts.getDateTime();
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"); //这里规定时间的格式要与String时间类型的格式相同
date=sdf.parse(str);//date类型
2、Date--->String
Date date=new Date();
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //Date转String时,这里的格式随意
String tr=sdf.format(date);
二、String与Timestamp的转换
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的转换
1、Timestamp--->Date
Timestamp ts=new Timestamp(System.currentTimeMillis());
Date date=new Date();
date=ts;
2、Date--->Timestamp
父类不能直接转换成子类,可以先转成String后,在转Timestamp
Date date=new Date();
Timestamp ts=new Timestamp(date.getTime());
四、long与Timestamp的转换
1、long--->Timestamp
long l="";
new Timestamp(l);
2、Timestamp--->long
Timestamp ts=new Timestamp();
long now=ts.getDateTime();
相关文章推荐
- java、MySQL时间类型的转换及存储(Timestamp、Date、String、Long)
- java、MySQL时间类型的转换及存储(Timestamp、Date、String、Long)
- Java中的String和Date、Timestamp之间的转换+long类型的陷阱
- Java 日期时间 Date类型,long类型,String类型表现形式的转换
- java中数据类型转换 Integer String Long Float Double Date
- java日期类型转换总结date timestamp calendar string
- java类型转换 Integer String Long Float Double Date
- java中String,int,Integer,char,Long,Float,Double,Date 类型转换
- java类型转换 Integer String Long Float Double Date
- Java 日期时间 Date类型,long类型,String类型表现形式的转换
- Java 日期时间 Date类型,long类型,String类型表现形式的转换
- Java 日期时间 Date类型,long类型,String类型表现形式的转换
- Java 日期时间 Date类型,long类型,String类型表现形式的转换
- java中数据类型转换 Integer String Long Float Double Date Char
- Java 日期时间 Date类型,long类型,String类型表现形式的转换
- Java 日期时间 Date类型,long类型,String类型表现形式的转换
- java中数据类型转换 Integer String Long Float Double Date
- Java 日期时间 Date类型,long类型,String类型表现形式的转换
- java类型转换 Integer String Long Float Double Date
- Java中Long、String、Date 类型之间的转换