0%

6.29 常用类

2017年6月29日 上午8:02

1.

Object

Java.lang.Object
1. equal()
2. toString()
3. getClass() 返回运行时类
4. finalize()
5. notify() 唤醒在此对象监视上等待的单个线程
6. notifyAll()
7. wait()

Math

java.util.Math
都是静态方法
1. abs()
2. max()
3. min()
4. random() 0.0~1.0 包括0.0 不包括1.0
5. sin()
6. exp()
7. sqart(9) = 3
8. Pow(2.3) = 8

Date

1. 日期转字符串1(DateFormat.getDateInstance)
1
2
3
4
Date date = new Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
String string= df.format(date);
System.out.println(string);
2. 日期转字符串2(SimpleDateFormat)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//日期转换成字符串
Date date = new Date();

//这里的"yyyy-MM-dd" 要与 “String time = "1995-2-27"”格式对应
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String string = sdf.format(date);
System.out.println(string);

//字符串转换成日期
String time = "1995-2-27";
try {
Date dtime = sdf.parse(time);
System.out.println(dtime);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Calendar//默认指向的是当前时间,站在当前日期的角度

操作的步骤一般是1-4
1. 获取对象
Calendar.getInstance()
2. 修改默认时间
1. cal.add()
2. cal.set(年,月,日)
1. 月是0-11
3. 对当前设置的时间进行转换
1. cal.get(Calendar.DAY_OF_WEEK)
1. 星期日=1
2. 获取当前设置日期的星期几
4. 得到Date类型的时间
1. cal.getTime()

1
2
3
4
5
6
7
8
9
10
11
12
//获取操作对象
Calendar cal = Calendar.getInstance();
//修改当前时间
cal.add(Calendar.MONTH, -1);//修改当前日期
cal.set(1995, 1, 27);//覆盖了上一句
System.out.println("输出星期几:"+cal.get(Calendar.DAY_OF_WEEK));
//得到当前时间
Date date = cal.getTime();
//对时间进行转换
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String string = sdf.format(date);
System.out.println("设置的当前时间:"+string);

包装类

  1. 八个基本类型对应一个包装类
  2. 数字和字符之间的转换
    1. Integer.parseInt(str)
      1
      2
      3
      4
      String str="123";
      int num1 = Integer.parseInt(str);
      double num2 = Double.parseDouble(str);
      Sysytem.out.print(num1 + num2);
    2. String.valueOf(num1)
      1
      2
      String str = num1 + "";
      String str = String.valueOf(num1);
  3. 装箱 拆箱
    1
    2
    3
    4
    5
    Integer integer = new Integer(20);  //手动装箱
    Integer integer = 20;//自动装箱

    int i = new Integer(5).intValue();//手动拆箱
    int i = new Integer(5);//自动拆箱
  4. Integer 不能自动转换为 Float
    1
    2
    3
    4
    5
    6
    public static void main(String args[]){
    method(5);//这里要改成method(5f)
    }
    public static void method(Float f){
    System.out.println(f);
    }