after before compareTo

发布时间 2023-12-04 18:05:15作者: 我的心儿

入场日期要求晚于该人员在系统中登记的最后一次离场日期 Java根据以上代码需求例子

import java.sql.*;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        // 根据实际情况,替换以下连接信息
        String url = "jdbc:mysql://localhost:3306/database_name";
        String username = "your_username";
        String password = "your_password";

        try {
            // 连接数据库
            Connection connection = DriverManager.getConnection(url, username, password);

            // 准备SQL查询语句
            String sql = "SELECT MAX(leave_date) AS last_leave_date FROM employee_leave WHERE employee_id = ?";

            // 替换以下变量为实际的员工ID和入场日期
            int employeeId = 123;
            Date entryDate = new Date();

            // 执行查询
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setInt(1, employeeId);
            ResultSet resultSet = statement.executeQuery();

            // 获取最后一次离场日期
            Date lastLeaveDate = null;
            if (resultSet.next()) {
                lastLeaveDate = resultSet.getDate("last_leave_date");
            }

            // 比较入场日期和最后一次离场日期
            if (lastLeaveDate != null && entryDate.after(lastLeaveDate)) {
                // 入场日期晚于最后一次离场日期,符合要求
                System.out.println("入场日期晚于最后一次离场日期");
            } else {
                // 入场日期早于或等于最后一次离场日期,不符合要求
                System.out.println("入场日期早于或等于最后一次离场日期");
            }

            // 关闭连接
            resultSet.close();
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

离场日期根据厂商人员查询是否存Java在离场信息,然后在取最后一次立场日期和入场日期比较并做提示 Java与sql结合示例

import java.sql.*;

public class Main {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            // 创建数据库连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_name", "username", "password");
            
            // 创建SQL语句
            String query = "SELECT leave_date FROM employee_leave ORDER BY leave_date DESC LIMIT 1";
            
            // 创建Statement对象
            statement = connection.createStatement();
            
            // 执行SQL查询
            resultSet = statement.executeQuery(query);
            
            if (resultSet.next()) {
                Date lastLeaveDate = resultSet.getDate("leave_date");
                Date entryDate = // 获取入场日期
                
                if (lastLeaveDate.compareTo(entryDate) > 0) {
                    System.out.println("最后一次离场日期晚于入场日期");
                    // 在这里可以进行相应的处理
                } else {
                    System.out.println("最后一次离场日期不晚于入场日期");
                    // 在这里可以进行相应的处理
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            try {
                if (resultSet != null) {
                    resultSet.close();
                }


                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

入场日期不得早于劳动合同起始日期 Java实现代码例子

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        String contractStartDate = "2022-01-01"; // 劳动合同起始日期
        String entryDate = "2022-02-01"; // 入场日期

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            // 将日期字符串转换为Date类型
            Date contractStartDateObj = sdf.parse(contractStartDate);
            Date entryDateObj = sdf.parse(entryDate);

            // 比较入场日期和劳动合同起始日期
            if (entryDateObj.before(contractStartDateObj)) {
                System.out.println("入场日期早于劳动合同起始日期");
                // 在这里可以进行相应的处理
            } else {
                System.out.println("入场日期不早于劳动合同起始日期");
                // 在这里可以进行相应的处理
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}