Java向数据库中插入当前系统时间(年月日时分秒)(java.sql.Date)

一般来说Java语言获取当前系统时间可以用以下方式:

年:yyyy

月:MM

日:dd

时:HH

分:mm

秒:ss

package cn.com.listen;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
	Date dd=new Date();
	//格式化
	SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	String time=sim.format(dd);
	System.out.println(time);
}
}

但是这种方式得到的时间是String类型,如果数据库字段是DATETIME类型的话,是无法从String类型转到Date类型的。

因此我们可以用以下方式:

//获取系统时间直接转换成yy--mm-dd HH:mm:ss 形式
Timestamp d= new Timestamp(System.currentTimeMillis());

需要导入java.sql.Date包

或者你可以在sql语句中使用now()函数插入当前时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值