zoukankan      html  css  js  c++  java
  • 星空雅梦

    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
  • 相关阅读:
    Pytest权威教程21-API参考-02-标记(Marks)
    GitLab获取人员参与项目-贡献项目列表
    通过Confulence API统计用户文档贡献量
    Git项目代码统计-Python3版gitstats
    Pytest从测试类外为测试用例动态注入数据
    Python操作Jira
    Selenium操作Chrome模拟手机浏览器
    剑指offer 构建乘积数组
    栈与堆 && char*, char[], char**, char*[], char[][]详解
    vector 容器知识点汇总
  • 原文地址:https://www.cnblogs.com/LiZhongZhongY/p/10991392.html
Copyright © 2011-2022 走看看