基本类型包装类
运用APL来解决问题
1.需求:int类型如何变成String类型?
String类型如何变成int类型?
public static String valueOf(int i):将int参数转换为String类型
public static int parselnt(String s):将字符串解析为int类型,该方法是Integer类中的方法
2.需求:如何判断一个数据是否在int范围内?
int范围:-2的31次方至2的31次方-1
在Integer类下有这两个常用的方法
public static final int MIN_VALUE:int的最小值
public static final int MAX_VALUE:int的最大值
只要判断这个数字在不在int范围内即可
3.需求:如何获取Integer对象?
构造方法:但是这两种不常用
public Integer(int value):根据int值创建Integer对象
public Integer(String s):根据String值创建Integer对象
静态方法获取对象:常用
public static Integer valueOf(int i):返回表示指定的int值的Integer实例
public static Integer valueOf(String s):返回一个保存指定值的Integer对象String
4.需求:通过Arrays类提供的方法,打印并且冒泡排序
public static String toString(int[]arr):返回指定数组的内容的字符串表示形式
public static void sort(int[]arr):按照数字顺序排列指定的数组
5.Math类的常用方法
这八个方法中全部都是static修饰,直接类名.方法名即可
1.public static int abs(int a):返回参数的绝对值
2.public static double ceil(double a):返回大于或等于参数的最小double值,去掉小数点后等于一个整数
3.public static double floor(double a):返回小于或等于参数的最大double值,去掉小数点后等于一个整数
4.public static int round(float a):按照四舍五入返回最接近参数的int
5.public static int max(int a,int b):返回两个int值中的最大值
6.public static int min(int a,int b):返回两个int值中的最小值
7.public static double pow(double a,double b):返回a的b次幂的值4
8.public static double random():随机返回一个为double的正值区间为:[0.0,1.0)
6.System类的常用方法
public static void exit(int status):终止当前运行的java虚拟机,非零表示异常终止
public static long currentTimeMillis():返回1970年1月1日当前时间(以毫秒为单位)
7.可以在学生类中重写toString方法
这样子输出对象名时就不会输出一大堆的地址值了
8.equals方法的小作用
可以在学生类中重写equals方法
需求:
给两个对象赋值,并且通过equals方法判断两个对象是否相等

9.Date类,一种表示时间的特殊格式
public Date():分配一个Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒
public Date(long date):分配一个Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数
(1)使用无参构造时:
Date s1 = new Date();
//返回当前的时间:cst中国时间, jan:一月,Mon:星期一
System.out.println(s1);//Mon Jan 02 15:14:45 CST 2023
(2)使用有参构造时:
作用:返回美国时间为1970年1月1日00:00:00的中国时间
Date s2 = new Date(1000*60*60);
System.out.println(s2);//Thu Jan 01 09:00:00 CST 1970
10.Date类里面的常用方法
public long getTime ():获取的是日期对象从1970年1月1日00:00:00到现在的毫秒值
public void setTime(long time):设置时间,给的是毫秒值

11.SimpleDateFormat类使用方法
构造方法:
public SimpleDateFormat ():构造一个simpleDateFormat,使用默认模式和日期格式
public SimpleDateFormat (String pattern):构造一个SimpleDateFormat使用给定的模式和默认的日期格式
格式化:从 Date 到 string
public final String format(Date date): 将日期格式化成日期/时间字符串
解析:从 string 到 Date
public Date parse (String source): 从给定字符串的开始解析文本以生成日期
如何格式化?
//格式化:从 Date 到 string
SimpleDateFormat s1 = new SimpleDateFormat();
Date d1= new Date();
System.out.println(d1);//Mon Jan 02 16:18:07 CST 2023
//通过format方法将当前时间:Mon Jan 02 16:18:07 CST 2023转换为23-1-2 下午4:17
String S1=s1.format(d1);
System.out.println(S1);//23-1-2 下午4:17
如何解析?
//解析:从 string 到 Date
String S2="2023/1/2 16:21:11";
//有参构造里面的格式必须与字符串给的格式相同
//至于为什么是yyyy不是y也不是Y,有待商讨
SimpleDateFormat s2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//通过parse方法将2023/1/2 16:21:11转换为Mon Jan 02 16:21:11 CST 2023
Date d2= s2.parse(S2);
System.out.println(d2);
附加:调用parse方法必须要抛出异常
处理异常快捷方式
抛出异常:alt+回车键 在方法上抛出异常
12.Calendar类
(1)public int get(int field):返回给定日历字段的值
(2)public abstract void add(int field, int amount):根据日历的规则,将指定的时间量添加或减去给定的日历字段
(3)public final void set(int year,int month,int date):设置当前日历的年月日
演示(1)的使用
//创建一个Calender对象
Calendar c=Calendar.getInstance();//多态的方式
//public int get(int field):返回给定日历字段的值
int n=c.get(Calendar.YEAR);
int y=c.get(Calendar.MONTH)+1;因为方法中是从0开始算月份的所以要将结果+1
int r=c.get(Calendar.DATE);
System.out.println("当前的时间为:"+n+"年"+y+"月"+r+"日");//当前的时间为:2023年1月6日
演示(2)的使用
需求:求三年前的今天
c.add(Calendar.YEAR,-3);
int n1=c.get(Calendar.YEAR);
int y1=c.get(Calendar.MONTH)+1;
int r1=c.get(Calendar.DATE);
System.out.println("三年前的今天为:"+n1+"年"+y1+"月"+r1+"日");//三年前的今天为:2020年1月6日
演示(3)的使用
c.set(2026,1,12);
int n3=c.get(Calendar.YEAR);
int y3=c.get(Calendar.MONTH)+1;
int r3=c.get(Calendar.DATE);
System.out.println("自己自定义的时间为:"+n3+"年"+y3+"月"+r3+"日");//自己自定义的时间为:2026年2月12日