publicclassIntegerDemo{publicstaticvoidmain(String[] args){//int --- Stringint number =100;//方式1String s1 = number +"";System.out.println(s1);//方式2//public static String valueOf(int i)String s2 =String.valueOf(number);System.out.println(s2);System.out.println("--------");}}
String转换为int
转换方式
方式一:先将字符串数字转成Integer,再调用valueOf()方法
方式二:通过Integer静态方法parseInt()进行转换
示例代码
publicclassIntegerDemo{publicstaticvoidmain(String[] args){//String --- intString s ="100";//方式1:String --- Integer --- intInteger i =Integer.valueOf(s);//public int intValue()int x = i.intValue();System.out.println(x);//方式2//public static int parseInt(String s)int y =Integer.parseInt(s);System.out.println(y);}}
public final void set(int year,int month,int date)
设置当前日历的年月日
示例代码
publicclassCalendarDemo{publicstaticvoidmain(String[] args){//获取日历类对象Calendar c =Calendar.getInstance();//public int get(int field):返回给定日历字段的值int year = c.get(Calendar.YEAR);int month = c.get(Calendar.MONTH)+1;int date = c.get(Calendar.DATE);System.out.println(year +"年"+ month +"月"+ date +"日");//public abstract void add(int field, int amount):根据日历的规则,将指定的时
间量添加或减去给定的日历字段
//需求1:3年前的今天// c.add(Calendar.YEAR,-3);// year = c.get(Calendar.YEAR);// month = c.get(Calendar.MONTH) + 1;// date = c.get(Calendar.DATE);// System.out.println(year + "年" + month + "月" + date + "日");//需求2:10年后的10天前// c.add(Calendar.YEAR,10);// c.add(Calendar.DATE,-10);// year = c.get(Calendar.YEAR);// month = c.get(Calendar.MONTH) + 1;// date = c.get(Calendar.DATE);// System.out.println(year + "年" + month + "月" + date + "日");//public final void set(int year,int month,int date):设置当前日历的年月日
c.set(2050,10,10);
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH)+1;
date = c.get(Calendar.DATE);System.out.println(year +"年"+ month +"月"+ date +"日");}}
2.6二月天案例(应用)
案例需求 获取任意一年的二月有多少天
代码实现
publicclassCalendarTest{publicstaticvoidmain(String[] args){//键盘录入任意的年份Scanner sc =newScanner(System.in);System.out.println("请输入年:");int year = sc.nextInt();//设置日历对象的年、月、日Calendar c =Calendar.getInstance();
c.set(year,2,1);//3月1日往前推一天,就是2月的最后一天
c.add(Calendar.DATE,-1);//获取这一天输出即可int date = c.get(Calendar.DATE);System.out.println(year +"年的2月份有"+ date +"天");}}
publicclassExceptionDemo02{publicstaticvoidmain(String[] args){System.out.println("开始");method();System.out.println("结束");}publicstaticvoidmethod(){try{int[] arr ={1,2,3};System.out.println(arr[3]);//new ArrayIndexOutOfBoundsException();System.out.println("这里能够访问到吗");}catch(ArrayIndexOutOfBoundsException e){//newArrayIndexOutOfBoundsException();// e.printStackTrace();//public String getMessage():返回此 throwable 的详细消息字符串// System.out.println(e.getMessage());//Index 3 out of bounds for length 3//public String toString():返回此可抛出的简短描述// System.out.println(e.toString());//java.lang.ArrayIndexOutOfBoundsException: Index 3 out of boundsfor length 3//public void printStackTrace():把异常的错误信息输出在控制台
e.printStackTrace();// java.lang.ArrayIndexOutOfBoundsException: Index 3 out of boundsfor length 3// at com.itheima_02.ExceptionDemo02.method(ExceptionDemo02.java:18)// at com.itheima_02.ExceptionDemo02.main(ExceptionDemo02.java:11)}}}
3.5编译时异常和运行时异常的区别(记忆)
编译时异常
都是Exception类及其子类
必须显示处理,否则程序就会发生错误,无法通过编译
运行时异常
都是RuntimeException类及其子类
无需显示处理,也可以和编译时异常一样处理
3.6throws方式处理异常(应用)
定义格式
自定义异常类
publicvoid 方法()throws 异常类名 {}
示例代码
publicclassExceptionDemo{publicstaticvoidmain(String[] args){System.out.println("开始");// method();try{method2();}catch(ParseException e){
e.printStackTrace();}System.out.println("结束");}//编译时异常publicstaticvoidmethod2()throwsParseException{String s ="2048-08-09";SimpleDateFormat sdf =newSimpleDateFormat("yyyy-MM-dd");Date d = sdf.parse(s);System.out.println(d);}//运行时异常publicstaticvoidmethod()throwsArrayIndexOutOfBoundsException{int[] arr ={1,2,3};System.out.println(arr[3]);}}