java常用数据类型转换

基本数据类型String转换

1.使用”+”完成.任何基本数据类型遇到String类型都会自动向String类型转换.代码示例如下:

public class TestDemo
{
    public static void main(String [] args)
    {
        int a = 100;
        String str = a + "";
        System.out.println(str.length());
    }
}

程序运行结果:
3


2.使用String类中的vauleOf()方法实现转换,代码示例如下:

public class TestDemo
{
    public static void main(String [] args)
    {
        int a = 100;
        String str = String.vauleOf(a);
        System.out.println(str.length);
    }
}

String类型向基本数据类型转换

  1. String型向int型转换
    • 使用Integer类的parseInt()方法完成,代码实例如下:
public class TestDemo
{
    public static void main(String [] args)
    {
        String str = "10";
        int a = Integer.parseInt(str);
        System.out.println(a * a);
    }
}

程序运行结果:
100


2.String类型向double类型转换
- 使用Double类的parseDouble()方法完成,代码如下:

public class TestDemo
{
    public static void main(String [] args)
    {
        String str = "10.0";
        double a = Double.parseDouble(str);
        System.out.println(a * a);
    }
}

程序运行结果:
100.0


3.String类型向boolean型数据转换
- 使用Boolean类的parseBoolean()方法完成,示例代码如下:

public class TestDemo
{
    public static void main(String [] args)
    {
        String str = "true";
        boolean b = Boolean.parseBoolean(str);
        if(b == true)
        {
            System.out.println(b);
        }
        else
        {
            System.out.println("转换失败.");
        }
    }
}

程序运行结果:
true


4.String型数据向char型转换
- 使用String类的charAt()函数完成,代码示例如下:

public class TestDemo
{
    public static void main(String [] args)
    {
        String str = "Hello";
        char a = str.charAt(3);
        System.out.println(a);
    }
}

程序运行结果:
l


long型数据和Date型数据间的相互转换

  1. long型数据转换为Date型数据
    • 使用Date类的构造方法Data(long time)完成。代码示例:
import java.util.Date;
public class TestDemo
{
    public static void main(String [] args)
    {
        // 获得系统当前时间(毫秒)
        long num = System.currentTimeMillis();
        // 将long型数据转换为Date型数据
        Date date = new Date(num);
        System.out.println(date);
    }
}

程序运行结果:
Thu Feb 09 23:23:14 CST 2017


2.Date型数据向long型数据转换
- 使用Date类的getTime()方法完成。代码示例如下:

import java.util.Date;
public class TestDemo
{
    public static void main(String [] args)
    {
        Date date = new Date();
        long num = date.getTime();
        System.out.println(num);
    }
}

程序运行结果:
1486653917272


String型数据和Date型数据间的相互转换

  1. Date型日期数据转换称字符串
    • 实例化Date类对象
    • 传入时间标记字符串实例化SimpleDateFormat类对象
    • 调用SimpleDateFormat类的format()方法完成转换。代码示例如下:
import java.util.Date;
import java.text.SimpleDateFormat;
public class TestDemo
{
    public static void main(String [] args)
    {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String time = sdf.format(date);
        System.out.println(time);
    }
}

程序运行结果:
2017年02月09日 23:26:37


2.String型数据转换称Date型数据
- 传入时间标记字符串实例化SimpleDateFormat类对象
- 调用SimpleDateFormat类的parse()方法将时间字符串转为Date数据,代码示例如下:

import java.util.Date;
import java.text.SimpleDateFormat;
public class TestDemo
{
    public static void main(String [] args)
    {
        String str = "2017年2月8日 00:08:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date date = null;
        try
        {
            date = sdf.parse(str);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        System.out.println(date);
    }
}

程序运行结果:
Wed Feb 08 00:08:00 CST 2017


String型数据和StringBuffer型数据间的相互转换

  1. 将String型数据转换成StringBuffer型数据
    • 使用StringBuffer类的构造方法:public StringBuffer(String str)完成。代码示例如下:
public class TestDemo
{
    public static void main(String [] args)
    {
        String str  = "Hello";
        StringBuffer buf = new StringBuffer(str);
        buf.append(" World.");
        System.out.println(buf);
    }
}

程序运行结果:
Hello World.


  • 使用StringBuffer类的append()方法实现。示例代码如下:
public class TestDemo
{
    public static void main(String [] args)
    {
        String str = "Hello World.";
        StringBuffer buf = new StringBuffer();
        buf.append(str);
        System.out.println(buf);
    }
}

程序运行结果:
Hello World.


2.将StringBuffer型数据转换成String型数据

  • 使用StringBuffer类的toString()方法完成,代码示例如下:
public class TestDemo
{
    public static void main(String [] args)
    {
        StringBuffer buf = new StringBuffer("Hello World.");
        String str = buf.toString();
        System.out.println(str);
    }
}

程序运行结果:
Hello World.


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值