几种常见的数据类型转换

几种常见的数据类型转换,记录一下

        一、Timestap与String  BigDecimal与String

        项目使用的数据库Oracle,字段类型为Date与Number,ORM框架为Mybatis,返回类型和参数类型均为         java.util.Map,此时方法返回的Map {END_DATE=2012-11-11 14:39:35.0, FLAG=0} ,本以为(String)map.get(""),直接转换为String类型,最后报错了,为了保证代码健壮,强制类型转换时可以使用instance of判段类型

    

        Timestap转String

Java代码     收藏代码
  1. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  2. java.sql.Timestamp ts= (java.sql.Timestamp) map.get("END_DATE");  
  3. String endDate=sdf.format(ts);  

 

        String转化为Timestamp

   

Java代码     收藏代码
  1. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  2. String time = sdf.format(new Date());  
  3. Timestamp ts = Timestamp.valueOf(time);  

     

        BigDecimal转String

当valueOf()和toString()返回相同结果时,宁愿使用前者

因为调用null对象的toString()会抛出空指针异常,如果我们能够使用valueOf()获得相同的值,那宁愿使用valueOf(),传递一个null给valueOf()将会返回“null”,尤其是在那些包装类,像Integer、Float、Double和BigDecimal。     

Java代码     收藏代码
  1. java.math.BigDecimal bd = (BigDecimal)m1.get("FLAG");  
  2. String flag = bd.toString();  //  
  3. 如果bd为null抛出 "Exception in thread "main" java.lang.NullPointerException"  
  4.   
  5. String flag = String.valueOf(bd);  

 

        String转BigDecimal

   

Java代码     收藏代码
  1. BigDecimal bd = new BigDecimal("10");  

    

        二、Date与String之间的转换

 

        String转Date    

Java代码     收藏代码
  1. DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  2. Date date = null;String str = null;  
  3. str = "2010-10-10";  
  4. date = format.parse(str); //Sun Oct 10 00:00:00 CST 2010  
  5. date = java.sql.Date.valueOf(str); //返回的是java.sql.Date 2010-10-10  

 

        Date转String

    

Java代码     收藏代码
  1. DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  2. Date date = null;String str = null;  
  3. date = new Date();   
  4. str = format.format(date);   

        省略了异常处理部分

 

        把字符串转化为java.sql.Date

        字符串必须是"yyyy-mm-dd"格式,否则会抛出IllegalArgumentException异常

    java.sql.Date sdt=java.sql.Date.valueOf("2010-10-10");

 

 

三、文件与byte数组的相互转换

 

所有的文件在硬盘或在传输时都是以字节的形式传输的

 

文件转byte[]

 

Java代码     收藏代码
  1. public static void readFile() throws Exception {  
  2.         FileInputStream fis = new FileInputStream("luffy.gif");  
  3.         BufferedInputStream bis = new BufferedInputStream(fis);  
  4.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  5.         int num = bis.read();  
  6.         while (num != -1) {  
  7.             baos.write(num);  
  8.         }  
  9.         bis.close();  
  10.         byte[] array = baos.toByteArray();  
  11.         System.out.println(array.toString());  
  12.           
  13.     }  

 

byte[] 转文件

 

Java代码     收藏代码
  1. public static void writeFile(byte[] array) throws Exception{  
  2.         FileOutputStream fos =new FileOutputStream("one.gif");  
  3.         BufferedOutputStream bos =new BufferedOutputStream(fos);  
  4.         bos.write(array);  
  5.         bos.close();  
  6.         System.out.println("success");  
  7.     }  

 

 

 byte与16进制字符串转换

Java代码     收藏代码
  1. public static String byte2hex(byte[] b) {  
  2.        String hs = "";    
  3.        String stmp = "";    
  4.        for (int n = 0; n < b.length; n++) {    
  5.         stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));    
  6.         if (stmp.length() == 1)    
  7.          hs = hs + "0" + stmp;    
  8.         else    
  9.          hs = hs + stmp;    
  10.        }    
  11.        return hs;    
  12.     }    
  13.     
  14.     public static byte[] hex2byte(String str) {    
  15.        if (str == null)    
  16.         return null;    
  17.        str = str.trim();    
  18.        int len = str.length();    
  19.        if (len == 0 || len % 2 == 1)    
  20.         return null;    
  21.         
  22.        byte[] b = new byte[len / 2];    
  23.        try {    
  24.         for (int i = 0; i < str.length(); i += 2) {    
  25.          b[i / 2] = (byte) Integer    
  26.            .decode("0x" + str.substring(i, i + 2)).intValue();    
  27.         }    
  28.         return b;    
  29.        } catch (Exception e) {    
  30.         return null;    
  31.        }    
  32.     }    

你可能感兴趣的:(几种常见的数据类型转换)