import com.example.demo.Demo.dto.Student;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test03 {
private final static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) throws ParseException {
//TODO 设置当前时间 return 当前时间: 2021-07-06 11:37:17 没有超过今天
String str = "2021-07-06 11:37:17";
Student student = new Student();
student.setId(24);
student.setName("瑶瑶");
student.setBirthday(dateFormat.parse(str));
System.err.println(f(student));
}
private static String f(Student student) {
Date now;
Calendar c = Calendar.getInstance();
try {
//获取当前日期
now = new Date(c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH));
//截取前端传的日期
String format = dateFormat.format(student.getBirthday());
//截取前端传的日期
Date createDateResource = new Date(Integer.parseInt(format.substring(0, 4)), Integer.parseInt(format.substring(5, 7)), Integer.parseInt(format.substring(8, 10)));
if (createDateResource.after(now)) {
//创建时间不能大于当前时间
return "当前时间: " + format + " 超过了今天";
} else {
return "当前时间: " + format + " 没有超过今天";
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}