查询日期转换思路
需求:根据前端传的string 类型日期 转成 需要的 ‘/’分隔的日期 String类型的日期
public Message dateFormat(String s) {
String[] split = s.split("/");
// =1 表示不是 "-" 分隔
if (split.length == 1) {
String[] sp = s.split("-");
if (sp.length == 1 || sp.length > 3) {
return Message.failure("日期格式不正确");
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-d");
Date parse = null;
try {
parse = simpleDateFormat.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
String format = dateFormat.format(parse);
return Message.success(format,"日期转换成功");
} else if (split.length > 3) {
return Message.failure("日期格式不正确");
} else {
return Message.success(s,"日期转换成功");
}
}
调用的 地方 判断 转换 状态,返回 或者继续