可变字符序列——StringBuffer
StringBuffer类和String类的方法几乎一样,不过StringBuffer对象表示的字符串是可以改变的,而String对象保存的字符串是不可变的。
public class Demo {
public static void main(String[] args) throws Exception {
// StringBuffer([int capacity]) 不指定容量时,默认capacity为16
StringBuffer s = new StringBuffer(4);
System.out.println(s.capacity()); //4
System.out.println(s.length()); //0
// 如果字符串的长度大于原来的容量,则会扩充容量
s.append("hello");
System.out.println(s.capacity()); //10
System.out.println(s.length()); //5
}
}
日期类——Date
import java.util.Date;
public class Demo {
public static void main(String[] args) throws Exception {
System.out.println(new Date());
// Thu Nov 29 16:01:26 CST 2018
// 获取自1970年1月1日起的毫秒数
System.out.println(new Date().getTime());
// 1543478551775
// 传入某个时刻的时间戳,转换为当时的时间标准格式
System.out.println(new Date(1543478551775L));
// Thu Nov 29 16:02:31 CST 2018
}
}
更易用的日期类——SimpleDateFormat
import java.util.Date;
import java.text.SimpleDateFormat;
public class Demo {
public static void main(String[] args) {
SimpleDateFormat d1 = new SimpleDateFormat();
System.out.println(d1.format(new Date()));
// 18-11-29 下午4:12
SimpleDateFormat d2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(d2.format(new Date()));
// 2018-11-29 16:16:14
// 将时间戳转化为标准格式
System.out.println(d2.format(new Date(1543478551775L)));
// 2018-11-29 16:02:31
}
}
数学计算类——Math
注意Math类是java.lang包下面,所以Math类不用import。