8种数据类型之间的转换(转)

8种数据类型之间的转换

 版权声明:所属版权归本人所有 https://blog.csdn.net/weixin_42295141/article/details/89879196
  1.  
    package com.itheima;
  2.  
     
  3.  
    import java.text.ParseException;
  4.  
    import java.text.SimpleDateFormat;
  5.  
    import java.util.Calendar;
  6.  
    import java.util.Date;
  7.  
     
  8.  
    public class 各种转换 {
  9.  
    public static void main(String[] args) throws ParseException {
  10.  
    /*
  11.  
    * 1.基本数据类型转换
  12.  
    */
  13.  
    //隐式转换 byte,short,char -- int -- long -- float -- double
  14.  
    //强制转换
  15.  
    int a = 12;
  16.  
    byte b = (byte) a;
  17.  
     
  18.  
    /*
  19.  
    * 2.String StringBuilder
  20.  
    */
  21.  
    //String to StringBuilder
  22.  
    StringBuilder sb = new StringBuilder("abcde");
  23.  
    //StringBuilder to String
  24.  
    String s = sb.toString();
  25.  
     
  26.  
    /*
  27.  
    * 3.String 和 数组
  28.  
    */
  29.  
    //String to 数组
  30.  
    String ss = "abcdefg";
  31.  
    char[] charArray = ss.toCharArray();
  32.  
    byte[] bytes = ss.getBytes();
  33.  
    //数组 to String
  34.  
    String bys = new String(bytes);
  35.  
    String chs = new String(charArray);
  36.  
     
  37.  
    * 4 String 和 基本数据类型
  38.  
    //基本数据类型 to String
  39.  
    int an = 10;
  40.  
    String aa = an+ "";
  41.  
    String aa1 = String.valueOf(an);
  42.  
     
  43.  
    //String to 基本数据类型
  44.  
    int bb = Integer.parseInt("123");
  45.  
    //String to int
  46.  
    char charAt = "123".charAt(0);
  47.  
    //String to char
  48.  
     
  49.  
    /*
  50.  
    *5 String 大小写转
  51.  
    */
  52.  
    String bigSmall = "AbCdEf";
  53.  
    String big = bigSmall.toUpperCase();
  54.  
    String small = bigSmall.toLowerCase();
  55.  
     
  56.  
    /*
  57.  
    *6 自动装箱和拆箱
  58.  
    */
  59.  
    Integer i = 123;//自动装箱
  60.  
    int ii = i; //自动拆箱
  61.  
     
  62.  
    /*
  63.  
    *7 Date 和 String
  64.  
     
  65.  
    Date d = new Date();
  66.  
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  67.  
    String format = sdf.format(d);
  68.  
    //Date to String
  69.  
    Date parse = sdf.parse(format);
  70.  
    //String to Date
  71.  
     
  72.  
    * 8 Date 和 Calendar
  73.  
     
  74.  
    Date date = new Date();
  75.  
    Calendar cal = Calendar.getInstance();
  76.  
    Date time = cal.getTime();
  77.  
    //Calendar to Date
  78.  
    cal.setTime(date);
  79.  
    //Date to Calendar

 

转载于:https://www.cnblogs.com/LiZhongZhongY/p/10991392.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值