Java-类型转换,String转Object和Object转String
MessageFormat;
ParsePosition;
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);
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 = String(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 = String(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 =String(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();
System.out.println();
}
}object to
总结:
String 转换 Object :  使⽤类似  Obj.parseObj(String) , Obj.valueOf(String)
Object转换 String:    使⽤类似  String(Obj), String.valueOf(Obj)
Date类型的转换使⽤SimpleDateFormat
如果是 JDK8 的应⽤,可以使⽤ Instant 代替 Date,LocalDateTime 代替 Calendar, DateTimeFormatter 代替SimpleDateFormat,
执⾏效果:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。