Java类型转换(详细)

(1)数组 =》 字符串:

Arrays.toString(array);

(2)字符串=》字符数组

第一种:String.getChars(srcBegin,srcEnd,destn,destBegin);

第二种:String.toCharArray();

(3)字符=》字符串

char a[]={‘a’,’b’,’c’};

方法一:System.out.println(new String (a));

方法二:String.valueOf(char c);

(4)ArrayList=》字符串

ArrayList al=new ArrayList();

al.toString();

(5)字符串=》字节数组

String str=”李子祥”;

byte b[]=str.getBytes();

(6)整形=》字符串

String.valueOf(int n);

(7)字符串=》整形

Stirng str=”123”;

int a=Integer.parseInt(str);

(8)字符串=》float

String str=”1.3”;

float a=Float .parseFloat(str);

(9)字符串=》日期(java.sql.Date)(只能转换格式为:2017-10-10)

Date birth=Date.valueOf(birthStr);

方法二:

字符串=》日期(java.util.Date)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = sdf.parse(payment_time);

(10)日期格式1=》日期格式2(可以转换任意格式的日期字符串)

simpleDateFormat sdf = new simpleDateFormat(“yyyy-MM-dd”);

String date = sdf.format(日期格式1);

java.util.Date time = java.sql.Date.valueOf(date);

(11)Integer=》Integer数组

        Integer num = 1001;
        // 转换成字符串
        String str = num.toString();
        // 拆分字符串
        // 定义字符串数组引用和整形引用用于存放取出来的数
        String[] str2 = new String[str.length()];
        Integer[] nums = new Integer[str.length()];
        // 取出来
        for (int i = 0; i < str.length(); i++) {
            // 临时变量接收取出来的每一位数,作为字符串
            String temp = str.substring(i, i + 1);
            // 每次取出来的数重新变成整数
            nums[i] = new Integer(temp);
        }

(12)bigdecimal转换成integer

            BigDecimal bd = new BigDecimal(obj);(obj为String类型)
            int intValue = bd.intValue();

(13)double转换int

double d = 10.29;
int temp = new Double(d).intValue();

(14)数组转换List

String[] split = str.split(cutApartSign);
list = Arrays.asList(split);

(15)byte数组转换String

byte[] a = new byte[]{'','',''}
String str = new String(a);
(16)List转换数组 || Object[]转换String[]
ArrayList<String> list = new ArrayList<>();
String[] strings = list.toArray(new String[10]);
 (16)List转换数组 || Object[]转换String[]
 
LocalDateTime l1 = Date.from(Instant.now()).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值