8 kinds of conversion between data types (rpm)

Converting data types between 8

 Copyright: copyright belongs to himself all 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 various conversion {
  9.  
    public static void main(String[] args) throws ParseException {
  10.  
    /*
  11.  
    1. The basic data type conversion *
  12.  
    */
  13.  
    // implicit conversion byte, short, char - int - long - float - double
  14.  
    // cast
  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 and arrays
  28.  
    */
  29.  
    // String to an array
  30.  
    String ss = "abcdefg";
  31.  
    char [] charArray = ss.toCharArray ();
  32.  
    byte[] bytes = ss.getBytes();
  33.  
    // Array to String
  34.  
    String bys = new String(bytes);
  35.  
    String chs = new String(charArray);
  36.  
     
  37.  
    * 4 String and basic data types
  38.  
    // basic data types to String
  39.  
    int an = 10;
  40.  
    String aa = an+ "";
  41.  
    String aa1 = String.valueOf(an);
  42.  
     
  43.  
    // String to basic data types
  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 transfer case
  51.  
    */
  52.  
    String bigSmall = "AbCdEf";
  53.  
    String big = bigSmall.toUpperCase();
  54.  
    String small = bigSmall.toLowerCase();
  55.  
     
  56.  
    /*
  57.  
    6 * Automatic boxing and unboxing
  58.  
    */
  59.  
    I = Integer 123; // autoboxing
  60.  
    II = I int; // automatically unpacking
  61.  
     
  62.  
    /*
  63.  
    * 7 Date and 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 and 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

 

Guess you like

Origin www.cnblogs.com/LiZhongZhongY/p/10991392.html