- 时间戳去掉毫秒数:
public void test56() throws ParseException {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println("old time stamp: "+timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long time = sdf.parse(sdf.format(timestamp)).getTime();
timestamp = new Timestamp(time);
System.out.println("new time stamp: "+timestamp);
/** 运行结果:
old time stamp: 2017-11-14 18:07:24.339
new time stamp: 2017-11-14 18:07:24.0
*/
}
2.日期转换规定的格式:
public void test56() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
System.out.println("old format date: "+date);
String newDate = sdf.format(date);
System.out.println("new format date: "+newDate);
/*
* 运行结果:
* old format date: Tue Nov 14 18:11:46 CST 2017
new format date: 2017-11-14 18:11:46
*/
}
3.字符串转日期:
public void test56() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sDate = "2017-11-14 18:11:46";
Date date = sdf.parse(sDate);
System.out.println(date);
/*
* 运行结果:
* Tue Nov 14 18:11:46 CST 2017
*/
}