ITeye博客

[原]Java对数据库中时间的操作

独家冠名 · 2008-03-15 22:22 · 数据库 · 数据库 · C · Java · SQL · JDBC · Oracle · Markdown

日期类 java.util.Date下面继承了三个子类,分别是 java.sql.Time:针对数据库中的时间 java.sql.Date:针对数据库中的日期 java.sql.Timesamp:数据库中的时期日期部分

对于日期的格式化,常用的是 java.text.DateFormt类的子类java.text.SimpleDateFormat

如果要想获得日期中的具体的年,月,日,时,分,秒等,那么可以用java.util.Calendar类

声明:以上部分完全是API中的内容

/**

  • Java对数据库中时间的操作
  • @author DaHai
  • 如果要取出数据库中的日期或时间,建议用Timestamp类

*/ public class DateTime { public static void main(String[] args) { DateTime oracle = new DateTime(); oracle.query(); }

public Connection getConnection() {
	Connection conn = null;
	try {
		Class.forName("oracle.jdbc.driver.OracleDriver");
		conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:test", "scott", "tiger");
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return conn;
}

public void query() {
	Connection conn = getConnection();
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	String time = "";
	int dt = 0;
	try {
		pstmt = conn.prepareStatement("select scsj from erp_testwhere wygjz=1");
		rs = pstmt.executeQuery();
		while (rs.next()) {
			Date d = rs.getDate("scsj");
			
			//数据库为2008-1-8 16:51:00这种格式
			//Date类即包括时间又包括日期
			//因为是用的getDate方法,当做date取出来,所以时间没有取到
			//因为是用的getTime方法,当做time取出来,所以日期没有取到
			//输出为日期
			System.out.println(rs.getDate("scsj"));
			//输出为时间
			System.out.println(rs.getTime("scsj"));
			
			//注意打印格式,这里的rs.getDate只获取日期,要获得时间需用rs.getTime()
		    //输出应该为2008年01月08日,但此处输出00:00:00,因为d是getDate()方法
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
			System.out.println(sdf.format(d));
			
			dt = getDateTime(rs.getDate("scsj"),Calendar.MONTH);
			System.out.println(dt);
			
			time = this.getDate(rs.getTimestamp("scsj"));
			System.out.println(time);
		}
	}  catch (SQLException e) {
		try {
			if(conn!=null){
				conn.rollback();
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		e.printStackTrace();
	} finally{
		try {
			if(rs!=null){
				rs.close();
				rs = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(pstmt!=null){
				pstmt.close();
				pstmt = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(conn!=null){
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

/**
 * 获得date字段的年份
 * Timestamp类可以取出时间与日期,时间或日期
 * 此处取日期
 * @param ts 
 * @return
 */
public String getDate(Timestamp ts){		
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	return sdf.format(ts);
}

/**
 * 获得date字段的时间
 * Timestamp类可以取出时间与日期,时间或日期
 * 此处取时间
 * @param ts
 * @return
 */
public String getTime(Timestamp ts){
	SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
	return sdf.format(ts);
}

/**
 * 返回数据库中日期中的具体月份
 * @param d
 * @param dt,dt变量为Calendar的静态变量
 * @return
 */
public int getDateTime(Date d ,int dt){
	Calendar c = Calendar.getInstance();
	c.setTime(d);
	return c.get(dt);
}

}

评论(2)

  1. #2 楼 · mj1389541 · 2008-05-14 18:31

    /**

    • Java对数据库中时间的操作
    • @author DaHai
    • 如果要取出数据库中的日期或时间,建议用Timestamp类

    */ public class DateTime { public static void main(String args) { DateTime oracle = new DateTime(); oracle.query(); }

    public Connection getConnection() {
    	Connection conn = null;
    	try {
    		Class.forName("oracle.<SPAN class=hilite1>jdbc</SPAN>.driver.OracleDriver");
    		conn = DriverManager.getConnection("<SPAN class=hilite1>jdbc</SPAN>:oracle:thin:@localhost:1521:test", "scott", "tiger");
    	} catch (ClassNotFoundException e) {
    		e.printStackTrace();
    	} catch (SQLException e) {
    		e.printStackTrace();
    	}
    	return conn;
    }
    
    public void query() {
    	Connection conn = getConnection();
    	PreparedStatement pstmt = null;
    	ResultSet rs = null;
    	String time = "";
    	int dt = 0;
    	try {
    		pstmt = conn.prepareStatement("select scsj from erp_testwhere wygjz=1");
    		rs = pstmt.executeQuery();
    		while (rs.next()) {
    			Date d = rs.<SPAN class=hilite2>getDate</SPAN>("scsj");
    			
    			//数据库为2008-1-8 16:51:00这种格式
    			//Date类即包括时间又包括日期
    			//因为是用的<SPAN class=hilite2>getDate</SPAN>方法,当做date取出来,所以时间没有取到
    			//因为是用的getTime方法,当做time取出来,所以日期没有取到
    			//输出为日期
    			System.out.println(rs.<SPAN class=hilite2>getDate</SPAN>("scsj"));
    			//输出为时间
    			System.out.println(rs.getTime("scsj"));
    			
    			//注意打印格式,这里的rs.<SPAN class=hilite2>getDate</SPAN>只获取日期,要获得时间需用rs.getTime()
    		    //输出应该为2008年01月08日,但此处输出00:00:00,因为d是<SPAN class=hilite2>getDate</SPAN>()方法
    			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    			System.out.println(sdf.format(d));
    			
    			dt = <SPAN class=hilite2>getDate</SPAN>Time(rs.<SPAN class=hilite2>getDate</SPAN>("scsj"),Calendar.MONTH);
    			System.out.println(dt);
    			
    			time = this.<SPAN class=hilite2>getDate</SPAN>(rs.getTimestamp("scsj"));
    			System.out.println(time);
    		}
    	}  catch (SQLException e) {
    		try {
    			if(conn!=null){
    				conn.rollback();
    			}
    		} catch (SQLException e1) {
    			e1.printStackTrace();
    		}
    		e.printStackTrace();
    	} finally{
    		try {
    			if(rs!=null){
    				rs.close();
    				rs = null;
    			}
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		try {
    			if(pstmt!=null){
    				pstmt.close();
    				pstmt = null;
    			}
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		try {
    			if(conn!=null){
    				conn.close();
    				conn = null;
    			}
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    /**
     * 获得date字段的年份
     * Timestamp类可以取出时间与日期,时间或日期
     * 此处取日期
     * @param ts 
     * @return
     */
    public String <SPAN class=hilite2>getDate</SPAN>(Timestamp ts){		
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	return sdf.format(ts);
    }
    
    /**
     * 获得date字段的时间
     * Timestamp类可以取出时间与日期,时间或日期
     * 此处取时间
     * @param ts
     * @return
     */
    public String getTime(Timestamp ts){
    	SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    	return sdf.format(ts);
    }
    
    /**
     * 返回数据库中日期中的具体月份
     * @param d
     * @param dt,dt变量为Calendar的静态变量
     * @return
     */
    public int <SPAN class=hilite2>getDate</SPAN>Time(Date d ,int dt){
    	Calendar c = Calendar.getInstance();
    	c.setTime(d);
    	return c.get(dt);
    }
    

    }

  2. #1 楼 · Jatula · 2008-03-16 12:48

    我想说写得很好,但可以更好! getTime,getDate两个方法用参数来控制输出代码会更加简洁;给个建议而已;

评论功能维护中,当前仅展示历史评论。