Converting data types between 8
Copyright: copyright belongs to himself all https://blog.csdn.net/weixin_42295141/article/details/89879196
-
package com.itheima;
-
-
import java.text.ParseException;
-
import java.text.SimpleDateFormat;
-
import java.util.Calendar;
-
import java.util.Date;
-
-
public class various conversion {
-
public static void main(String[] args) throws ParseException {
-
/*
-
1. The basic data type conversion *
-
*/
-
// implicit conversion byte, short, char - int - long - float - double
-
// cast
-
int a = 12;
-
byte b = (byte) a;
-
-
/*
-
* 2.String StringBuilder
-
*/
-
//String to StringBuilder
-
StringBuilder sb = new StringBuilder("abcde");
-
//StringBuilder to String
-
String s = sb.toString();
-
-
/*
-
* 3.String and arrays
-
*/
-
// String to an array
-
String ss = "abcdefg";
-
char [] charArray = ss.toCharArray ();
-
byte[] bytes = ss.getBytes();
-
// Array to String
-
String bys = new String(bytes);
-
String chs = new String(charArray);
-
-
* 4 String and basic data types
-
// basic data types to String
-
int an = 10;
-
String aa = an+ "";
-
String aa1 = String.valueOf(an);
-
-
// String to basic data types
-
int bb = Integer.parseInt("123");
-
//String to int
-
char charAt = "123".charAt(0);
-
//String to char
-
-
/*
-
* 5 String transfer case
-
*/
-
String bigSmall = "AbCdEf";
-
String big = bigSmall.toUpperCase();
-
String small = bigSmall.toLowerCase();
-
-
/*
-
6 * Automatic boxing and unboxing
-
*/
-
I = Integer 123; // autoboxing
-
II = I int; // automatically unpacking
-
-
/*
-
* 7 Date and String
-
-
Date d = new Date();
-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
String format = sdf.format(d);
-
//Date to String
-
Date parse = sdf.parse(format);
-
//String to Date
-
-
* 8 Date and Calendar
-
-
Date date = new Date();
-
Calendar cal = Calendar.getInstance();
-
Date time = cal.getTime();
-
//Calendar to Date
-
cal.setTime(date);
-
//Date to Calendar