Date dt=new Date();
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy年MM月dd HH:mm:ss");
String s=sdf.format(dt).toString(); /** 类型转换**/
s=s.substring(0, s.length()-1); /**删除末尾的字符串结束符**/
Test test=new Test();
test.setCurrentTime(java.sql.Date.valueOf(s));
Date类下有很多方法啊,如:getHours()//获得Date对象表示的小时。虽然已过时,但还是能用的。
其它相应的方法也都有,自己去帮助文档里查吧。
用Date date = new date();获取当前时间
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
Date date = new Date();
// String time=date.toLocaleString(); //方法过时了
// System.out.println(time);
SimpleDateFormat formater= new SimpleDateFormat("yyyy年MM月dd HH:mm:ss");
String time=formater.format(date);
System.out.println(time);
}
SimpleDateFormat()里面支持yy mm dd hh mm ss等 格式随便