在开发
web
应用中,针对不同的数据库类型,我们需要在我们的程序中做各种不同的转换
数字―――>数字数字对象
- int i = 169;
- Integer io = new Integer( i );
- i = io.intValue();
数字―――>字符串
- String s = String.valueOf( value); // 其中 value 为任意一种数字类型。
- 或者
- String a=Integer.toString('整型');
字符串―――>数字
- String s = "169";
- byte b = Byte.parseByte( s );
- int i = Integer.parseInt( s );
- Double d = Double.parseDouble( s );
日期―――>字符串
- date1.toString()
字符串―――>日期
将字符串类型的日期dateString转换为一个Date
- DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS", Locale.ENGLISH);//设定格式
- dateFormat.setLenient(false);
- java.util.Date timeDate = dateFormat.parse(dateString);//util类型
- java.sql.Date dateTime = new java.sql.Date(timeDate.getTime());//sql类型
日期―――>日期
日期date转换为timestamp
法一:使用
new Timestamp(long)
- Timestamp t = new Timestamp(new Date().getTime());
法二:使用
Timestamp(int year,int month,int date,int hour,int minute,int second,int nano)
- Timestamp tt = new Timestamp(Calendar.getInstance().get(
- Calendar.YEAR) - 1900, Calendar.getInstance().get(
- Calendar.MONTH), Calendar.getInstance().get(
- Calendar.DATE), Calendar.getInstance().get(
- Calendar.HOUR), Calendar.getInstance().get(
- Calendar.MINUTE), Calendar.getInstance().get(
- Calendar.SECOND), 0);