标签:system 打印 cut except exce nec 显示 [] stat
1.mysql数据库中有这样一个表:
mysql> select * from test_table;
+----------+---------------------+
| username | date |
+----------+---------------------+
| chengyu | 1990-10-04 00:00:00 |
| chengpei | 1980-09-12 12:23:01 |
+----------+---------------------+
其中date字段是datetime类型的;从数据库中将date字段取出来:
public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_demo?user=root&password=root"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from test_table"); while(rs.next()){ Date date = rs.getDate("date"); System.out.println(date); } }
Date取出来是java.sql.Date类型的;打印但是Date的toString()方法;显示如下:
1990-10-04
1980-09-12
现在将date取出来,转化为字符串,再次打印出来:
public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_demo?user=root&password=root"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from test_table"); while(rs.next()){ Date date = rs.getDate("date"); String date2 = new SimpleDateFormat("yyyy年MM月dd日").format(date); System.out.println(date2); } }
打印结果如下:
1990年10月04日
1980年09月12日
2.
标签:system 打印 cut except exce nec 显示 [] stat
原文地址:http://www.cnblogs.com/tenWood/p/6246414.html