大家好,又见面了,我是你们的朋友全栈君。
import java.text.MessageFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author 课时二 :类型转换
*
*/
public class TypeConvert {
public static void main(String[] args) {
/**
* 1. 隐式转换:小类型转大类型自动转换 2.强制转换:大类型转小类型强制转换
*/
Byte bt = 1;
int num = 100;
// 隐式转换
short stNumBt = bt;
// int to short强制转换
short stNum = (short) num;
// String转换成Object
StringToObject();
System.out.println();
System.out.println("********************************************************分界线******************************************************************");
System.out.println();
//Object转换成String
ObjectToString();
}
/**
* String转换成Object
*/
public static void StringToObject() {
String intStr = "10000";
String boolStr = "true";
String doubleStr = "20.88";
String dateStr = "2019-1-1 10:10:10";
// String to int 方法一
int IntNum = Integer.parseInt(intStr);
System.out.printf("String to int:%d", IntNum);
System.out.println();
// String to int 方法二
int IntNum2 = Integer.valueOf(intStr);
System.out.printf("String to int: %d ", IntNum2);
System.out.println();
// String to boolean 方法一
boolean bl = Boolean.parseBoolean(boolStr);
System.out.printf("String to boolean: %b ", bl);
System.out.println();
// String to boolean 方法二
boolean bl2 = Boolean.valueOf(boolStr);
System.out.printf("String to boolean: %b ", bl2);
System.out.println();
// String to Date
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = simpleDate.parse(dateStr, new ParsePosition(0));
System.out.printf("String to Date: %tF %tT", dt, dt);
System.out.println();
System.out.println(dt);
// String to double
double db = Double.valueOf(doubleStr);
System.out.printf(" String to double: %.2f", db);
System.out.println();
}
/**
* Object转换成String
*/
public static void ObjectToString() {
int num = 1;
boolean isExists = true;
double db = 20.88;
Date dt = new Date();
// Int to String方法一
String numString = Integer.toString(num);
System.out.printf("Int to String:%s", numString);
System.out.println();
// Int to String方法二
String numString2 = String.valueOf(num);
System.out.printf("Int to String2:%s", numString2);
System.out.println();
// boolean to String方法一
String boolString = Boolean.toString(isExists);
System.out.printf("boolean to String:%s", boolString);
System.out.println();
// boolean to String方法二
String boolString2 = String.valueOf(isExists);
System.out.printf("boolean to String2:%s", boolString2);
System.out.println();
// double to String方法一
String doubleString =Double.toString(db);
System.out.printf("double to String:%s", doubleString);
System.out.println();
// double to String方法二
String doubleString2 = String.valueOf(db);
System.out.printf("double to String2:%s", doubleString2);
System.out.println();
// Date to String 方法一
String dtString = String.valueOf(dt);
System.out.printf("Date to String:%s", dtString);
System.out.println();
SimpleDateFormat sdDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dtString2=sdDateFormat.format(dt);
System.out.printf(MessageFormat.format("Date to String:{0}", dtString2));
System.out.println();
}
}
总结:
String 转换 Object : 使用类似 Obj.parseObj(String) , Obj.valueOf(String)
Object转换 String: 使用类似 Obj.toString(Obj), String.valueOf(Obj)
Date类型的转换使用SimpleDateFormat
如果是 JDK8 的应用,可以使用 Instant 代替 Date,LocalDateTime 代替 Calendar, DateTimeFormatter 代替 SimpleDateFormat,
执行效果:
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/131921.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...