import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) throws Exception {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //首先设定日期时间格式,HH指使用24小时制,hh是使用12小时制
//获取当前时间,并转换为String
Date date = new Date();//获取当前时间,Date型
String dateStr = dateFormat.format(date); // Date型转换成String型,可直接存储进数据库
System.out.println("String:" + dateStr);
//将当前时间转换为从1970年1月1日到现在所经历的毫秒数
Long dateLong = date.getTime(); // Date型转换成Long型毫秒数,用于计算
System.out.println("毫秒数dateLong:" + dateLong);
Timestamp timestamp = new Timestamp(dateLong); //Long型毫秒数转为Timestamp型,可直接存储进数据库
System.out.println("Timestamp:&#
java、MySQL时间类型的转换及存储(Timestamp、Date、String、Long)
最新推荐文章于 2024-05-10 06:06:58 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)