判斷設置的時間是否大於當前時間


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;
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM