java基础-数据类型转换

String int

 

String str = "123";
int n = 0;
// 第一种转换方法:Integer.parseInt(str);
n = Integer.parseInt(str);
// 第二种转换方法:Integer.valueOf(str).intValue();效率比第一种高,推荐
n = Integer.valueOf(str).intValue();

 

int 转 String

int   num = 10;
// 第一种方法:String.valueOf(i);速度第二,i可以是null
String str = String.valueOf(num);
// 第二种方法:Integer.toString(i);速度最快,推荐,不过i不能是null
String str2 = Integer.toString(num);
// 第三种方法:"" + i;速度最慢
String str3 = num + "";

Json字符串 转 List

import com.alibaba.fastjson.JSONObject;
//Json字符串转list
String listStr= "json字符串";
List<实体类> redisList = JSONArray.parseArray(listStr, 实体类.class);

List 转 Json字符串

import com.alibaba.fastjson.JSONObject;
List<实体类> List = new ArrayList<实体类>();
String jsonstr = JSONObject.toJSONString(List)

 

String 转 Date

//String转Date
Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2020-11-01 08:30:20");

 

Date 转 String

//Date转String
Date date2 = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = sdf.format(date2);

 

posted @ 2022-09-02 15:18  Yesong_Li  阅读(23)  评论(0)    收藏  举报