一般来说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()函数插入当前时间