package application;
import com.cgp.common.utils.StringUtils;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
/**
* 类型转换
*/
public class TypeConversion {
@Test
public void StringTest() throws UnsupportedEncodingException {
// 1.将字符串转换为字符数组
String str = "Manaphy";
char[] charArray = str.toCharArray();
System.out.println(Arrays.toString(charArray)); // [M, a, n, a, p, h, y]
// 2.将字符数组转换为字符串
str = new String(charArray);
System.out.println(str); // Manaphy
// 3.将字符串转换为byte数组
byte[] bytes = str.getBytes(); // 以默认编码转换(UTF-8)
byte[] gbkBytes = str.getBytes("GBK"); // 以GBK编码转换 (抛UnsupportedEncodingException异常)
System.out.println(Arrays.toString(bytes)); // [77, 97, 110, 97, 112, 104, 121]
// 4.将byte数组转换为字符串
str = new String(bytes);
System.out.println(str); // Manaphy
// 5.把其他类型数据转换为字符串
int i = 123;
String txt = "" + i; // 字符串与基本类型数据连接
txt = String.valueOf(i); // 转换基本数据类型
// 6.String、int、Integer的互相转换
// int 转 Integer
Integer integer1 = new Integer(10);
Integer integer2 = Integer.valueOf(10); // 官方推荐这种写法
Integer integer3 = 10; // 自动装箱
// String 转 Integer
Integer integer4 = Integer.valueOf("10");
// Integer 转 int
int int1 = integer1.intValue();
int int2 = integer3; // 自动拆箱
// int 转 String
String str2 = String.valueOf(int1);
String str3 = Integer.toString(int2);
// String 转 int
int int3 = Integer.parseInt(str2);
// Integer 转 String
String str4 = integer1.toString();
}
/**
* 7.BigDecimal 与各种基本类型的互相转换
*/
@Test
public void BidDecimalTest() {
// String 转 BigDecimal
BigDecimal bd = new BigDecimal("12.34");
// int 转 BigDecimal
BigDecimal intBd = new BigDecimal(10);
// long 转 BigDecimal
BigDecimal longBd = new BigDecimal(12L);
// double 转 BigDecimal (不建议直接转,先转换为String类型再转为 BigDecimal)
BigDecimal doubleDb = new BigDecimal(12.34 + "");
// BigDecimal 转换为基本类型和 String
String str = bd.toString();
int intValue = intBd.intValue();
long longValue = longBd.longValue();
double doubleValue = doubleDb.doubleValue();
}
/**
* 8.List,Set,数组之间的转换
*/
@Test
public void ArrayTest() {
String[] arr = new String[]{"A", "B", "C"};
// 数组 转 List
List<String> list = Arrays.asList(arr); // 长度固定不变 [list.add("D") 会报错]
List<String> list1 = new ArrayList<>(Arrays.asList(arr)); // 长度可变 [可add()]
// List 转 Set
Set<String> set = new HashSet<>(list);
// Set 转 List
List<String> list2 = new ArrayList<>(set);
// 数组 转 Set-->利用数组转List再转Set
Set<String> set1 = new HashSet<>(Arrays.asList(arr));
// List 转 数组
Object[] array = list.toArray();
// Set 转 数组
Object[] arraySet = set.toArray();
// 数组转字符串 org.apache.commons.lang3.StringUtils
String join = StringUtils.join(arr, ","); // A,B,C
}
/**
* 日期相关的互相转换
*/
@Test
public void DateTest() throws ParseException {
// 9.String 与 Date 之间的互相转换
String strDate = "2019-04-13";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 将 String 转化为 Date
Date date = sdf.parse(strDate);
// 将 Date 转换为 String
String format = sdf.format(date);
// 10.Date与LocalDateTime(JDK8新的时间类)的转换 (需要使用Instant中转)
Date now = new Date();
// Date 转 Instant
Instant instant = Instant.ofEpochMilli(now.getTime());
Instant instant1 = now.toInstant();
// Instant 转 Date
Date date1 = Date.from(instant);
// Instant 转 LocalDateTime
LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
// LocalDateTime 转 Instant
Instant instant2 = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
}
}
Java各种类型之间的互相转换
最新推荐文章于 2023-11-12 22:50:48 发布