方法一:日期类型精确到秒
Date startTime =new Date();
Date endTime =new Date();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
startTime = calendar.getTime();
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 59);
endTime = calendar.getTime();
方法二:字符串类型日期转换为日期类型并精确到秒
String startTime;
String endTime;
//开始时间和结束时间精确到秒
Date start_time= this.getDateTime(startTime.trim()+"000000", "yyyyMMddHHmmss");
Date end_time= this.getDateTime(endTime.trim()+"235959", "yyyyMMddHHmmss");
/**
* 字符串时间按格式转成 Date时间对象
*
* @param dateStr
* 字符串时间
* @param pattern
* 字符串时间格式
* @return
* @throws ParseException
*/
public static Date getDateTime(String dateStr, String pattern)
throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
return formatter.parse(dateStr);
}