1.BigDecimal 转 Double
BigDecimal b ;
String op =b.toString();(可能会抛出空指针异常,建议用valueOf,即 String.valueOf(b) )
double d = Double.parseDouble(op);
2.java.util.Date 转 String
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 ");
//设置自己想要的时间格式
String aa=sdf.format(new Date());
System.out.println(aa);
3.String 转 Date
String string = "2016-10-24 21:59:06";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.parse(string));
举个栗子:
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
4文件转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());
}
5btye数组转文件
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");
}
6.int和String互转
一、String转为int
int i=Integer.parseInt(string);
int i=Integer.valueOf(s).intValue();
二、int转为String
String s = String.valueOf(i);
String s = Integer.toString(i);
String s = “” + i;
7.double和String互转
double转String
先转为int类型再转String
Double num;
int i = Math.round(num);//注意:4舍5入
String s = String.valueOf(i);
String转Double
Double.parseDouble(String)
持续更新