1. Math
1.1 Math类概述
Math 包含执行基本数字运算的方法
1.2 Math中方法的调用方式
Math 类中无构造方法,但内部的方法都是静态的,则可以通过 类名.进行调用
1.3 Math类的常用方法
方法名 方法名 说明 public static int abs(int a) 返回参数的绝对值 public static double ceil(double a) 返回大于或等于参数的最小double值,等于一个整数 public static double floor(double a) 返回小于或等于参数的最大double值,等于一个整数 public static int round(float a) 按照四舍五入返回最接近参数的int public static int max(int a,int b) 返回两个int值中的较大值 public static int min(int a,int b) 返回两个int值中的较小值 public static double pow (double a,double b) 返回a的b次幂的值 public static double random() 返回值为double的正值,[0.0,1.0)
2. System
2.1 System 类的常用方法
方法名 说明 public static void exit(int status) 终止当前运行的 Java 虚拟机,非零表示异常终止 public static long currentTimeMillis() 返回当前时间(以毫秒为单位)
2.2 示例代码
需求: 在控制台输出 1-10000,计算这段代码执行了多少毫秒
public class SystemDemo {
public static void main ( String[ ] args) {
long start = System. currentTimeMillis ( ) ;
for ( int i = 1 ; i <= 10000 ; i++ ) {
System. out. println ( i) ;
}
long end = System. currentTimeMillis ( ) ;
System. out. println ( "共耗时:" + ( end start) + "毫秒" ) ;
}
}
3. Object
3.1 Object类的toString方法
Object 类概述 Object 是类层次结构的根,每个类都可以将 Object 作为超类。所有类都直接或者间接的继承自该类,换句话说,该类所具备的方法,所有类都会有一份查看方法源码的方式 选中方法,按下 Ctrl + B重写 toString方法的方式
Alt + Insert 选择toString 在类的空白区域,右键 -> Generate -> 选择toString toString 方法的作用: 以良好的格式,更方便的展示对象中的属性值示例代码:
class Student extends Object {
private String name;
private int age;
public Student ( ) {
}
public Student ( String name, int age) {
this . name = name;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
@Override
public String toString ( ) {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}' ;
}
}
public class ObjectDemo {
public static void main ( String[ ] args) {
Student s = new Student ( ) ;
s. setName ( "林青霞" ) ;
s. setAge ( 30 ) ;
System. out. println ( s) ;
System. out. println ( s. toString ( ) ) ;
}
}
运行截图:
3.2 Object类的equals方法
equals 方法的作用
用于对象之间的比较,返回 true和false的结果 举例: s1.equals(s2); s1和s2是两个对象 重写 equals方法的场景 不希望比较对象的地址值,想要结合对象属性进行比较的时候。重写 equals方法的方式
alt + insert 选择equals() and hashCode(),IntelliJ Default,一路next,finish即可 在类的空白区域,右键 -> Generate -> 选择equals() and hashCode(),后面的同上。 示例代码:
class Student {
private String name;
private int age;
public Student ( ) {
}
public Student ( String name, int age) {
this . name = name;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
@Override
public boolean equals ( Object o) {
if ( this == o) return true ;
if ( o == null || getClass ( ) != o. getClass ( ) ) return false ;
Student student = ( Student) o;
if ( age != student. age) return false ;
return name != null ? name. equals ( student. name) : student. name == null;
}
}
public class ObjectDemo {
public static void main ( String[ ] args) {
Student s1 = new Student ( ) ;
s1. setName ( "林青霞" ) ;
s1. setAge ( 30 ) ;
Student s2 = new Student ( ) ;
s2. setName ( "林青霞" ) ;
s2. setAge ( 30 ) ;
System. out. println ( s1. equals ( s2) ) ;
}
}
4. 包装类
4.1 基本类型包装类
基本类型包装类的作用 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据 常用的操作之一:用于基本数据类型与字符串之间的转换基本数据类型包装类
基本数据类型 包装类 byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean
4.2 Integer类
Integer 类概述 包装一个对象中的原始类型 int 的值Integer 类构造方法
方法名 说明 public Integer(int value) 根据 int 值创建 Integer 对象(过时) public Integer(String s) 根据 String 值创建 Integer 对象(过时) public static Integer valueOf(int i) 返回表示指定的 int 值的 Integer 实例 public static Integer valueOf(String s) 返回一个保存指定值的 Integer 对象 String
public class IntegerDemo {
public static void main ( String[ ] args) {
Integer i1 = new Integer ( 100 ) ;
System. out. println ( i1) ;
Integer i2 = new Integer ( "100" ) ;
System. out. println ( i2) ;
System. out. println ( "--------" ) ;
Integer i3 = Integer. valueOf ( 100 ) ;
System. out. println ( i3) ;
Integer i4 = Integer. valueOf ( "100" ) ;
System. out. println ( i4) ;
}
}
4.3 自动拆箱和自动装箱
自动装箱 把基本数据类型转换为对应的包装类类型自动拆箱 把包装类类型转换为对应的基本数据类型示例代码
Integer i = 100 ;
i += 200 ;
5. int和String类型的相互转换
5.1 int 转换为String
转换方式 方式一:直接在数字后加一个空字符串 方式二:通过 String类静态方法valueOf()示例代码 String 转换为int转换方式 方式一:先将字符串数字转成 Integer,再调用valueOf()方法 方式二:通过 Integer静态方法parseInt()进行转换示例代码
public class IntegerDemo {
public static void main ( String[ ] args) {
int number = 100 ;
String s1 = number + "" ;
System. out. println ( s1) ;
String s2 = String. valueOf ( number) ;
System. out. println ( s2) ;
System. out. println ( "--------" ) ;
}
}
5.2 String 转换为int
转换方式 方式一:先将字符串数字转成 Integer,再调用valueOf()方法 方式二:通过 Integer静态方法parseInt()进行转换示例代码
public class IntegerDemo {
public static void main ( String[ ] args) {
String s = "100" ;
Integer i = Integer. valueOf ( s) ;
int x = i. intValue ( ) ;
System. out. println ( x) ;
int y = Integer. parseInt ( s) ;
System. out. println ( y) ;
}
}
5.3 字符串数据排序案例
案例需求 有一个字符串:“91 27 46 38 50”,请写程序实现最终输出结果是:“27 38 46 50 91”代码实现
public class IntegerTest {
public static void main ( String[ ] args) {
String s = "91 27 46 38 50" ;
String[ ] strArray = s. split ( " " ) ;
int [ ] arr = new int [ strArray. length] ;
for ( int i= 0 ; i< arr. length; i++ ) {
arr[ i] = Integer. parseInt ( strArray[ i] ) ;
}
Arrays. sort ( arr) ;
StringBuilder sb = new StringBuilder ( ) ;
for ( int i= 0 ; i< arr. length; i++ ) {
if ( i == arr. length - 1 ) {
sb. append ( arr[ i] ) ;
} else {
sb. append ( arr[ i] ) . append ( " " ) ;
}
}
String result = sb. toString ( ) ;
System. out. println ( result) ;
}
}
6. 时间工具类
6.1 Data类
Date 类概述 Date 代表了一个特定的时间,精确到毫秒Date 类构造方法
方法名 说明 public Date() 分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒 public Date(long date) 分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数
public class DateDemo01 {
public static void main ( String[ ] args) {
Date d1 = new Date ( ) ;
System. out. println ( d1) ;
long date = 1000 * 60 * 60 ;
Date d2 = new Date ( date) ;
System. out. println ( d2) ;
}
}
方法名 说明 public long getTime() 获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值 public void setTime(long time) 设置时间,给的是毫秒值
public class DateDemo02 {
public static void main ( String[ ] args) {
Date d = new Date ( ) ;
long time = System. currentTimeMillis ( ) ;
d. setTime ( time) ;
System. out. println ( d) ;
}
}
6.2 SimpleDateFormat类
SimpleDateFormat 类概述 SimpleDateFormat是一个具体的类,用于以区域设置敏感的方式格式化和解析日期 。SimpleDateFormat 类构造方法
方法名 说明 public SimpleDateFormat() 构造一个SimpleDateFormat,使用默认模式和日期格式 public SimpleDateFormat(String pattern) 构造一个SimpleDateFormat使用给定的模式和默认的日期格式
SimpleDateFormat 类的常用方法
格式化 (从Date到String) public final String format(Date date) :将日期格式化成日期/时间字符串解析 (从String到Date) public Date parse(String source) :从给定字符串的开始解析文本以生成日期 示例代码
public class SimpleDateFormatDemo {
public static void main ( String[ ] args) throws ParseException {
Date d = new Date ( ) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy年MM月dd日 HH:mm:ss" ) ;
String s = sdf. format ( d) ;
System. out. println ( s) ;
System. out. println ( "--------" ) ;
String ss = "2048-08-09 11:11:11" ;
SimpleDateFormat sdf2 = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
Date dd = sdf2. parse ( ss) ;
System. out. println ( dd) ;
}
}
6.3 日期工具类案例
案例需求 定义一个日期工具类(DateUtils),包含两个方法:把日期转换为指定格式的字符串;把字符串解析为指定格式 的日期,然后定义一个测试类(DateDemo),测试日期工具类的方法代码实现
public class DateUtils {
private DateUtils ( ) { }
public static String dateToString ( Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat ( format) ;
String s = sdf. format ( date) ;
return s;
}
public static Date stringToDate ( String s, String format) throws
ParseException {
SimpleDateFormat sdf = new SimpleDateFormat ( format) ;
Date d = sdf. parse ( s) ;
return d;
}
}
public class DateDemo {
public static void main ( String[ ] args) throws ParseException {
Date d = new Date ( ) ;
String s1 = DateUtils. dateToString ( d, "yyyy年MM月dd日 HH:mm:ss" ) ;
System. out. println ( s1) ;
String s2 = DateUtils. dateToString ( d, "yyyy年MM月dd日" ) ;
System. out. println ( s2) ;
String s3 = DateUtils. dateToString ( d, "HH:mm:ss" ) ;
System. out. println ( s3) ;
System. out. println ( "--------" ) ;
String s = "2048-08-09 12:12:12" ;
Date dd = DateUtils. stringToDate ( s, "yyyy-MM-dd HH:mm:ss" ) ;
System. out. println ( dd) ;
}
}
7. Calendar类
Calendar 类概述
Calendar 为特定瞬间与一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法 Calendar 提供了一个类方法 getInstance 用于获取这种类型的一般有用的对象。 该方法返回一个Calendar 对象。 其日历字段已使用当前日期和时间初始化:Calendar rightNow = Calendar.getInstance(); Calendar 类常用方法
方法名 说明 public int get(int field) 返回给定日历字段的值 public abstract void add(int field, int amount) 根据日历的规则,将指定的时间量添加或减去给定的日历字段 public final void set(int year,int month,int date) 设置当前日历的年月日
public class CalendarDemo {
public static void main ( String[ ] args) {
Calendar c = Calendar. getInstance ( ) ;
int year = c. get ( Calendar. YEAR) ;
int month = c. get ( Calendar. MONTH) + 1 ;
int date = c. get ( Calendar. DATE) ;
System. out. println ( year + "年" + month + "月" + date + "日" ) ;
间量添加或减去给定的日历字段
c. set ( 2050 , 10 , 10 ) ;
year = c. get ( Calendar. YEAR) ;
month = c. get ( Calendar. MONTH) + 1 ;
date = c. get ( Calendar. DATE) ;
System. out. println ( year + "年" + month + "月" + date + "日" ) ;
}
}