@Test
public void test4() throws ParseException {
Test1 test = new Test();
//生成特定时间
DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date myDate2 = dateFormat2.parse("2022-12-31 14:24:18");
Date str = test.timeClear(myDate2);
System.out.println("修改后时间:"+str);
//时间装换格式输出
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datef = sdf.format(str);
System.out.println("格式化后的日期:" + datef);
}
public Date timeClear(Date date) {
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(date);
rightNow.set(Calendar.HOUR_OF_DAY, 0); //将小时变成0
rightNow.set(Calendar.MINUTE, 0); //将分钟变成0
rightNow.set(Calendar.SECOND, 0); //将秒变成0
//rightNow.set(Calendar.MILLISECOND, 0);
Date dt1 = rightNow.getTime();
return dt1;
}
Java将时间date的时分秒置0
最新推荐文章于 2024-04-25 21:04:33 发布