实用类

一、枚举

    定义:枚举指由一组固定的常量组成的类型。

    语法:访问修饰符  enum  枚举名{              public enum Sex{

              常量1,                                 Male,

              常量2,                                 Female

              ...

          }                                       }

     使用:   Sex sex = Sex.Male;

              if(sex == Sex.Male){

                  syso("男");

              }else{

                  syso("女");

              }

二、包装类

    作用:包装类把基本类型数据转换为引用类型

    注意:1.每个基本类型在java.lang包中都有一个相应的包装类

            int-Integer、long-Long、double-Double、short-Short、float-Float、boolean-Boolean、char-Character

          2.Number的扩展类(Short、Byte、Integer、Long、Float、Double)

          3.Object:Boolean、Number、Character

三、包装类常用方法

    1.toString()

    2.parseXXX();    parseInt()、parseDouble()

    3.valueOf()

四、装箱和拆箱

    装箱:基本类型转换为包装类的对象     Integer i = 5;

    拆箱:包装类对象转换为基本类型的值   int j = i;

五、Math类

    java.lang.Math类提供了常用的数学运算方法和两个静态常量E(自然对数的底数) 和PI(圆周率)

    abs、max、min、random、ceil、floor

六、Random类

    生成随机数

    Random rand=new Random(); //创建一个Random对象

    int num=rand.nextInt(10); //0-9

七、字符串常用的方法

    计算字符串的长度、比较字符串、连接字符串、提取字符串

    length、equals、equalsIgnoreCase()、toLowerCase()、toUpperCase()、indexOf()、lastIndexOf()、substring()、split()

八、StringBuffer

    对字符串频繁修改(如字符串连接)时,使用StringBuffer类可以大大提高程序执行效率。

    StringBuilder,等价StringBuffer

                StringBuffer buffer = new StringBuffer();

for(int i = 0; i < 10; i++){

String str = input.next();

buffer.append(str);

//在内存中都要重新开辟空间存储新的字符串

}

System.out.println(buffer.toString());

九、时间操作

    1.Date

      Date date = new Date();//获取系统当前时间

      注意:很多方法已经过时。

            月份值为0-11,代表1月-12月

    2.SimpleDateFormat

      作用:格式化时间      

            2016-06-07   2016-6-7  2016/06/07  2016年06月07日    06/07/16

      Date date1 = new Date();

      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      String text1 = dateFormat.format(date1);

      String text2 = "2016-08-15 12:30:57";

      try{

             Date date2 = dateFormat.parse(text2);

      }catch(Exception ex){

     System.out.println("转换失败");

      }

    3.Calendar

      //Calender   日历   abstract 抽象类无法直接实例化

      Calendar cal = Calendar.getInstance();

原文地址:https://www.cnblogs.com/liuxinsir/p/6437205.html