JDBC1-4时间处理

测试时间处理(java.sql.Date,Time,Timestamp)

/**
 * 测试时间处理(java.sql.Date,Time,Timestamp)
 */

public class Demo07 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        String URL = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "123456";
        try {

            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(URL, user, password);
            for (int i = 0; i <1000 ; i++) {

                ps=conn.prepareStatement("insert into t_user (username,pwd,regTime,lastLoginTime) values (?,?,?,?)");
                int rand=100000000+new Random().nextInt(100000000);
                Date date=new Date(System.currentTimeMillis()-rand);
                Timestamp timestamp=new Timestamp(System.currentTimeMillis()-rand);
                ps.setObject(1,"小明"+i);
                ps.setObject(2,"1234565");
                ps.setObject(3,date);
                ps.setObject(4,timestamp);
                ps.execute();
            }



        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                if (ps!=null){
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (conn!=null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

取出指定时间段的数据

public class Demo08 {
    /**
     * 将日期转为LONG数字(yyyy-mm-dd hh:mm:ss)
     */
    public static long str2Date(String dateStr) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            return format.parse(dateStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs;
        String URL = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "123456";


        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(URL, user, password);
            String sql = "SELECT * FROM t_user where regTime>? and regTime<?";
            String sql2 = "SELECT * FROM t_user where lastLoginTime>? and lastLoginTime<?";
            ps = connection.prepareStatement(sql2);
            long data1=str2Date("2020-6-22 10:22:45");
            System.out.println("LONG"+data1);
            Date start = new Date(str2Date("2020-6-22 00:00:01"));
            Date end = new Date(str2Date("2020-6-23 22:22:45"));
            ps.setObject(1, start);
            ps.setObject(2, end);
            rs = ps.executeQuery();
            while (rs.next()) {
               // System.out.println(rs.getInt("id")+"--"+rs.getString("username")+"--"+rs.getDate("regTime"));
                System.out.println(rs.getInt("id")+"--"+rs.getString("username")+"--"+rs.getTimestamp("lastLoginTime"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值