几种常见的数据类型转换,记录一下 javascript
1、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判段类型 java
Timestap转String sql
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- java.sql.Timestamp ts= (java.sql.Timestamp) map.get("END_DATE");
- String endDate=sdf.format(ts);
String转化为Timestamp 数据库
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String time = sdf.format(new Date());
- Timestamp ts = Timestamp.valueOf(time);
BigDecimal转String 数组
当valueOf()和toString()返回相同结果时,宁愿使用前者 app
由于调用null对象的toString()会抛出空指针异常,若是咱们可以使用valueOf()得到相同的值,那宁愿使用valueOf(),传递一个null给valueOf()将会返回“null”,尤为是在那些包装类,像Integer、Float、Double和BigDecimal。 框架
- java.math.BigDecimal bd = (BigDecimal)m1.get("FLAG");
- String flag = bd.toString(); //
- 若是bd为null抛出 "Exception in thread "main" java.lang.NullPointerException"
-
- String flag = String.valueOf(bd);
String转BigDecimal spa
- BigDecimal bd = new BigDecimal("10");
2、Date与String之间的转换
String转Date 指针
- DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- Date date = null;String str = null;
- str = "2010-10-10";
- date = format.parse(str); //Sun Oct 10 00:00:00 CST 2010
- date = java.sql.Date.valueOf(str); //返回的是java.sql.Date 2010-10-10
Date转String code
- DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- Date date = null;String str = null;
- date = new Date();
- str = format.format(date);
省略了异常处理部分
把字符串转化为java.sql.Date
字符串必须是"yyyy-mm-dd"格式,不然会抛出IllegalArgumentException异常
java.sql.Date sdt=java.sql.Date.valueOf("2010-10-10");
3、文件与byte数组的相互转换
全部的文件在硬盘或在传输时都是以字节的形式传输的
文件转byte[]
- public static void readFile() throws Exception {
- FileInputStream fis = new FileInputStream("luffy.gif");
- BufferedInputStream bis = new BufferedInputStream(fis);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int num = bis.read();
- while (num != -1) {
- baos.write(num);
- }
- bis.close();
- byte[] array = baos.toByteArray();
- System.out.println(array.toString());
-
- }
byte[] 转文件
- public static void writeFile(byte[] array) throws Exception{
- FileOutputStream fos =new FileOutputStream("one.gif");
- BufferedOutputStream bos =new BufferedOutputStream(fos);
- bos.write(array);
- bos.close();
- System.out.println("success");
- }
byte与16进制字符串转换
- public static String byte2hex(byte[] b) {
- String hs = "";
- String stmp = "";
- for (int n = 0; n < b.length; n++) {
- stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
- if (stmp.length() == 1)
- hs = hs + "0" + stmp;
- else
- hs = hs + stmp;
- }
- return hs;
- }
-
- public static byte[] hex2byte(String str) {
- if (str == null)
- return null;
- str = str.trim();
- int len = str.length();
- if (len == 0 || len % 2 == 1)
- return null;
-
- byte[] b = new byte[len / 2];
- try {
- for (int i = 0; i < str.length(); i += 2) {
- b[i / 2] = (byte) Integer
- .decode("0x" + str.substring(i, i + 2)).intValue();
- }
- return b;
- } catch (Exception e) {
- return null;
- }
- }