微信技术群:Day9884125
一 根据输入的日期组装指定的时间点
public class DateDmeo{
private final static String STRING_FORMAT = "yyyy-MM-dd";
private final static String INTACT_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static void main(String[] args) throws Exception{
DateDmeo de = new DateDmeo();
Date res = de.assembleDate(new Date(), "22:34");
System.out.println("date=" + res);
}
// 组装特定日期
public Date assembleDate(Date date, String time){
String stringDate = null;
try {
stringDate = getStringDate(date, STRING_FORMAT);
System.out.println("stringDate=" + stringDate);
} catch (Exception e) {
e.printStackTrace();
}
// 拼装成需要的string结果
String result = stringDate +" " + time +":00";
// 将string类型的日期转化成date类型
Date dateResult = null;
try {
dateResult = stringToDate(result, INTACT_FORMAT);
} catch (Exception e) {
e.printStackTrace();
}
return dateResult;
}
// 将Date类型的转化成指定样式的string类型
public String getStringDate(Date date, String format) throws Exception {
DateFormat df = new SimpleDateFormat(format);
if(date == null || format == null){
throw new Exception("输入日期字符不能为空");
}
return df.format(date);
}
// 将string类型的日期转化成date类型
public Date stringToDate(String str, String format) throws Exception {
Date date = new Date();
try{
date = new SimpleDateFormat(format).parse(str);
}catch(Exception e){
throw new Exception("输入日期不合法");
}
return date;
}
}