java常见数据类型转换总结

 String转int

String str = "11";
int a = Integer.parseInt(str);

int转String

int i = 12;
s=String.valueOf(i);

String转Double

String a = "10";
Double d = Double.parseDouble(a);

Double转String

Double d = 10.0;
String str = d.toString();

String转Float

String str = "1.2";
Float f = Float.valueOf(str);

Float转String

Float f = 1.6f;
String str = f.toString();

String转String[]

String str = "a,b,c";
String [] strarr = str.split(",");

String转Date

String time = "2019-01-01 12:10:12";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);

String转list

String str = "a,b,c";
List list = Arrays.asList(str.split(","));

List转String

List<String> lists = Arrays.asList("1","2","3","4","5");
String str1= String.join("," , lists); 
或者 String str2 = lists.stream().collect(Collectors.joining(","));

List转String[]

List<String> list=new ArrayList<String>();
String[] strings = list.toArray(new String[list.size()]);

String转BigDecimal

String str = "3.30";
BigDecimal bd = new BigDecimal(str);

BigDecimal转String

String str = "3.36";
BigDecimal bd = new BigDecimal(str);
//1:代表小数点后位数,BigDecimal.ROUND_HALF_UP四舍五入
bd = bd.setScale(1,BigDecimal.ROUND_HALF_UP);

String转timestamp

String str = "2019-03-11 02:10:22";
//返回为秒数
Long second = Timestamp.valueOf(str).getTime();

timestamp转String

Long timestamp = 1552241422000L;
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(timestamp);
String str = sf.format(date);

 

 

 

 

持续更新!!!

posted @ 2019-12-28 09:28  夏夜凉凉  阅读(231)  评论(0编辑  收藏  举报