现在的位置: 首页 > 综合 > 正文

java类型和数据库字段类型的转换

2013年08月05日 ⁄ 综合 ⁄ 共 1191字 ⁄ 字号 评论关闭
在开发web应用中,针对不同的数据库类型,我们需要在我们的程序中做各种不同的转换
数字―――>数字数字对象

  1. int i = 169;    
  2. Integer io = new Integer( i );    
  3. i = io.intValue();    
数字―――>字符串

  1. String s = String.valueOf( value); // 其中 value 为任意一种数字类型。    
  2. 或者   
  3. String a=Integer.toString('整型');   
  4.   
字符串―――>数字

  1. String s = "169";    
  2. byte b = Byte.parseByte( s );    
  3. int i = Integer.parseInt( s );    
  4. Double d = Double.parseDouble( s );   
  5.   
 
日期―――>字符串
 
 
  1. date1.toString()  
字符串―――>日期
将字符串类型的日期dateString转换为一个Date
  1. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS", Locale.ENGLISH);//设定格式   
  2. dateFormat.setLenient(false);   
  3. java.util.Date timeDate = dateFormat.parse(dateString);//util类型   
  4. java.sql.Date dateTime = new java.sql.Date(timeDate.getTime());//sql类型   
  5.   
日期―――>日期
日期date转换为timestamp
法一:使用new Timestamp(long)

  1. Timestamp t = new Timestamp(new Date().getTime());   
 
法二:使用Timestamp(int year,int month,int date,int hour,int minute,int second,int nano)
  1. Timestamp tt = new Timestamp(Calendar.getInstance().get(   
  2. Calendar.YEAR) - 1900, Calendar.getInstance().get(   
  3. Calendar.MONTH), Calendar.getInstance().get(   
  4. Calendar.DATE), Calendar.getInstance().get(   
  5. Calendar.HOUR), Calendar.getInstance().get(   
  6. Calendar.MINUTE), Calendar.getInstance().get(   
  7. Calendar.SECOND), 0);   
 

抱歉!评论已关闭.