java中如何将一个2009-1-18 19:54:40的Date对象转换成2009-1-18 00:00:00的Date对象

来源:百度知道 编辑:UC知道 时间:2024/05/05 00:00:35
rt
非常感谢!

SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
String date=sf.format(new Date());
System.out.println(date);
最简单的方法!

用dateformat类

不过太麻烦

我喜欢用Calendar来拼装

同意楼上的,不过dateformat估计他还是得用

//yourdate就是存你那个日期数据的变量

Calendar cal=Calendar.getInstance();
cal.setTime(youdate);
StringBuffer temp=new StringBuffer();
temp.append(String.valueOf(cal.get(Calendar.YEAR)));
temp.append("-"+String.valueOf((cal.get(Calendar.MONTH)+1)));//月份是从0月到11月,所以要加1
temp.append("-"+String.valueOf(cal.get(Calendar.DAY_OF_MONTH)));
temp.append(" 00:00:00");
//以上,数据提取完毕,然后设置格式
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
newdate=sdf.parse(temp);

//newdate就是你要的东西了,至于newdate的类型,你自己定义,我不知道你用的是哪个包里的Date类