基本类型之间的转换

基本类型:

byte、short、int、long、float、double、chat、boolean

1、string转换int (double、float、Long的大同小异)

    String num = "123";
        int num2 = 123;
        
      //3种
Integer.parseInt(num); //返回int(生成整型) Integer.valueOf(num); //返回Integer包装类 Integer.valueOf(num).intValue(); //返回int(生成对象)

2、int转换为string (double、float、Long的大同小异)

//toString() 是类的串转换的方法
        
    //3种 String.valueOf(num2); Integer.toString(num2); String string2
= num2 + ""; //字符串拼接

3、float转换为double

    float num3 = 10.00f;
       new Float(num3).doubleValue();

4、double转换为int

    double num4 = 10.0;
       new Double(num4).intValue();

5、Date基本显示

    Date date = new Date();
       SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd EE hh:mm:ss.S");
       sdf.format(date); //完整的时间

6、string转换date

    String num5 = "2019-06-03";
       DateFormat dateFormat = DateFormat.getDateInstance();//获取日期格式对象方法
       long time = dateFormat.parse(num5).getTime();//获取long类型时间
       Date date2 = new Date(time);
        
       //重新设置输出格式
       String format = DateFormat.getDateTimeInstance().format(date2);

基本上的一些转换,详细的可以看api文档。

猜你喜欢

转载自www.cnblogs.com/huangcan1688/p/12116871.html