データベース内のフィールドタイプがdatetimeでjavaのDateタイプに変換
JDBC:
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
Timestamp timestamp = rs.getTimestamp("action_time");
Date date = new Date(timestamp.getTime());
String dateStr = dateUtil.date2String(date);
}
package com.ydtf.ipcc.sms.util;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author Shixf
* @date 2018 5 4
*/
public class DateUtil {
/**
* Date String
* @param date
* @return
*/
public String date2String(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sdf.format(date);
return dateStr;
}
/**
* Timestamp String
* datetime
* @param timestamp
* @return
*/
public String time2String(Timestamp timestamp) {
Date date = new Date(timestamp.getTime());
String dateStr = date2String(date);
return dateStr;
}
}