类型转换

来源:互联网 发布:java将链接生成二维码 编辑:程序博客网 时间:2024/05/08 12:23
  • 引用类型的转换--如果一个对象与另一个对象没有任何的继承关系,就不能进行类型转换。

        class Person{}
        class Man extends Person{
         public static void main(String[] args){
           Person person = new Man();  //上溯造型(Upcast)--派生类对象赋值给基类对象
           Man man =(Man)Person;  //下溯造型(Downcast强制类型转换)--基类对象赋值给派生类对象
          Man man1=(Man)new Person();//抛出ClassCastException异常
          }
        }
  • 如何将字串 String 转换成Integer ?
    Integer integer=Integer.valueOf(str);
  • 如何将字串String转换成整数int

int i =Integer.parseInt([String]); i =Integer.parseInt([String],[int radix]);

//直接使用静方法,不会生多余的象,但会抛出异常
int i = Integer.valueOf(my_str).intValue();

//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s))

也会抛异常,但会多生一个
: 字串Double, Float, Long的方法大同小异。


  •  如何将整数 int 转换成字符串 String (有三方法: ) 

1)String s = String.valueOf(i);  //直接使用String的静方法,只生一个

2)String s = Integer.toString(i);

3)String s = (new Integer(i)).toString();

4)String s = "" + i;  //生两个String


  • 如何将字符串 String转换成日期 Date 格式
    private   Date   strToDate(String   str)   throws   ParseException   {  
          Date   date   =   null;  
          if   (str   !=   null   &&   str.length()   >   0)   {  
              SimpleDateFormat   simpleDate   =   new   SimpleDateFormat("yyyy-MM-dd");  
              date   =   simpleDate.parse(str);  
          }  
          return   date;  
      }

           DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");       
         DateFormat format 2= new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");       
         Date date = null;  
         String str = null;                
                   
         // String转Date  
         str = "2007-1-18";        
         try {  
             date = format1.parse(str); 
             data = format2.parse(str);
         } catch (ParseException e) {  
             e.printStackTrace();  
         }  
         
         //Date转String
         date=new Date();
         str=format1.format(date);
         str=format2.format(date);
  1. import java.text.DateFormat;
  2. import java.text.SimpleDateFormat;
  3. import java.text.ParseException;
  4. import java.util.Calendar;
  5. import java.util.Date;


  6. DateFormat format = new SimpleDateFormat("yyyy-MM-dd");        
  7. Date date = null;   
  8. String str = null;                 
  9.            
  10. // String转Date   
  11. str = "2007-1-18";         
  12. try {   
  13.     date = format.parse(str);  // Thu Jan 18 00:00:00 CST 2007   
  14. catch (ParseException e) {   
  15.     e.printStackTrace();   
  16. }   
  17.            
  18. date = java.sql.Date.valueOf(str);  // 只保留日期部分,返回的是java.sql.Date  2007-01-18   
  19.            
  20. // Date转String   
  21. date = new Date();   // Thu Jan 18 21:35:01 CST 2007   
  22. /*Calendar cal = Calendar.getInstance();  
  23. date = new Date(cal.getTimeInMillis());*/  
  24. str = format.format(date);  // 2007-01-18   
  25.            
  26. format = DateFormat.getDateInstance(DateFormat.SHORT);   
  27. str = format.format(date);  // 07-1-18   
  28.            
  29. format = DateFormat.getDateInstance(DateFormat.MEDIUM);   
  30. str = format.format(date);  // 2007-1-18   
  31.            
  32. format = DateFormat.getDateInstance(DateFormat.FULL);   
  33. str = format.format(date);  // 2007年1月18日 星期四  
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
下面引用自 http://develop.csai.cn/java/200812101134421742.htm

1.直接创建Date数据类型的实例变量date并直接将其输出:

    /**
    * 创建Date类型的变量 this is date :Sat Dec 06 00:26:54 CST 2008
    * 程序运行过程中直接将当前的时间打印出来
    */
    Date date = new Date();
    System.out.println("this is date :" + date);

  2.将直接实例化的Date数据以String的方式进行输出:

    **
    * 实例方法toString() Sat Dec 06 00:26:54 CST 2008
    * 其实效果也是和程序的直接输出一样只不过将输出的参数类型改变成为String的类型。
    */
    String str_date = date.toString();
    System.out.println("this is date_ str :" + str_date);

  3.采用DateFormat()方法格式化或者过滤所需要的数据参数:

    /**
    * 方法 :DateFormat.getInstance() 输出 :08-12-6 上午12:26
    * DateFormat()方法将日期格式化,格式输出到当前日的分上面。
    */
    String str_date_1 = DateFormat.getInstance().format(date);
    System.out.println("this is str_date_1 :" + str_date_1);

  4.采用SimpleDateFormat()方法格式化或者过滤所需要的数据参数:

    /**
    * 方法 :SimpleDateFormat() 输出 :2008 12 06 00 26 54
    * SimpleDateFormat()放法和DateFormat()类似他可以直接制定到当前日期的某一阶段 例如实例指定当前的秒钟。
    */
    SimpleDateFormat time = new SimpleDateFormat("yyyy MM dd HH mm ss");
    System.out.println("this is SimpleDateFormat :" + time.format(date));

  5.采用MessageFormat()方法格式化或者过滤所需要的数据参数:

    /**
    * 方法 : MessageFormat() 输出 :2008-12-06-00-26:54:2654
    */
    String dateTime = MessageFormat.format(
    "{0,date,yyyy-MM-dd-HH-mm:ss:ms}",
    new Object[] { new java.sql.Date(System.currentTimeMillis()) });
    System.out.println("this is Message datetime :" + dateTime);

    /**
    * 方法 : MessageFormat() 输出 :2008-12-06-00-26:54:2654
    */
    String dateTime = MessageFormat.format(
    "{0,date,yyyy-MM-dd-HH-mm:ss:ms}",
    new Object[] { new java.sql.Date(System.currentTimeMillis()) });
    System.out.println("this is Message datetime :" + dateTime); 

6.采用SimpleDateFormat()方法格式化或者过滤所需要的数据参数:

    /**
    * 方法 : SimpleDateFormat() 输出 : 2008-12-06
    * 类似于前面所讲到的SimpleDateFormat()方法、其实原理都是相同的只不过在处理过程中采用别的附加条件。
    *
    */
    SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd"); // 格式化当前系统日期
    String dateTime_1 = dateFm.format(new java.util.Date());
    System.out.println("this is SimpleDateFormat :" + dateTime_1);

  7.采用DateFormat()方法格式化得到你所需要的Date参数:

    /**
    * 方法 :DateFormat()
    * 通过DateFormat()方法所控制的不同参数来显示当前日期时间
    */
    //简略的将当前日期时间显示出来
    DateFormat shortDateFormat = DateFormat.getDateTimeInstance(
    DateFormat.SHORT, DateFormat.SHORT);
    System.out.println(shortDateFormat.format(date));

    //精确地显示当前日期时间
    DateFormat mediumDateFormat = DateFormat.getDateTimeInstance(
    DateFormat.MEDIUM, DateFormat.MEDIUM);
    System.out.println(mediumDateFormat.format(date));

    //完全的将当前的日期时间显示出来
    DateFormat longDateFormat = DateFormat.getDateTimeInstance(
    DateFormat.LONG, DateFormat.LONG);
    System.out.println(longDateFormat.format(date));

    //全部标准化的将当前日期时间按输出出来。
    DateFormat fullDateFormat = DateFormat.getDateTimeInstance(
    DateFormat.FULL, DateFormat.FULL);
    System.out.println(fullDateFormat.format(date));

 8.同时可以通过getTime()方法获取当前日期的时间:

  不过这样所得到的Date类型数据是以秒来计算的、并且是以1970年1月1日为开始的。在声明数据类型时需要较大存储空间使用Long数据类型或者同等存储类型数据。

    /** 
    * 方法 : getTime() 输出 : 1228494414199 这个时间是按照1970年1月1日开始经历的毫秒数了、 
    */ 
    long str_get = date.getTime(); 

    System.out.println("this is gettime :" + str_get);

String

valueOf() 要把参数中给的值,转化为String类型    

Integer

valueOf(int i) 返回一个表示指定的 int 值的 Integer 实例。
valueOf(String s) 返回保存指定的 String 的值的 Integer 对象。
valueOf(String s, int radix) 返回一个 Integer 对象,该对象中保存了用第二个参数提供的基数进行解析时从指定的                                   String 中提取的值。

intValue()  把Integer类型转化为Int类型。

parseInt(String s) 将字符串参数作为有符号的十进制整数进行解析。
parseInt(String s, int radix) 使用第二个参数指定的基数,将字符串参数解析为有符号的整数。

 

//如何将字串 String 转换成整数 int?
int i = Integer.valueOf(my_str).intValue();
int i=Integer.parseInt(str);

//如何将字串 String 转换成Integer ?
Integer integer=Integer.valueOf(str);

//如何将整数 int 转换成字串 String ?
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = "" + i;

//如何将整数 int 转换成Integer ?
Integer integer=new Integer(i);

//如何将Integer 转换成字串 String ?
Integer integer=String

//如何将Integer 转换成 int ?
int num=Integer.intValue();

//如何将String转换成BigDecimal ?
BigDecimal d_id = new BigDecimal(str);

//日期
Calendar calendar=Calendar.getInstance();
int year=calendar.get(Calendar.YEAR);
int month=calendar.get(Calendar.MONTH)+1;
int day=calendar.get(Calendar.DATE);

//获取今天的日期字符串
String today=java.text.DateFormat.getDateInstance().format(new java.util.Date());

//获取今天的日期
new java.sql.Date(System.currentTimeMillis())


原创粉丝点击