更新时间:2018年09月30日10时30分 来源:传智播客 浏览次数:
1
2
|
String toString ( ) :返回该对象的字符串表示 boolean equals ( Object obj ) :指示其他某个对象是否与此对象 "相等" |
1
|
public String toString ( ) :返回该对象的字符串表示 |
1
2
3
4
5
|
< > Object类中toString ( ) 的实现方式 : public class Object { public String toString ( ) { return getClass ( ) .getName ( ) + "@" + Integer.toHexString ( hashCode ( ) ) ; } } |
1
2
3
4
5
6
|
getClass().getName(): getClass(): Object类的方法, 获取当前对象的类的字节码对象 getClass().getName(): 通过字节码对象获取该类的全名 Integer.toHexString(hashCode()) hashCode(): Object类的方法, 获取当前对象地址值的哈希值 Integer.toHexString( int n): 将参数转换为十六进制数字的字符串 |
IDEA快捷键: Alt+Insert, 选 equals() and hashCode() |
01
02
03
04
05
06
07
08
09
10
|
public boolean equals ( Object o ) { / / 如果对象地址值相同 , 则是同一个对象 , 那么属性值肯定相同 , 认为相等 if ( this = = o ) return true ; / / 如果被比较对象为null , 或者不是同一个类型 , 则认为不相等 if ( o = = null || getClass ( ) ! = o.getClass ( ) ) returnfalse; / / 如果不是同一个对象 , 且不为null , 且是一个类型 , 则向下转型比较子类特有属性 Person person = ( Person ) o; / / 基本类型属性值用 = = 比较 , 引用类型属性值用Objects工具类的 equals ( ) 比较 return age = = person .age & & Objects. equals ( name , person . name ) ; |
1
2
3
|
public static boolean equals ( Object a , Object b ) { return ( a = = b ) || ( a ! = null & & a. equals ( b ) ) ; } |
1
2
|
Date ( ) : 创建Date对象 , 表示当前系统时间 Date ( long date ) : 创建Date对象 , 使用指定的毫秒值作为时间 |
1
2
3
4
5
|
long getTime ( ) : 获取Date对象中保存的时间毫秒值 void setTime ( long time ) : 修改Date对象的时间 UNIX时间戳 : 从 0 时区 1970 -01 -0100 : 00 : 00 开始 , 至今经过的毫秒值 ( 1 秒 = 1000 毫秒 ) 10 位 精确到秒 : 1494992791 = 2017 / 5 / 17 11 : 46 : 31 13 位 精确到毫秒 : 1494992791000 = 2017 / 5 / 17 11 : 46 : 31 |
1
2
3
4
5
|
java. text .DateFormat抽象类 : 用于格式化和解析时间. 提供了方便的方法 / / 常用成员方法 ( 抽象类不能创建对象 , 但可以有非抽象的方法供子类使用 ) String format ( Date date ) : 格式化 , 从Date对象转换为String对象 Date parse ( String source ) : 解析 , 从String对象转换为Date对象 java. text .SimpleDateFormat类 |
01
02
03
04
05
06
07
08
09
10
11
12
|
y : 年 M : 月 d : 日 H : 时 ( 24 小时制 ) m : 分 s : 秒 E : 星期 D : 年中的天 K : 小时 ( 12 小时制 ) S : 毫秒 示例 : "yyyy-MM-dd E HH:mm:ss.SSS" 结果 : 2016 -04 -01 星期五 17 : 29 : 15.8 68 |
1
|
SimpleDateFormat format = newSimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ; |
1
2
3
|
例如 , SimpleDateFormat对象的模式是 : "yyyy年MM月dd日 HH:mm:ss" 那么 , 将Date格式化后就可以是这种样子 : 2018 年 01 月 02 日 03 : 04 : 05 SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy年MM月dd日 HH:mm:ss" ) ; |
1
|
Date date = new Date ( ) ; |
1
2
|
String s = sdf.format ( date ) ; System.out.println ( s ) ; |
1
2
3
|
Date parse ( String source ) : 解析 , 从String对象转换为Date对象 例如 , SimpleDateFormat对象的模式是 : "yyyy-MM-dd" 要解析为Date对象的字符串必须符合模式 : 2000 -01 -02 [ / size][ / font ][ / align][align = left][ font = 微软雅黑][size = 3 ][color = #df402a]String s = "2000-10-01";[/color][/size][/font][font=微软雅黑][size=3] |
1
2
|
SimpleDateFormat sdf = newSimpleDateFormat ( "yyyy-MM-dd" ) ; Date date = sdf.parse ( s ) ; |
1
2
|
long time = date .getTime ( ) ; System.out.println ( time ) ; |
1
|
static Calendar getInstance ( ) : 根据当前系统设置获取合适的Calendar对象 , 表示当前系统时间 |
1
|
Calendar c = Calendar.getInstance ( ) ; / / 代表了当前时间 |
1
|
Calendar.getInstance ( ) 会根据当前系统获取合适的子类对象 , 我们获取到的是 GregorianCalendar |
1
2
3
4
5
6
7
|
static int YEAR : 年份 static int MONTH : 月份. 注意月份数值是 0 -11 static int DAY_OF_MONTH : 日期 static int HOUR : 小时 ( 12 小时制 ) static int HOUR_OF_DAY : 小时 ( 24 小时制 ) static int MINITE : 分钟 static int SECOND : 秒 |
1
2
3
4
5
|
int get ( int field ) : 获取指定日历字段的值 int year = cal. get ( Calendar.YEAR ) void set ( int field , int value ) : 修改指定日历字段为指定的值 void add ( int field , int amount ) : 调整指定日历字段的值. 正数增加 , 负数减少 Date getTime ( ) : Calendar转Date void setTime ( Date d ) : Date转Calendar |
1
|
int day = calendar. get ( Calendar.DAY_OF_MONTH ) ; |
1
|
calendar. set ( Calendar.YEAR , 2000 ) ; |
1
|
calendar. set ( Calendar.MONTH , 0 ) ; |
1
|
calendar. add ( Calendar.DAY_OF_MONTHY , 1 ) ; |
1
|
calendar. add ( Calendar.DAY_OF_MONTHY , -1 ) ; |
1
|
currentTimeMillis ( ) |
1
|
static long currentTimeMillis ( ) : 返回当前系统时间的毫秒值 |
01
02
03
04
05
06
07
08
09
10
11
12
|
/ / 先记录开始时间毫秒值 long start = System.currentTimeMillis ( ) ; / / 循环 for ( int i = 0 ; i < 100000 ; i + + ) { System.out.println ( "我爱Java " + i ) ; } / / 循环结束后记录结束时间毫秒值 long end = System.currentTimeMillis ( ) ; / / 显示结果 System.out.println ( "程序执行时间: " + ( end - start ) + "毫秒" ) ; arrayCopy ( Object src , int srcPos , Object dest , intdestPos , int length ) java.lang.System类 : 系统相关功能 |
1
2
3
4
5
6
7
|
static void arrayCopy ( Object src , int srcPos , Objectdest , int destPos , int length ) : 复制源数 组中指定长度个元素到一个新数组中 * Object src : 源数组 ( 被复制的数组 ) * int srcPos : 源数组索引 ( 从源数组的哪个索引开始复制 ) * Object dest : 目标数组 ( 要复制到哪个数组 ) * int destPos : 目标数组索引 ( 指定目标数组接收元素的索引位置 ) * int length : 长度 ( 要复制的元素个数 ) |
1
|
|