关于”输入时间“的问题,如何解决?

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        //see();
        add();
    }

    private static void see() throws SQLException {
        //1.注册驱动
        //Class.forName("com.mysql.jdbc.Driver");
        //2.获取连按对象

        String url = "jdbc:mysql:///test";
        String username = "root";
        String password = "000";
        Connection conn = DriverManager.getConnection(url, username, password);
        //3,定义SQL

        String sql2 = "select * from application;";


        //4.获取执行对象
        Statement stmt = conn.prepareStatement(sql2);


        //5.执行sql
        ResultSet rs2 = stmt.executeQuery(sql2);

        //创建集合
        List<Application> list = new ArrayList<>();

        //6.处理结果
        while (rs2.next()){
            Application application = new Application();
            String stu_username = rs2.getString("stu_username");
            String tea_username = rs2.getString("tea_username");
            String title = rs2.getString("title");
            String des = rs2.getString("des");
            Date atime = rs2.getDate("atime");
            LocalDateTime time = rs2.getTimestamp("time").toLocalDateTime();

           //赋值
            application.setStu_username(stu_username);
            application.setTea_username(tea_username);
            application.setTitle(title);
            application.setDes(des);
            application.setAtime(atime);
            application.setTime(time);

            list.add(application);//存入集合
        }

        System.out.println(list);

        //7.释放资源
        rs2.close();
        stmt.close();
        conn.close();
    }

    private static void add() throws Exception {
        Scanner sc = new Scanner(System.in);
        //连接
        String url = "jdbc:mysql:///test";
        String username = "root";
        String password = "000";
        Connection conn = DriverManager.getConnection(url, username, password);

        //定义插入的内容
        System.out.print("请输入用户名:");
        String stu_username = sc.next();
        String tea_username = sc.next();
        System.out.print("请输入标题:");
        String title = sc.next();
        System.out.print("请输入描述:");
        String des = sc.next();

        System.out.print("请输入验收时间:");
        String input = sc.next();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setLenient(false); // 禁用 lenient 模式

        Date atime = null;
        try {
            atime = sdf.parse(input);
            System.out.println(atime.toString());
        } catch (ParseException e) {
            System.out.println("日期格式不正确!");
        }


        LocalDateTime time = LocalDateTime.now();

        //定义sql语句
        String sql1 = "insert into application(stu_username,tea_username,title,des,atime,time) values(?,?,?,?,?,?);";

        PreparedStatement pstmt = conn.prepareStatement(sql1);

        // 修改为
        pstmt.setString(1, stu_username);
        pstmt.setString(2, tea_username);
        pstmt.setString(3, title);
        pstmt.setString(4, des);
        pstmt.setDate(5, new java.sql.Date(atime.getTime()));
        pstmt.setTimestamp(6, Timestamp.valueOf(time));

        //执行sql
        int count = pstmt.executeUpdate();   //影响的行数
        //处理结果
        System.out.println(count > 0);
        //释放资源
        pstmt.close();
        conn.close();
    }
}

86行到98行,无法正常输入时间,总是输出格式错误。

next() 遇到空格结束,所以只获取了 年月日的部分

img

改成:

sc.nextLine();
System.out.print("请输入验收时间:");
String input = sc.nextLine();