java 各种类型间的变换

java 各种类型间的转换

int -> String        int i;   方法1:s=i+"";

                                     方法2:String s=String.valueOf(i);

String -> int        String s;   方法1:Integer.parseInt(s);

                                           方法2:Integer.valueOf(s).intValue();

String -> Date     方法1:Date date = new Date("2013-04-18");

                            方法2:SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                                         String str = "2013-04-18";

                                         Date date = sdf.parse(str);

Date -> String     SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 

                             java.util.Date date=new java.util.Date(); 

                             String str=sdf.format(date); 

String -> Timestamp      Timestamp ts = Timestamp.valueOf("2013-04-18");

Timestamp -> String      Timestamp ts = new Timestamp(System.currentTimeMillis()); 

                                         DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

                                         方法1:String str = sdf.format(ts);

                                         方法2:String str = ts.toString();

Timestamp -> Date         Date date = new Timestamp(System.currentTimeMillis());

Date -> Timestamp         Timestamp ts = new Timestamp(date.getTime());

 

1 楼 西铁城 22 小时前  
lz在发表前前检查一下内容,把"改一下。