20201225-杜飞强
总结
String、StringBuffer、StringBuilder的区别
String:字符串常量
String是不可变实例,对String类型进行改变时,相当于生成了一个新的String实例,然后指向新的String实例。
StringBuffer:字符串变量
StringBuffer是可变字符串,每次对StringBuffer实例进行改变时,会对StringBuffer对象本身进行操作,而不是生成新的实例。
StringBuilder:字符串变量
StringBuilder类是下JDK5.0之后才有的,与StringBuffer类用法相同,区别在于StringBuffer类是线程安全的,而StringBuilder类是单线程的,在效率方面优于StringBuffer,在安全性方面弱于StringBuffer。
日期操作类
Date类
用来表示日期和时间,最多的是获取当前系统的日期和时间,主要实例方法有:
public boolean after(Date when)
测试日期是否在被测日期之后,即若当前的Date的瞬间晚于参数when的瞬间,则返回true。
public boolean before(Date when)
测试日期是否在被测日期之前,即若当前Date的瞬间早于参数when的瞬间,则返回true。
public boolean equals(Object ojb)
比较两个日期的相等性,即若当前Date实例的与指定参数的值相等,则返回true。
public int compareto(Date anotherDate)
比较两个日期的顺序,即当Date实例的表示的瞬间,早于、等于、晚于anotherDate表示的瞬间,则分别返回大于0、等于0、小于0的整数
Calendar类
是一个抽象类,提供多种格式化和解析时间的方法,GregorianCalendar类是 Calendar非抽象的子类,Calendar为特定瞬间与一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法。
格式化:将日期转换成文本
解析:将文本转换成日期格式
calender.clean():清除所有日历的字段
calender.set():设置单个日历字段的值
public final Date getTime()
返回一个表示此 Calendar时间值(从历元至现在的毫秒偏移量)的 Date 对象。
DateFormat类
SimpleDateFormat类是一个格式化和解析日期的具体类,
使用SimpleDateFormat类的format()方法可以将Date类型转换为String类型。
LocalDate类(year month dateofMonth)
java.time包是JDK1.8新增的包,而LocalDate类是1.8新增的最终类,它的实例都是不可变对象,包括
public static LocalDate now()在默认时区中从系统时钟获取当前日期。
public static LocalDate of(int year, int month, int dayOfMonth):LocalDate从年,月和日获取实例。
LocalTime类(hour / minute / second / nano )
不变的日期时间对象,通常被视为年-月-日-时-分-秒。
public static LocalDate now():在默认时区中获取当前系统的日期
public static LocalDate of(int year, int month, int dayOfMonth, int hour, int minute):获取LocalDateTime年,月,日,小时和分钟的实例,将秒和纳秒设置为零
近期评论