java 数据类型转换

int -> String

 String s = String.valueOf(i);

 String s = Integer.toString(i);

 String s = "" + i;

String -> int

int i = Integer.parseInt(s);

 int i = Integer.valueOf(s);上面两种都可,最好用第一种,因为第二种返回的其实是Integer对象

 

Integer -> int

int i  = new Integer(1).intValue();

double d = new Integer(1).doubleValue();

 

int -> Integer

Integer i = Integer.valueOf(i);

 

Date -> String

String createdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) ;

 

String -> Date

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2008-08-08 12:10:12");

Timestamp birthday = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(rs.getString("birthday")).getTime()); 

其中rs是jdbc中的结果集resultset

 

double -> BigDecimal

double temp = 3.89075;

BigDecimal big = new BigDecimal(temp);

String  cc = big.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue() + "%";//3.89%

 

1.直接在构造String时建立。 char data[] = {'s', 'g', 'k'}; String str = new String(data);

2.String有方法可以直接转换。 String.valueOf(char[] chr)就可以。 如: char[] cha = {'s','g','h'}; String n = String.valueOf(cha);

 

posted on 2012-02-22 16:04  lovebeauty  阅读(211)  评论(0编辑  收藏  举报

导航