Java资深小白,不足之处,或者有任何错误欢迎指出。 --蓝紫
目录
String与Integer、int相互转换
- String转Integer、int
String count = "12345";
int parseInt = Integer.parseInt(count);
Integer parseInteger = Integer.valueOf(count);
- Integer、int转String
// 方式1
int total = 12345;
String totalString = String.valueOf(total);
// 方式2
Integer sum = 12345;
String sumString = sum + "";
String与List相互转换
- String转List
// String转List,使用英文逗号拆分
String abc = "a,b,c";
//将中文逗号替换成英文逗号
String replace = abc.replace(",", ",");
List<String> abcList = Arrays.asList(replace.split(","));
- List转String
// List转String,英文逗号间隔
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
String abcString = StringUtils.join(list, ",");
这里的StringUtils引入的是lang3的包:org.apache.commons.lang3.StringUtils,jar下载地址:commons-lang3-3.10
String与BigDecimal相互转换
- String转BigDecimal
String average = "3.1415926";
BigDecimal ave = new BigDecimal(average);
// 四舍五入,保留两位小数
BigDecimal round = ave.setScale(2, BigDecimal.ROUND_HALF_UP);
- BigDecimal转String
BigDecimal age = new BigDecimal(18);
String ageString = age.toString();
String与Long、long相互转换
- String转Long、long
String time = "20220506104523";
//方式1
Long timeLong = Long.valueOf(time);
//方式2
long timelong = Long.parseLong(time);
- Long、long转String
long random = new Random().nextLong();
//方式1
String randomString = Long.toString(random);
//方式2
String r = random + "";
//方式3
Long number = new Long(random);
String numberString = String.valueOf(number);
String与Double、double相互转换
- String转Double、double
String code = "10.24";
//方式1
Double codeDouble = new Double(code);
//方式2
Double codeDouble2 = Double.valueOf(code);
//方式3
double codedouble = Double.parseDouble(code);
- Double、double转String
double dou = 20.220505;
//方式1
String douString = String.valueOf(dou);
//方式2
String ble = dou + "";
//方式3
Double day = new Double(dou);
String d = day.toString();
//方式4,返回格式化的字符串,保留2位小数
String dayString = String.format("%.2f", day);
String与Float、float相互转换
- String转Float、float
String birth = "10.16";
//方式1
Float birthFloat = new Float(birth);
//方式2
Float f = Float.valueOf(birth);
//方式3
float birthfloat = Float.parseFloat(birth);
- Float、float转String
float flo = 3.1415F;
//方式1
String floString = String.valueOf(flo);
//方式2
String floa = flo + "";
//方式3
Float Flo = new Float(flo);
String FloString = Flo.toString();
//方式4,返回格式化的字符串,保留2位小数
String FloatString = String.format("%.2f", Flo);
String与Date相互转换
- String转Date
String today = "2022-05-06";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(today);
- Date转String
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
Date times = new Date();
String newTime = simpleDateFormat.format(times);
测试代码:
public class DataTypeTransfer {
public static void main(String[] args) {
// String转int
String count = "12345";
int parseInt = Integer.parseInt(count);
System.out.println("String转Int:" + parseInt);
// String转Integer
Integer parseInteger = Integer.valueOf(count);
System.out.println("String转Integer:" + parseInteger);
// int转String
int total = 12345;
String totalString = String.valueOf(total);
System.out.println("int转String:" + totalString);
// Integer转String
Integer sum = 12345;
String sumString = sum + "";
System.out.println("Integer转String:" + sumString);
// List转String,英文逗号间隔
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
String abcString = StringUtils.join(list, ",");
System.out.println("List转String:" + abcString);
// String转List,英文逗号拆分
String abc = "a,b,c";
String replace = abc.replace(",", ",");
List<String> abcList = Arrays.asList(replace.split(","));
System.out.println("String转List:" + abcList);
// String转BigDecimal
String average = "3.1415926";
BigDecimal ave = new BigDecimal(average);
System.out.println("String转BigDecimal:" + ave);
// 四舍五入,保留两位小数
BigDecimal round = ave.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println(ave + " 四舍五入后的值:" + round);
// BigDecimal转String
BigDecimal age = new BigDecimal(18);
String ageString = age.toString();
System.out.println("BigDecimal转String:" + ageString);
// String转Long、long
String time = "20220506104523";
Long timeLong = Long.valueOf(time);
System.out.println("String转Long:" + timeLong);
long timelong = Long.parseLong(time);
System.out.println("String转long:" + timelong);
// Long、long转String
long random = new Random().nextLong();
String randomString = Long.toString(random);
String r = random + "";
System.out.println("long转String:" + randomString);
System.out.println("long转String:" + r);
Long number = new Long(random);
String numberString = String.valueOf(number);
System.out.println("Long转String:" + numberString);
// String转Double、double
String code = "10.24";
Double codeDouble = new Double(code);
Double codeDouble2 = Double.valueOf(code);
System.out.println("String转Double:" + codeDouble);
System.out.println("String转Double:" + codeDouble2);
double codedouble = Double.parseDouble(code);
System.out.println("String转double:" + codedouble);
// Double、double转String
double dou = 20.220505;
String douString = String.valueOf(dou);
System.out.println("double转String:" + douString);
String ble = dou + "";
System.out.println("double转String:" + ble);
Double day = new Double(dou);
String d = day.toString();
System.out.println("Double转String:" + d);
// 返回格式化的字符串,保留2位小数
String dayString = String.format("%.2f", day);
System.out.println("Double转String:" + dayString);
// String转Float、float
String birth = "10.16";
Float birthFloat = new Float(birth);
System.out.println("String转Float:" + birthFloat);
Float f = Float.valueOf(birth);
System.out.println("String转Float:" + f);
float birthfloat = Float.parseFloat(birth);
System.out.println("String转float:" + birthfloat);
// Float、float转String
float flo = 3.1415F;
String floString = String.valueOf(flo);
System.out.println("float转String:" + floString);
String floa = flo + "";
System.out.println("float转String:" + floa);
Float Flo = new Float(flo);
String FloString = Flo.toString();
System.out.println("Float转String:" + FloString);
String FloatString = String.format("%.2f", Flo);
System.out.println("Float转String:" + FloatString);
// String转Date
String today = "2022-05-06";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = dateFormat.parse(today);
System.out.println("String转Date:" + date);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
Date times = new Date();
String newTime = simpleDateFormat.format(times);
System.out.println("Date转String:" + newTime);
}
}