JDK的常用类,暂时先写这些吧

JDK常用类

  1. 系统相关类:System Runtime
  2. 字符串相关类:String StringBuffer StringBuilder
  3. 日期相关类: Date DateFormat(抽象) SimpleDateFormat Calendar(抽象) GregorianGalendar
  4. 数学运算相关类:Math 随机数

装箱和拆箱

装箱:基础转引用

int a =10;
Integer integer = new Integer(a);
integer integer = a;//自动装箱

拆箱:引用转基础

String Calendar Date之间的转换

1.Calendar 转化 String

Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(calendar.getTime()); 
System.out.println(dateStr);

2.String 转化Calendar

String str="2012-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

3.Date 转化String

Date date = new Date();
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String dateStr=sdf.format(date);
System.out.println(dateStr);

4.String 转化Date

String str="2012-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date= sdf.parse(str);
System.out.println(date);

5.Date 转化Calendar

Date date = new Date();
 Calendar calendar = new GregorianCalendar();
 calendar.setTime(date);
 System.out.println(calendar);

6.Calendar转化Date

Calendar calendar = new GregorianCalendar();
Date date =calendar.getTime();
System.out.println(date);

各类型的常用方法

System类

currentTimeMillis()

返回以毫秒为单位的当前时间。

exit(int status)

终止当前正在运行的 Java 虚拟机。

gc()

运行垃圾回收器。

getProperties()

确定当前的系统属性。

getProperty(String key)

获取指定键指示的系统属性。

RunTime类

freeMemory()

返回 Java 虚拟机中的空闲内存量。

totalMemory()

返回 Java 虚拟机中的内存总量。

String类

charAt(int index)

返回指定索引处的 char 值。

String a = "helloworld!";  
System.out.println(a.charAt(7));
compareTo(String anotherString)
compareToIgnoreCase(String str)
String a = "A";
String b = "z";
System.out.println(a.compareTo(b));//按字典顺序比较两个字符串
System.out.println(a.compareToIgnoreCase(b));//按字典顺序比较两个字符串,不考虑大小写。
concat(String str)
contains(CharSequence s)
endsWith(String suffix)
String a = "hello";
String b = "world!";
System.out.println(a.concat(b));//将指定字符串连接到此字符串的结尾。
System.out.println(a.contains("hel"));//当且仅当此字符串包含指定的 char 值序列时,返回 true。  
System.out.println(b.endsWith("rld!"));//测试此字符串是否以指定的后缀结束。
indexOf(int ch)

返回指定字符在此字符串中第一次出现处的索引。

replace(char oldChar, char newChar)

返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。

startsWith(String prefix)

测试此字符串是否以指定的前缀开始。

toCharArray()

将此字符串转换为一个新的字符数组。

trim()

返回字符串的副本,忽略前导空白和尾部空白。

Date类

compareTo(Date anotherDate)

比较两个日期的顺序。

getDate()

已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.DAY_OF_MONTH) 取代。

setTime(long time)

设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。

DateFormat类

format(Date date)

将一个 Date 格式化为日期/时间字符串。

parse(String source)

从给定字符串的开始解析文本,以生成一个日期。

Calendar类

compareTo(Calendar anotherCalendar)

比较两个 Calendar 对象表示的时间值(从历元至现在的毫秒偏移量)。

get(int field)

返回给定日历字段的值。

getInstance()

使用默认时区和语言环境获得一个日历。

setTime(Date date)

使用给定的 Date 设置此 Calendar 的时间。

GregorianCalendar类

用来作为Calendar类的初始化对象

Math类

pow(double a, double b)

返回第一个参数的第二个参数次幂的值。

ceil向上取整
floor向下取整
round 四舍五入
max 找两个数中较大的数
min 找两个数中较小的数
random 随机数(0-1)
原文地址:https://www.cnblogs.com/tangyizhuo/p/6099611.html