千万一定注意
- yyyy-MM-dd HH:mm:ss里面,
MM代表月份,mm代表分钟,大小写一定不能乱写 - yyyy-MM-dd HH:mm:ss里面,
HH代表24小时制,hh代表12小时制.
DateUtil:
public static Date transferDateTime(String dateStr) throws ParseDateException{
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
throw new ParseDateException();
}
return date;
}
public static String transferDateToString(Date date){
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
String dateStr = sdf.format(date);
return dateStr;
}
对时间的加减
String sendTime = "2016-12-29 00:00:00":
Date date = DateUtils.transferDateTime(sendTime);
//减五分钟
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, -5);
String startTime = DateUtils.transferDateToString(calendar.getTime());
//加五分钟
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(date);
calendar2.add(Calendar.MINUTE, 5);
String endTime = DateUtils.transferDateToString(calendar2.getTime());
Map<String, String> map = new HashMap<>(2);
map.put("startTime", startTime);
map.put("endTime", endTime);