• 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);

    持续更新!!!

  • 相关阅读:
    二、云计算openstack共享组件--时间同步服务ntp
    一、云计算openstack介绍
    五、Kvm虚拟机迁移
    四、Kvm虚拟化网络管理
    三、Kvm虚拟化存储管理
    二、kvm虚拟机管理
    一、kvm虚拟化介绍
    九、Linux网络技术管理及进程管理
    园主的码云网站,可以在里面查看园主的练习代码哦
    万能Makefile,前戏做足项目做起来才顺畅。
  • 原文地址:https://www.cnblogs.com/flyinghome/p/12100772.html
Copyright © 2020-2023  润新知