基本数据类型的包装类
package testwarppedclass;
import javax.xml.bind.ParseConversionEvent;
//包装类,以Integer类为例
public class test01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer i=new Integer(1000);
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.toHexString(i));//转为16进制
Integer i2=Integer.parseInt("4002");//将纯数字的字符串当为int类型
System.out.println(i2);
Integer i3=new Integer("333");
System.out.println(i3.intValue());//把Integer对象类型变成int的基础数据类型;
}
}
自动装箱和拆箱
package testwarppedclass;
//测试包装类的基本用法,自动装箱和拆箱
public class test02 {
public static void main(String[] args) {
//Integer a=new Integer(100);
Integer a=1000;
//自动装箱,编译器帮助改进代码: Integer a=new Integer(100);
int b=new Integer(300);
//自动拆箱,改进:new Integer(300).intValue();(用法见上图)
Integer a1=1000;
System.out.println(a==a1);
//按照装箱的理解会创建2个对象,地址当然不同
System.out.println(a.equals(a1));
System.out.println("**************");
Integer c=100;
Integer c1=100;
System.out.println(c==c1);
//结果与a==a1时候相反,[-128,127]之间的数字都按基本数据类型处理,来提高效率
System.out.println(c.equals(c1));
}
}
时间类
package testDate;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.crypto.Data;
public class TestDateFormat {
public static void main(String[] args) throws ParseException {
DateFormat df=new SimpleDateFormat("YYYY年MM月DD日 hh:mm:ss,属于本年的第w周");
Date d=new Date(24*3600*1000L);
String string=df.format(d);//Formats a Date into a date/time string.
System.out.println(string);
System.out.println("**********");
String string2="1996-10-03";
DateFormat df2=new SimpleDateFormat("YYYY-MM-DD");
Date d2=df2.parse(string2);
// Parses text from the beginning of the given string to produce a date.//
System.out.println(string2);
}
}
Calendar类
package testDate;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Year;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class TestCalender {
public static void main(String[] args) {
Calendar c=new GregorianCalendar();
c.set(1996, 10,03);
System.out.println(c);
Date d =c.getTime();
// Returns a Date object representing this Calendar's time value
System.out.println(d);
System.out.println(c.get(Calendar.YEAR));
c.add(Calendar.YEAR, 30);
System.out.println(c);
}
}