首页 > 编程语言 > 详细

java日期处理SimpleDateFormat等

时间:2017-01-03 22:09:26      阅读:246      评论:0      收藏:0      [点我收藏+]

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.

  

  

java日期处理SimpleDateFormat等

原文:http://www.cnblogs.com/tenWood/p/6246414.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!