20201225-张景安
总结:
零碎知识点补充:
-
整数交换:= eg: a=b; b=a; a=b;
public void sort( int[] array ) { for( int i = 0 ; i < array.length ; i++ ) {
for( int j = 0 ; j < array.length - 1 - i ; j++ ) {
if( array[ j ] > array[ j + 1 ] ) {
array[ j ] ^= array[ j + 1 ] ;
array[ j + 1 ] ^= array[ j ] ;
array[ j ] ^= array[ j + 1 ] ;
}
}
}
} -
进制表示:
- 以0b或0B为前缀的是二进制;
- 以0x或0X为前缀的是十六进制;
- 以0为前缀的是8进制;
package com.itlaobing.methods.exercise; public class OperatorTest {
public static void main(String[] args) {
// 以 0b 或 0B 为前缀的是 二进制,计数符号有 0、1
// 以 0 为前缀是 八进制,计数符号有 0、1、2、3、4、5、6、7
// 没有任何前缀直接书写的是 十进制,计数符号有 0、1、2、3、4、5、6、7、8、9
// 以 0x 或 0X 为前缀是 十六进制,计数符号有 0、1、2、3、4、5、6、7、8、9、A、B、C、D、E、F
int a = 0b0000_0000_0000_0000_0000_0000_0000_0111 ; // 7
int b = 0b0000_0000_0000_0000_0000_0000_0000_0101 ; // 5
System.out.println( "a = " + a + " , b = " + b );
// 7: 0000_0000_0000_0000_0000_0000_0000_0111
// 5: 0000_0000_0000_0000_0000_0000_0000_0101
a ^= b ; // a = a ^ b ; // 0000_0000_0000_0000_0000_0000_0000_0010
System.out.println( a );
// 5: 0000_0000_0000_0000_0000_0000_0000_0101
// 2: 0000_0000_0000_0000_0000_0000_0000_0010
b ^= a ; // b = a ^ b ; // 0000_0000_0000_0000_0000_0000_0000_0111
System.out.println( b );
// 2: 0000_0000_0000_0000_0000_0000_0000_0010
// 7: 0000_0000_0000_0000_0000_0000_0000_0111
a ^= b ; // a = a ^ b ; // 0000_0000_0000_0000_0000_0000_0000_0101
System.out.println( a );
System.out.println( "a = " + a + " , b = " + b );
}
}
Calendar类:
-
Calendar 类是一个抽象类,GregorianCalendar 类是 Calendar 非抽象的子类
-
Calendar 为特定瞬间与一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法
package com.itlaobing.calendar;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* 1、Calendar 类是一个抽象类,GregorianCalendar 类是 Calendar 非抽象的子类
* 2、Calendar 为特定瞬间与一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法
*/
public class CalendarTest1 {
public static void main(String[] args) {
StringBuilder builder = new StringBuilder();
Calendar calendar = new GregorianCalendar();
System.out.println( calendar );
// Calendar.YEAR 是 与年份有关的 日历字段( calendar field )
int year = calendar.get( Calendar.YEAR ); // 获得年份
builder.append( year );
builder.append( '年' );
// 鬼子那边的月份是从 0 开始计数的
int month = calendar.get( Calendar.MONTH ) + 1 ; // 获得月份
builder.append( month );
builder.append( '月' );
int date = calendar.get( Calendar.DATE ); // 获得日期 Calendar.DAY_OF_MONTH / Calendar.DATE
builder.append( date );
builder.append( '号' );
int hour = calendar.get( Calendar.HOUR_OF_DAY );
builder.append( hour );
builder.append( ':' );
int minute = calendar.get( Calendar.MINUTE );
builder.append( minute );
builder.append( ':' );
int second = calendar.get( Calendar.SECOND );
builder.append( second );
builder.append( '.' );
int millis = calendar.get( Calendar.MILLISECOND );
builder.append( millis );
System.out.println( builder ); // builder == null ? "null" : builder.toString()
}
}
Date类:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
public class DetaTest {
public static void main(String[] args) {
long currentTime = System.currentTimeMillis();
Date date = new Date(currentTime);
System.out.println(date);
System.out.println("--------------------------------");
LocalDate today = LocalDate.now();
System.out.println(today); //输出当前日前;
LocalDateTime time = LocalDateTime.now();
System.out.println(time); //输出当前日期时分秒毫秒;
System.out.println("--------------------------------");
LocalDate today1 = LocalDate.of(1997,11,01);
System.out.println(today1); //输出指定日前;
LocalDateTime time1 = LocalDateTime.of(1997,11,01,12,35,45,765);
System.out.println(time1); //输出指定日期时分秒毫秒;
}
}
//无参构造:输出date时,输出当前时间;
//有参构造:带系统当前的毫秒值,输出当前时间;
-
java.util.Date类尚未被废弃的实例方法:
- public long getTime()
- public void setTime( long millis )
- public int hashCode()
- public String toString()
- public boolean before( Date when ) 若 当前Date实例表示的瞬间 早于 参数when表示的瞬间 就返回 true
- public boolean after( Date when ) 若 当前Date实例表示的瞬间 晚于 参数when表示的瞬间 就返回 true
- public boolean equals( Object another ) 若 当前Date实例 与 参数指定的对象 相等 则返回 true
- public int compareTo( Date anotherDate ) 比较两个日期的顺序,
- 当 当前Date实例表示的瞬间 早于、等于、晚于 参数anotherDate所表示的瞬间 则分别返回 小于零、零 、大于零的 整数
- public Object clone()
- public Instant toInstant() 【 JDK 1.8 新增 】
-
java.util.Date类中的类方法:
- public static Date from( Instant instant ) 【 JDK 1.8 新增 】
package com.itlaobing.date;
import java.util.Date;
/**
* 1、java.util.Date类尚未被废弃的实例方法:
* public long getTime()
* public void setTime( long millis )
* public int hashCode()
* public String toString()
* public boolean before( Date when ) 若 当前Date实例表示的瞬间 早于 参数when表示的瞬间 就返回 true
* public boolean after( Date when ) 若 当前Date实例表示的瞬间 晚于 参数when表示的瞬间 就返回 true
* public boolean equals( Object another ) 若 当前Date实例 与 参数指定的对象 相等 则返回 true
* public int compareTo( Date anotherDate ) 比较两个日期的顺序,
* 当 当前Date实例表示的瞬间 早于、等于、晚于 参数anotherDate所表示的瞬间 则分别返回 小于零、零 、大于零的 整数
* public Object clone()
* public Instant toInstant() 【 JDK 1.8 新增 】
* 2、java.util.Date类中的类方法:
* public static Date from( Instant instant ) 【 JDK 1.8 新增 】
*/
public class DateTest3 {
public static void main(String[] args) {
Date first = new Date( 1608884569999L );
System.out.println( first.getTime() );
Date second = new Date( 1608884569999L );
System.out.println( second.getTime() );
Date third = new Date( 1598884567040L );
System.out.println( third.getTime() );
System.out.println( "- - - - - - - - - - - - - - - - - - - -" );
System.out.println( first.before( second ) );
System.out.println( first.before( third ) );
System.out.println( "- - - - - - - - - - - - - - - - - - - -" );
System.out.println( first.after( second ) );
System.out.println( first.after( third ) );
System.out.println( "- - - - - - - - - - - - - - - - - - - -" );
System.out.println( first.equals( second ) );
System.out.println( first.equals( third ) );
System.out.println( "- - - - - - - - - - - - - - - - - - - -" );
System.out.println( first.compareTo( second ) );
System.out.println( first.compareTo( third ) );
System.out.println( third.compareTo( first ) );
}
}
点赞(1)
评论留言