getText()方法为什么要加toString

来源:互联网 发布:化妆品淘宝店怎么开 编辑:程序博客网 时间:2024/05/29 02:14

EditText中的getText()方法的返回值为CharSequence,如果我们想要获得string类型数据的话,需要在后边加上.toString

 

 

     另外,String类型转为int:Integer.parseInt(str);

 

               String类型转为float::Float.parseFloat(str);

 

     string和Date的相互转换

  1. DateFormat format = new SimpleDateFormat("yyyy-MM-dd");        
  2. Date date = null;   
  3. String str = null;                 
  4.            
  5. // StringDate   
  6. str = "2007-1-18";         
  7. try {   
  8.     date = format.parse(str);  // Thu Jan 18 00:00:00 CST 2007   
  9. catch (ParseException e) {   
  10.     e.printStackTrace();   
  11. }   
  12.            
  13. date = java.sql.Date.valueOf(str);  // 只保留日期部分,返回的是java.sql.Date  2007-01-18   
  14.            
  15. // DateString   
  16. date = new Date();   // Thu Jan 18 21:35:01 CST 2007   
  17. /*Calendar cal = Calendar.getInstance();  
  18. date = new Date(cal.getTimeInMillis());*/  
  19. str = format.format(date);  // 2007-01-18   
  20.            
  21. format = DateFormat.getDateInstance(DateFormat.SHORT);   
  22. str = format.format(date);  // 07-1-18   
  23.            
  24. format = DateFormat.getDateInstance(DateFormat.MEDIUM);   
  25. str = format.format(date);  // 2007-1-18   
  26.            
  27. format = DateFormat.getDateInstance(DateFormat.FULL);   
  28. str = format.format(date);  // 2007年1月18日 星期四
原创粉丝点击