基本数据类型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类型向基本数据类型转换
- 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型数据间的相互转换
- 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型数据间的相互转换
- 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型数据间的相互转换
- 将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.