日期和异常

日期

package laodu05;
import sun.java2d.pipe.SpanShapeRenderer;
import java.text.SimpleDateFormat;
import java.util.Date;
​
public class DateTest01 {
    public static void main(String[] args) throws Exception{
        Date d = new Date();
        System.out.println(d);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/mm/dd HH:mm:ss");
        //date转换成String
        String nowtime = simpleDateFormat.format(d);
        System.out.println(nowtime);
        //创建一个字符串数组 将他转换成时间
        //String装换成date
             String h = "1999/11/07 16:20:30";                //格式必须和上面的一样
             SimpleDateFormat st = new SimpleDateFormat("yyyy/mm/dd HH:mm:ss");
             Date nowtime2 = st.parse(h);
        System.out.println(nowtime2);
​
    }
}
​

  1. 计算调用一个方法花费的时间

      //获取系统从1970到现在当前时间的总毫秒数
            long begin = System.currentTimeMillis();
            print();
            //获取系统从1970到现在当前时间的总毫秒数(比上面的多运行了一个方法的时间)
            long end = System.currentTimeMillis();
                System.out.println(end-begin); //计算运行print()方法话费单时间
    ​
    ​
    ​
        }
        //创建一个打印方法
        public static void print(){
            for (int i = 0; i < 1000 ;i++) {
                System.out.println( i = i+1);
    ​
            }
    ​
        }

  1. 生成一个随机数

           //先创建一个对象
            Random random = new Random();
            int i = random.nextInt();
            System.out.println(i);//生成一个随机数
                    //生成一个<10的随机数
            int b = random.nextInt(10);
            System.out.println(b);
        }

  1. HomeWork(自己写的)

    //生成5个随机数,并且不相等,相等的话重新生成,将他们放在一个数组中
     public static void main(String[] args) {
            //生成5个随机数,并且不相等,相等的话重新生成,将他们放在一个数组中
            //先创建一个对象
            Random random = new Random();
            //生成5个100以内的随机数
            int i = random.nextInt(100);
            int b = random.nextInt(100);
            int c = random.nextInt(100);
            int d = random.nextInt(100);
            int a = random.nextInt(100);
            //创建一个数组
            int[] sz = new int[5];
            if (a!=b&&b!=c&&c!=d&&d!=i){
                sz[0] = i;
                sz[1] = a;
                sz[2] = b;
                sz[3] = c;
                sz[4] = d;
            }
                        //遍历输出数组
            for (int j = 0; j < sz.length; j++) {
    ​
                System.out.print(sz[j]+" ");
            }
    ​
        }
  2. 老师写的

     public static void main(String[] args) {
            Random random = new Random();
                int[] sz = new int[5];
                int dex = 0;
    ​
                for (int i = 0; i < sz.length; i++) {
                    int a = random.nextInt(100);
                               //如果两个数组不相等是true,执行下一步
                        if (!panduan(sz,a)){
                            sz[dex] = a;
                            dex++;
                        }
                    System.out.println(sz[i]);
            }
    ​
        }
    //用来判断每次传进来的数字与数组中的是否一样
       public static boolean panduan(int[] sz,int a){
            //遍历数组
           for (int i = 0; i < sz.length; i++) {
               //如果sz[i]!=a,返回true;
               if (sz[i]==a){
                   return true;
               }
           }   return false;
       }

    枚举

    1. 一枚一枚可以列举出来的,采用枚举

    2. 枚举编译之后也是class文件

    3. 枚举也是一种引用数据类型

    4. 枚举中的每一个值都可以看做常量

    5. 枚举的语法是

      enum 枚举类型名{

      枚举值1,枚举值2

          result a =  divde(10,20);
             result b =  divde(10,0);
              System.out.println(a==result.sucss ?"计算成功":"计算失败" );//输出的a为sucss,所以输出计算成功
              System.out.println(b==result.sucss ?"计算成功":"计算失败" );//输出的b为fale,所以输出计算失败
      ​
      ​
          }
      ​
         public static result divde(int a, int b){
               try{
                    int i = a/b;
                    return result.sucss;
      ​
               }catch (Exception e){
                    return result.fale;
               }
         }
      ​
      }
      enum result{
      ​
          sucss,fale
      }

异常

  1. 异常也是一个类

  2. 出现异常的时候系统会new一个对象

    java的异常处理机制

    1. 编译时异常(受检异常)

      • 在异常发生前对异常做出预处理(发生率较高的),比如我看见外面下雨了,出去大概率感冒,我提前做出处理带一把伞

    2. 运行时异常(未受检异常)

      • 就是没对那些发生概率很低的异常就行预处理

    3. 处理异常的两种方式

      • 在方法声明的位置上,使用throws关键字,抛给上一级

        谁调用抛给谁

      • 使用try..catch语句进行异常的铺装

    4. java中异常如果发生之后一直上跑抛给了main方法的调用者,那么该项目运行终止

         public static void main(String[] args) {
              System.out.println("main begin");
              try{
                 m1();
                  System.out.println();
              }catch (FileNotFoundException e){
                  System.out.println("文件找不到");
              }
              System.out.println("main over");
          }
          public static void m1() throws FileNotFoundException{
              System.out.println("m1 begin");
              m2();
              System.out.println("m1 over");
          }
          public static void m2() throws FileNotFoundException{
              System.out.println("m2 begin");
                 m3();
              System.out.println("m2 over");
          }
          public static void m3() throws FileNotFoundException {
              new FileInputStream("C:\\Users\\Administrator\\Desktop\\大三下");
          }
      ​
    5. catch 可以写多个

       try{
                 m1();
                  System.out.println();
              }catch (FileNotFoundException e){
                  System.out.println("文件找不到");
              }catch (IOException e){
           System.out.println("读取失败到");
       }
    6. 但是要注意catch异常的顺序比如,IOException是FileNotFoundException是父类,所以如果先catchIOException在catch FileNotFoundException会报错

       try{
                 m1();
                  System.out.println();
              }catch (IOException e){
                  System.out.println("文件找不到");
              }catch (FileNotFoundException e){//会报错
           System.out.println("读取失败到");
       }

      总结,catch可以写多个,但是要从上到下,由小到大

      catch可以写准确的异常类型,也可以写该类型的父类型

打印异常的追踪信息:x.printStackTrace();

 System.out.println("main begin");
        try{
           m1();
            System.out.println();
        }catch (FileNotFoundException e){
            //在这里可以打印异常的追踪信息
            e.printStackTrace();
           
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值