java基本类型之间的转换

基本类型:

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(生成对象)
  1. int转换为string (double、float、Long的大同小异)
//toString() 是类的串转换的方法
        
//3种
String.valueOf(num2);
Integer.toString(num2);
String string2 = num2 + "";    //字符串拼接
  1. float转换为double
float num3 = 10.00f;
new Float(num3).doubleValue();
  1. double转换为int
double num4 = 10.0;
new Double(num4).intValue();
  1. Date基本显示
Date date = new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd EE hh:mm:ss.S");
sdf.format(date); //完整的时间
  1. 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);
发布了12 篇原创文章 · 获赞 0 · 访问量 116

猜你喜欢

转载自blog.csdn.net/weixin_44304524/article/details/104864585