java常用类基础知识点

在这里插入图片描述
标题标题常用类基础知识点
主要包括:String类,StringBuffer类,StringTokenizer类,Date类,Calender类,Math类,BigInerger类, DecimalFormat类。

因为知识点较多,且不是概念性的,就用例子表示,
StringBuffer类

构造方法

 * public StringBuffer():无参构造方法
 * public StringBuffer(int capacity):指定容量的字符串缓冲区对象
 * public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
 * public int length():返回长度(字符数)。 实际值

添加功能

 * public StringBuffer append(String str):
    * 可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
 * public StringBuffer insert(int offset,String str):
    * 在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身

删除功能

  * public StringBuffer deleteCharAt(int index):
    * 删除指定位置的字符,并返回本身
  * public StringBuffer delete(int start,int end):
    * 删除从指定位置开始指定位置结束的内容,并返回本身

替换功能

   * public StringBuffer replace(int start,int end,String str):
    * 从start开始到end用str替换

反转功能

   * public StringBuffer reverse():
    * 字符串反转

截取功能

   * public String substring(int start):
    * 从指定位置截取到末尾
   * public String substring(int start,int end):
    * 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置

Math类

   * public static int abs(int a)
    Math.abs()  取绝对值
   * public static double ceil(double a)
    Math.ceil() 向上取整,结果是一个double值
   * public static double floor(double a)
    Math.floor() 向下取整,结果是一个double值
   * public static int max(int a,int b) min自学
    Math.max(a,b) 获取两个值当中的最大值
   * public static double pow(double a,double b)
    Math.pow(a,b) 求a得b次方
   * public static double random()
    Math.random() 生成0.0到1.0之间的所有小数,包括0.0,不包括1.0
   * public static int round(float a) 参数为double的自学
    Math.round() 四舍五入
   * public static double sqrt(double a)
    Math.sqrt() 开平方

System类

成员方法
* public static void gc()
System.gc() 运行垃圾回收器,相当于呼叫保洁大爷
* public static void exit(int status)
System.exit(0) 退出jvm,0是正常终止,非0是异常终止
* public static long currentTimeMillis()
System.currentTimeMillis() 返回当前时间与协调世界时1970年1月1日午夜之间的时间差(以毫秒为单位测量)
* pubiic static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
int[] src = {11,22,33,44,55};
int[] dest = new int[8];
System.arraycopy(src,0,dest,0,src.length);
for (int i = 0; i < dest.length; i++) {
System.out.println(dest[i]);
}
输出的结果为:11 22 33 44 55 0 0 0
Date类

构造方法
* public Date()
* public Date(long date)
成员方法
* public long getTime()
获取毫秒值,和System,currentTimeMillis()相似
* public void setTime(long time)
设置毫秒值
Date d = new Date();
d.setTime(1000);
System.out.println(d);
输出的结果为:Thu Jan 01 08:00:01 CST 1970

SimpleDateFormat类

构造方法
* public SimpleDateFormat()
* public SimpleDateFormat(String pattern)
成员方法
* public final String format(Date date)
format()是将日期对象转换为字符串的形式输出
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy年MM月dd日 HH:mm:ss”);
System.out.println(sdf.format(d));
输出的结果:2017年08月07日 09:45:02
* public Date parse(String source)
parse()是将时间字符串换成日期对象
String s = “2000年08月08日 08:08:08”;
SimpleDateFormat sdf = new SimpleDateFormat(“yy年MM月dd日 HH:mm:ss”);
Date d = sdf.parse(s);
System.out.println(d);
输出的结果为:Tue Aug 08 08:08:08 CST 2000

Calendar类

成员方法
* public static Calendar getInstance()
* public int get(int field)
Calendar c = Calendar.getInstance(); //父类引用子类对象
System.out.println(c.get(Calendar.YEAR));
System.out.println(c.get(Calendar.MONTH));
输出的结果:2017
(这里的7不是7月,是8月,java中默认是0开始)

   * public void add(int field,int amount)
      Calendar c = Calendar.getInstance();  
      c.add(Calendar.YEAR, 1);
      System.out.println(c.get(Calendar.YEAR));
      输出的结果:2018
  * public final void set(int year,int month,int date)
      Calendar c = Calendar.getInstance();
      c.set(Calendar.YEAR, 2000);
      System.out.println(c.get(Calendar.YEAR));
      输出的结果为:2000

猜你喜欢

转载自blog.csdn.net/qq_43039291/article/details/82780950