javatimestamp转换date_Java中Timestamp类型如何转成Date
类型
记录⼀下:今天在项⽬中遇到了类型是Timestamp的数据,在前台页⾯显⽰时不是我们习惯的常规时间显⽰格式,如下:
调整前台的时间显⽰,有两种思路:在前台js去调整和后台处理后再发送到前台展⽰。
这⾥选择了在后台先进⾏数据格式的处理,再发送到前台(主要前端不熟)
转换的代码如下:
for (int i=0;i
Date dates = new Date();
try {
Timestamp ts = new (i).getUpdateTime().getTime());
dates = ts;
}catch (Exception e){
e.printStackTrace();
}
<(i).setUpdateTime(dates);
}
for (int i=0;i
Date dates = new Date();
try {
Timestamp ts = new (i).getUpdateTime().getTime());
dates = ts;
}catch (Exception e){
e.printStackTrace();
}
<(i).setUpdateTime(dates);
}
这⾥需要注意的是,很多库提供了Timestamp,但是这⾥引⼊的包是:
import java.sql.Timestamp;
import java.sql.Timestamp;
扩展:
String 类型转Date类型
String dateStr = "2010/05/04 12:34:23";
Date date = new Date();
//注意format的格式要与⽇期String的格式相匹配
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); try {
date = sdf.parse(dateStr);
System.out.String());
} catch (Exception e) {
e.printStackTrace();
}
String dateStr = "2010/05/04 12:34:23";
Date date = new Date();
/
/注意format的格式要与⽇期String的格式相匹配
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); try {
date = sdf.parse(dateStr);
System.out.String());
} catch (Exception e) {
e.printStackTrace();
}
Date 类型转String类型
String dateStr = "";
Date date = new Date();
//format的格式可以任意
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss"); try {
dateStr = sdf.format(date);
System.out.println(dateStr);
dateStr = sdf2.format(date);
System.out.println(dateStr);
} catch (Exception e) {
e.printStackTrace();
}
String dateStr = "";
Date date = new Date();
//format的格式可以任意
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss"); try {
dateStr = sdf.format(date);
System.out.println(dateStr);
dateStr = sdf2.format(date);
System.out.println(dateStr);
} catch (Exception e) {
e.printStackTrace();
}
String 类型转Timestamp类型
Timestamp ts = new Timestamp(System.currentTimeMillis());
String tsStr = "2011-05-09 11:49:45";
try {
ts = Timestamp.valueOf(tsStr);
System.out.println(ts);
} catch (Exception e) {
e.printStackTrace();
}
Timestamp ts = new Timestamp(System.currentTimeMillis());
String tsStr = "2011-05-09 11:49:45";
try {
ts = Timestamp.valueOf(tsStr);
System.out.println(ts);
} catch (Exception e) {
e.printStackTrace();
}
Timestamp 类型转String类型
Timestamp ts = new Timestamp(System.currentTimeMillis());
String tsStr = "";
string转date的方法DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
tsStr = sdf.format(ts);
System.out.println(tsStr);
//⽅法⼆
tsStr = ts.toString();
System.out.println(tsStr);
} catch (Exception e) {
e.printStackTrace();
}
Timestamp ts = new Timestamp(System.currentTimeMillis());
String tsStr = "";
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); try {
//⽅法⼀
tsStr = sdf.format(ts);
System.out.println(tsStr);
//⽅法⼆
tsStr = ts.toString();
System.out.println(tsStr);
} catch (Exception e) {
e.printStackTrace();
}
本⽂⽤到的,Timestamp 转 Date
Timestamp ts = new Timestamp(System.currentTimeMillis());
Date date = new Date();
try {
date = ts;
System.out.println(date);
} catch (Exception e) {
e.printStackTrace();
}
Timestamp ts = new Timestamp(System.currentTimeMillis());
Date date = new Date();
System.out.println(date); } catch (Exception e) { e.printStackTrace();
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。