String转Long
Long long=new SimpleDateFormat("yyyyMMddHHmmss").parse(String).getTime();
Long转String
String string = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(Long));
String转Timestamp
Timestamp.valueOf(String)
Timestamp转String
Timestamp ts = new Timestamp(System.currentTimeMillis());
String string=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(ts);
String转Date
Date date=new SimpleDateFormat("yyyyMMddHHmmss").parse(String)
Date转String
String string=new SimpleDateFormat("yyyyMMddHHmmss").format(date)
Date转Long
Long long=date.getTime();
Long转Date
String string=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(Long));
Date date=new SimpleDateFormat("yyyyMMddHHmmss").parse(string);
Date转Timestamp
Timestamp ts = new Timestamp(date.getTime());
Timestamp转Date
Timestamp ts = new Timestamp(System.currentTimeMillis());
Date date=new Date();
date=ts;
Long转Timestamp
Long time1=System.currentTimeMillis();
Timestamp time=new Timestamp(time1);
Timestamp转Long
long now=t.getDateTime();
注意:如果数据库是dateTime类型的,即使java代码中声明的是Timestamp,但还是用.getTime()方法来获取这个Long类型的时间。
MySql的时间类型有 Java中与之对应的时间类型
date java.sql.Date
Datetime java.sql.Timestamp
Timestamp java.sql.Timestamp
Time java.sql.Time
Year java.sql.Date
所以,如果数据库是dateTime,则先用java转换成timestamp类型
Date date = new Date();//获得系统时间.
String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);//将时间格式转换成符合Timestamp要求的格式.
Timestamp xx = Timestamp.valueOf(nowTime);
然后存入数据库