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

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

/**
 * java.util.Date 
 * – 子类:java.sql.Date 表示年月日 
 * – 子类:java.sql.Time 表示时分秒 
 * –子类:java.sql.Timestamp 表示年月日时分秒
 * 如果需要插入指定日期,可以使用Calendar、DateFormat
 * 测试时间处理(java.sql.Date,Time,Timestamp)
 */
public class Demo07 {
	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps1 = null;
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");

			
			ps1 = conn.prepareStatement("insert into t_user (username,pwd,regTime,lastLoginTime) values (?,?,?,?)");
			ps1.setObject(1, "林伟茂");
			ps1.setObject(2, "123456");
			
			// System.currentTimeMillis() 传入当前时间
			java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
			Timestamp stamp = new Timestamp(System.currentTimeMillis());  // 如果需要插入指定日期,可以使用Calendar、DateFormat
			
			ps1.setDate(3, date);
			ps1.setTimestamp(4, stamp);
			ps1.execute();
			
			System.out.println("插入一个用户,林伟茂");

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

测试插入随机日期

/**
 * 
 * 测试插入随机日期
 */
public class Demo07_1 {
	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps1 = null;
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");

			for (int i = 0; i < 1000; i++) {
				ps1 = conn.prepareStatement("insert into t_user1 (username,pwd,regTime,lastLoginTime) values (?,?,?,?)");
				ps1.setObject(1, "林伟茂"+i);
				ps1.setObject(2, "123456");

				// System.currentTimeMillis() 传入当前时间
				// 插入随机日期
				int rand = 100000000+new Random().nextInt(1000000000);
				
				java.sql.Date date = new java.sql.Date(System.currentTimeMillis()-rand);
				Timestamp stamp = new Timestamp(System.currentTimeMillis()-rand); // 如果需要插入指定日期,可以使用Calendar、DateFormat
				
				ps1.setDate(3, date);
				ps1.setTimestamp(4, stamp);
				ps1.execute();
			}
			System.out.println("插入一个用户,林伟茂");

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

取出指定时间段的数据(数据为 Date 类型的,年月日的形式 例如:2020-02-17)

format(Date date) :将日期格式化成日期/时间字符串。 (返回类型为 String 类型)
parse(String source) :从给定字符串的开始解析文本以生成日期。 (返回类型为 Date 类型)

/**
 * 
 * 测试时间处理(java.sql.Date,Time,Timestamp),取出指定时间段的数据
 * 取出指定时间段的数据(数据为Date类型的,年月日的形式  例如:2020-02-17)
 */
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 {
			// 把传的字符串转成一个Date对象,然后在 getTime(),直接获取long类型的数值
			return format.parse(dateStr).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;
		}
	}

	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");
			
			// 取出指定时间段的数据(数据为Date类型的,年月日的形式  例如:2020-02-17)
			ps = conn.prepareStatement("select * from t_user1 where regTime>? and regTime < ?");
			java.sql.Date start = new java.sql.Date(str2Date("2020-2-15 10:23:45"));
			java.sql.Date end = new java.sql.Date(str2Date("2020-2-18 10:23: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"));
			}

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException 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();
			}
		}
	}
}

取出指定时间段的数据(数据为 Timestamp 类型的,年月日时间的形式 例如:2020-02-17 13:30:27.0)

/**
 * 
 * 测试时间处理(java.sql.Date,Time,Timestamp),取出指定时间段的数据
 * 取出指定时间段的数据(数据为Timestamp类型的,年月日时间的形式  例如:2020-02-17 13:30:27.0)
 */
public class Demo08_1 {
	
	// 将字符串代表的日期转为long数字(格式:yyyy-MM-dd hh:mm:ss)
	public static long str2Date(String dateStr) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			// parse(String source):从给定字符串开始解析文本以生成日期
			// 把传的字符串转成一个Date对象,然后在 getTime(),直接获取long类型的数值
			return format.parse(dateStr).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;
		}
	}

	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");

			// 取出指定时间段的数据(数据为Date类型的,年月日的形式  例如:2020-02-17)
//			ps = conn.prepareStatement("select * from t_user1 where regTime>? and regTime < ?");
//			java.sql.Date start = new java.sql.Date(str2Date("2020-2-15 10:23:45"));
//			java.sql.Date end = new java.sql.Date(str2Date("2020-2-18 10:23: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"));
//			}
			
			
			// 取出指定时间段的数据(数据为lastLoginTime类型的,年月日的形式  例如:2020-02-17 13:30:27.0)
			// order by:数据进行升序
			ps = conn.prepareStatement("select * from t_user1 where lastLoginTime>? and lastLoginTime < ? order by lastLoginTime");
			Timestamp start = new Timestamp(str2Date("2020-2-17 13:10:20"));
			Timestamp end = new Timestamp(str2Date("2020-2-17 15:9:10"));
			ps.setObject(1, start);
			ps.setObject(2, end);
			
			
			rs = ps.executeQuery();
			while (rs.next()) {
				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 (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
发布了45 篇原创文章 · 获赞 1 · 访问量 5214

猜你喜欢

转载自blog.csdn.net/weixin_42814000/article/details/104584898