在工做中常常会遇到须要将数据类型转化的状况,今天抽出时间总结一下。spa
date——stringcode
Date date = new Date(); DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String StringDate = String.valueOf(dateformat.format(date));
string——dateorm
String stringDate= "2018-02-01 11:11:11"; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); try{ Date date = dateFormat.parse(stringDate); System.out.println(date); } catch(ParseException e) { e.printStackTrace(); }
int——doubleblog
int intNum = 12345; String stringNum = String.valueOf(intNum); double doubleNum1 = Double.parseDouble(stringNum);//Double.parseDouble返回的是基本数据类型double DecimalFormat df = new DecimalFormat("#.00"); String dfString = df.format(doubleNum1); System.out.println(dfString);//在jdk1.5以后的能够自由相加。
double——intci
double doubleNum = 1.545; DecimalFormat df = new DecimalFormat("0"); int intNum = Integer.parseInt(df.format(doubleNum));//这是四舍五入,非四舍五入直接强转。