【学习笔记】常用类
API 简介
在了解具体的常用类之前,我们首先要知道什么是 API 。
API 是应用程序编程接口,在Java中,API指的是API文档,通常叫Java文档,是Java中提供的类的说明书。
Java中组件的层次结构:
模块(module) -> 包(package) -> 类或接口(class/interface)
那么什么是模块呢?
module, 自Java9起提供的一种新的Java基础组件,在包(package)的基础上有进行了一层封装,是包的容器。
-
JavaSE Modules
Java语言的核心类库,其下的模块名多以Javakaitou
-
JDK Modules
Java开发工具相关内容,其下的模块名多以JDK开头
在Oeacle官网可以查看Java API文档
Object 类
-
Object类是超类,也叫基类,所有的类直接或间接地继承它,它位于继承树的最顶层。
-
直接继承:任何的类没有直接写出extends Object 它都默认地继承了Object 类
-
间接继承:某一个类直接继承了Object 类,那么这个类的子类属于间接继承了Object 类
-
-
因为所有类都继承于Object 类,所以所有类都具备Object 类中所定义的方法
-
Object 类型可以存储任何对象
-
作为参数,可接受任何对象
-
作为返回值,可返回任何对象
-
-
Object 类中的常用方法
-
getClass() 返回this的运行时类
-
hashCode() 返回对象的哈希码值
-
toString() 返回对象的字符串表示形式
-
equals(Object obj) 指示某个其他对象是否等于此对象
-
finalize() 当垃圾收集确定不再有对该对象的引用时,由对象上的垃圾收集器调用。
-
getClass() 方法
-
public final Class<?> getClass(){}
-
返回引用中存储的实际对象类型
-
应用:通常用于判断两个对象是否是同一类型
package com.commonClass;
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
Teacher s3 = new Teacher();
Class class1 = s1.getClass();
Class class2 = s2.getClass();
Class class3 = s3.getClass();
System.out.println(class1);
System.out.println(class2);
System.out.println(class3);
if (class1 == class3){
System.out.println("类型相同");
}else{
System.out.println("类型不同");
}
}
}
hashCode() 方法
-
public int hashCode()
-
返回对象的哈希码值
-
哈希值根据对象的地址或字符串或数字使用hash算法计算出来的int 类型的数值
-
在创建对象时,会在堆中开辟一块空间,它所对应的有一个内存地址,根据该地址去计算哈希值
-
-
一般情况下相同对象返回相同哈希值
package com.commonClass;
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
//s1 和 s2 是不同的对象,它们的hashCode 肯定不相同
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
//把 s1 的值赋给 新创建的 s3 ,s3也指向 s1所实例化的对象,它的地址和s1的相同
Student s3 = s1;
System.out.println(s3.hashCode());
}
}
toString() 方法
-
public String toString(){}
-
返回该对象的字符串表示
-
可以根据程序需要重写父类的该方法
package com.commonClass;
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
我们可以看到,toString() 方法返回的是 类名 + 哈希值 (在此处哈希值为16进制,hashCode打印的哈希值为10进制)
可以查看toString() 方法的源码
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
当然,这不是我们所需要的,所以我们可以在子类中重写该方法
package com.commonClass;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.commonClass;
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student("张三",18);
Student s2 = new Student("李四",19);
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
equals() 方法
-
public boolean equals(Object obj){}
-
默认实现为(this == obj), 比较两个对象地址是否相同,从而比较两个对象的引用是否相同
-
可以进行重写,比较两个对象的内容是否相同
源码:
public boolean equals(Object obj) {
return (this == obj); //比较的是两个对象的地址
}
测试:
package com.commonClass;
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student("张三",18);
Student s2 = new Student("李四",19);
System.out.println(s1.equals(s2));
}
}
如果我们想比较两个对象的属性是否相同,使用该方法就不行了,如下:
package com.commonClass;
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student("张三",18);
Student s2 = new Student("李四",19);
System.out.println(s1.equals(s2));
Student s3 = new Student("李四",19);
System.out.println(s2.equals(s3));
}
}
从例子中我们可以看出,s2 和 s3 的属性相同,我们如何让equals 返回true呢?
这个时候我们就需要重写该方法
package com.commonClass;
import java.util.Objects;
public class Student {
private String name;
private int age;
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 obj) {
//判断两个对象是否是同一个引用
if (this == obj){
return true;
}
//判断obj是否为空
if (obj == null){
return false;
}
//判断两个对象是否是一个类型
if (obj instanceof Student){
//强制类型转换
Student s = (Student) obj;
//比较属性
if(this.name.equals(s.getName()) && this.age==s.getAge()){
return true;
}
}
return false;
}
}
finalize() 方法
-
当对象被判定为垃圾对象时,由JVM自动调用此方法,用以标记垃圾对象,进入回收队列
-
一般我们不自己去调用
-
垃圾对象:没有有效引用指向此对象时,为垃圾对象
-
比如 new Student(); 没有引用去指向它
-
当对象没有被使用,就要去回收,否则内存会溢出
-
-
垃圾回收:由GC销毁垃圾对象,释放数据空间
-
自动回收机制:JVM的内存耗尽,一次性回收所有垃圾对象
-
手动回收机制:使用System.gc(); 通知JVM执行垃圾回收
-
手动回收并不是真正的手动回收,而是通知垃圾回收器去回收
-
垃圾回收器去不去回收,还要看当前的运行情况决定
-
finalize() 方法源码:
protected void finalize() throws Throwable { }
我们发现这个方法里,没有东西,如果我们想要手动去通知垃圾回收器,就要重写该方法
package com.commonClass;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
protected void finalize() throws Throwable {
System.out.println(this.name + "垃圾对象被回收了");
}
}
package com.commonClass;
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student("张三",18);
Student s2 = new Student("李四",19);
Student s3 = new Student("王五",19);
//回收垃圾
System.gc();
System.out.println("回收垃圾");
}
}
通过结果可以看出,这三个对象没有被回收,所以它们不是垃圾对象
我们可以把它们的引用变量去掉,使它们变成垃圾对象
package com.commonClass;
public class TestStudent {
public static void main(String[] args) {
new Student("张三",18);
new Student("李四",19);
new Student("王五",19);
//回收垃圾
System.gc();
System.out.println("回收垃圾");
}
}
可以看到,这几个垃圾对象被回收了
String 类
String
-
字符串是常量,创建之后不可改变
-
字符串字面值存储在方法区中的字符串池中,可以共享 (方法区在JDK8之后叫元空间)
下面我们来看一下,字符串创建之后为什么不可改变,并且如何共享
String name = "hello"; //"hello" 常量存储在字符串池中
执行main方法,把main压入栈中,main中的局部变量name被创建,然后把hello 赋值给 name 那么就在字符串池中创建“hello”,并且name 指向 hello ,name中存储hello的地址
接下来我们看一下不可改变性
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String name = "hello";
name = "zhangsan"; //给字符串赋值时,没有修改数据,而是重新开辟空间
}
}
当把“zhangsan” 赋值给 name 时,并不是去修改“hello”的值,而是在字符串池中又开辟了一块空间为“zhangsan”,然后name指向“zhangsan”,原先的指向就断掉了,name的地址也修改为“zhangsan”的地址,“hello”如果没人用,就变成了垃圾
如果我们在创建一个变量,并赋值为zhangsan
String name2 = "zhangsan";
name2 也指向“zhangsan”,这就实现了字符串池常量的共享
-
除了上面创建字符串的方式,还有另一种方式
-
String str = new String("java");
-
这种方式,创建了两个对象
这种创建方式比较浪费空间
-
面试题
判断两个字符串是否相等
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String str = new String("java");
String str2 = new String("java");
System.out.println(str == str2);
}
}
答案是false
原因是,当你创建 str2 时,在堆中又创建了一个对象,它与str是不同的地址,所以它们并不相等
所以当我们比较两个字符串是否相等时,用equals() 方法,因为equals() 方法比较的不是地址,比较的是内容。
String 常用方法
1.length() 返回字符串的长度
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String content = "java是世界上最好的编程语言吗?";
System.out.println(content.length());
}
}
-
一个字符算一个长度,空格也算字符
2.chatAt(int index) 返回某个位置的字符
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String content = "java是世界上最好的编程语言吗?";
System.out.println(content.charAt(0));
System.out.println(content.charAt(content.length() - 1));
}
}
注意charAt()的参数,不要超出字符串的长度,下标是从0开始
String content = "java是世界上最好的编程语言吗?"; System.out.println(content.charAt(content.length()));
超出下标回报如下错误:
StringIndexOutOfBoundsException:
3.contains(String str) 判断是否包含某个子字符串
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String content = "java是世界上最好的编程语言吗?";
System.out.println(content.contains("java"));
System.out.println(content.contains("php"));
}
}
4.toCharArray() 返回字符串对应的数组
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String content = "java是世界上最好的编程语言吗?";
System.out.println(content.toCharArray());
}
}
5. indexOf() 返回子字符串首次出现的位置
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String content = "java是世界上最好的java编程语言吗?";
//首次出现的位置
System.out.println(content.indexOf("java"));
//从第几个位置往后,首次出现的位置
System.out.println(content.indexOf("java", 2));
}
}
6.lastIndexOf() 返回字符串最后一次出现的位置
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String content = "java是世界上最好的java编程语言吗?,java真香";
System.out.println(content.lastIndexOf("java"));
}
}
7.trim() 去掉字符串前后的空格
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String content = " hello world ";
System.out.println(content.trim());
}
}
8. toUpperCase() 小写转大写 toLowerCase() 大写转小写
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String content = " hello world ";
System.out.println(content.toUpperCase());
String content2 = "HELLO JAVA";
System.out.println(content2.toLowerCase());
}
}
9.endWith(String str) 判断是否以str结尾 startWith(String str) 判断是否以str开头
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String content = "hello.java";
System.out.println(content.endsWith("java"));
System.out.println(content.endsWith("zhang"));
System.out.println(content.startsWith("hello"));
}
}
10.replace(char old,char new) 用新的字符或字符串替换旧的字符或字符串
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String content = "hello.java";
System.out.println(content.replace("java", "world"));
}
}
11.split() 对字符串进行拆分
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String content = "java is the best programing language";
String[] arr = content.split(" ");
System.out.println(arr.length);
for (String str:arr) {
System.out.println(str);
}
}
}
我们是以 空格 来拆分的这个字符串,但如果我们想用 空格 也想用 逗号 来拆分怎么办呢
我们可以在参数中加一个 中括号 ,里面这样表示 ”[ ,]“ (空格+逗号)
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String content = "java is the best programing language,java very good";
String[] arr = content.split("[ ,]");
System.out.println(arr.length);
for (String str:arr) {
System.out.println(str);
}
}
}
如果我们不小心多打了一个或几个空格,怎么办呢
可以在中括号后面 加一个 加号 ”+“ ,这样表示可以连续出现一个或多个 空格或逗号
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String content = "java is the best programing language,java very good";
String[] arr = content.split("[ ,]+");
System.out.println(arr.length);
for (String str:arr) {
System.out.println(str);
}
}
}
12.equals() 比较两个字符串是否相等
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String str1 = "hello";
String str2 = "HELLO";
System.out.println(str1.equals(str2));
}
}
如果我们想要忽略大小写,可以用 equalsIgnoreCase()
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String str1 = "hello";
String str2 = "HELLO";
System.out.println(str1.equalsIgnoreCase(str2));
}
}
13.compareTo() 比较大小,实际上比较的是两个字符串在编码表里的顺序
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "xyz";
System.out.println(str1.compareTo(str2));
}
}
比较的是每个字符串第一个字符在编码表中的位置,a=97 x=120 ,97-120=-23
如果第一个字符相同,则去比较第二个字符
如果第二个字符串包含了第一个字符串,那么就不是比较在编码表里的顺序了,这样比较的是两个字符串的长度,第一个字符串的长度减去第二个字符串的长度
package com.commonClass.string;
public class Demo01 {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "abcxyz";
System.out.println(str1.compareTo(str2));
}
}
可变字符串
由于String的不可变性,程序运行时会产生一些无用的字符串,运行的效率也比较低
-
StringBuffer: 可变长字符串,由JDK1.0提供,运行效率慢,线程安全
-
会事先开辟缓冲区,之后在缓冲区上面操作
-
由于线程安全,它的运行效率较慢
-
-
StringBuilder:可变长字符串,由JDK5.0提供,运行效率快,线程不安全
-
它与StringBuffer在使用上完全一样,就是线程不安全
-
它的运行效率比StringBuffer要快
-
-
这两个类效率比String 高,比String节省内存
方法:
1.append() 追加
package com.commonClass.string;
public class Demo02 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("java是最好的语言");
System.out.println(sb.toString());
sb.append("java真香");
System.out.println(sb.toString());
}
}
2.insert() 添加/插入 它有两个参数,第一个参数是下标,表示在第几个字符前面插入数据,第二个参数是要插入的字符串。
package com.commonClass.string;
public class Demo02 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("java是最好的语言");
System.out.println(sb.toString());
sb.append("java真香");
System.out.println(sb.toString());
sb.insert(0,"我在这里");
System.out.println(sb.toString());
sb.insert(14,"哈哈哈");
System.out.println(sb.toString());
}
}
-
replace() 替换
它有三个参数 前两个参数是你要替换的字符串的范围,第三个参数是要替换的字符串的内容
package com.commonClass.string;
public class Demo02 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("java是最好的语言");
System.out.println(sb.toString());
sb.append("java真香");
System.out.println(sb.toString());
sb.insert(0,"我在这里");
System.out.println(sb.toString());
sb.replace(0,4,"hello");
System.out.println(sb.toString());
}
}
-
delete() 删除
package com.commonClass.string;
public class Demo02 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("java是最好的语言");
System.out.println(sb.toString());
sb.append("java真香");
System.out.println(sb.toString());
sb.insert(0,"我在这里");
System.out.println(sb.toString());
sb.replace(0,4,"hello");
System.out.println(sb.toString());
sb.delete(0,5);
System.out.println(sb.toString());
}
}
以上方法同样适用于StringBuilder(),在单线程的情况下,推荐用StringBuilder()
为什么StringBuilder/StringBuffer 比 String 的效率高呢
我们把0-1000 的 数字用字符串拼接起来,使用String 和 StringBuilder两种方式,并且获取这段代码的运行时间,以此来进行比较
String:
package com.commonClass.string;
public class Demo03 {
public static void main(String[] args) {
//开始时间
long start = System.currentTimeMillis();
String str = "";
for (int i = 0; i < 1000; i++) {
str += i;
}
System.out.println(str);
long end = System.currentTimeMillis();
System.out.println("用时:"+(end-start));
}
}
用时 22 ms
StringBuilder:
用时:1ms
所以看出 StringBuilder/StringBuffer 比 String 的效率高
BigDecimal 类
首先我们先看一个小题目
package com.commonClass.bigDecimal;
public class Demo01 {
public static void main(String[] args) {
double d1 = 1.0;
double d2 = 0.9;
System.out.println(d1-d2);
}
}
结果是 0.1 ? NO, 结果是这样的
为什么会出现这样的结果?原因就出在double上面
double/float 采用的是近似值的存储方式,也就是我们存的是1.0,但在内存中存的却是0.9999999999999,所以计算结果会出现一定的误差
我们想要计算结果精确就要使用精度更高的一个类 BigDecimal
BigDecimal 的存储是精确存储
下面我们用BigDecimal来解决上面的问题
package com.commonClass.bigDecimal;
import java.math.BigDecimal;
public class Demo01 {
public static void main(String[] args) {
BigDecimal d1 = new BigDecimal("1.0"); //这里我们要用字符串
BigDecimal d2 = new BigDecimal("0.9"); //这里我们要用字符串
BigDecimal result1 = d1.subtract(d2);
System.out.println("结果是:"+result1);
}
}
-
需要注意的是:我们创建BigDecimal对象时,传入的值一定要是字符串,因为你传入的不是字符串的话,它依旧是近似值存储。
-
BigDecimal提供的几种方法
-
subtract 减法
-
add 加法
package com.commonClass.bigDecimal; import java.math.BigDecimal; public class Demo01 { public static void main(String[] args) { BigDecimal d1 = new BigDecimal("1.0"); //这里我们要用字符串 BigDecimal d2 = new BigDecimal("0.9"); //这里我们要用字符串 BigDecimal result2 = d1.add(d2); System.out.println("加法结果是:"+result2); } }
-
multiply 乘法
package com.commonClass.bigDecimal; import java.math.BigDecimal; public class Demo01 { public static void main(String[] args) { BigDecimal d1 = new BigDecimal("1.0"); //这里我们要用字符串 BigDecimal d2 = new BigDecimal("0.9"); //这里我们要用字符串 BigDecimal result2 = d1.multiply(d2); System.out.println("乘法结果是:"+result2); } }
-
divide 除法
package com.commonClass.bigDecimal; import java.math.BigDecimal; public class Demo01 { public static void main(String[] args) { BigDecimal d1 = new BigDecimal("1.0"); //这里我们要用字符串 BigDecimal d2 = new BigDecimal("2.0"); //这里我们要用字符串 BigDecimal result2 = d1.divide(d2); System.out.println("除法结果是:"+result2); } }
-
在除法这里有一点问题,如果除不尽就会报错 比如 10/3
这时候我们可以这样解决
divide 有一些重载方法, divide(BigDecimal d2, int scale, int roundingMode)
package com.commonClass.bigDecimal;
import java.math.BigDecimal;
public class Demo01 {
public static void main(String[] args) {
BigDecimal d1 = new BigDecimal("10"); //这里我们要用字符串
BigDecimal d2 = new BigDecimal("3"); //这里我们要用字符串
BigDecimal result2 = d1.divide(d2,2,BigDecimal.ROUND_HALF_UP);
System.out.println("除法结果是:"+result2);
}
}
int scale ---------- 表示保存两位小数
int roundingMode ------------ 保留的方式 BigDecimal.ROUND_HALF_UP 表示四舍五入
-
面试题
(1.4-0.5)/ 0.9
package com.commonClass.bigDecimal;
import java.math.BigDecimal;
public class Demo01 {
public static void main(String[] args) {
BigDecimal result2 = new BigDecimal("1.4")
.subtract(new BigDecimal("0.5"))
.divide(new BigDecimal("0.9"));
System.out.println("结果是:"+result2);
}
}
Date 类
-
Date 表示特定的瞬间,精确到毫秒,在JDK1.1之后,Date类中的大部分方法都已经被Calendar类中的方法所取代
-
时间单位
-
1秒 = 1000毫秒
-
1毫秒 = 1000微秒
-
1000微秒 = 1000纳秒 纳秒也叫毫微秒
-
-
Date类中还未过时的两个构造方法
-
Date() 分配Date 对象并初始化此对象,以表示分配它的时间(精确的到毫秒)
package com.commonClass.date; import java.util.Date; public class Demo01 { public static void main(String[] args) { Date date1 = new Date(); System.out.println(date1.toString()); System.out.println(date1.toLocaleString()); } }
-
Date(long date) 分配Date 对象并初始化此对象,参数是表示自从标准基准时间,即1970年1月1日 00:00:00, 以来的毫秒数 这个时间是Unix 元年
-
package com.commonClass.date;
import java.util.Date;
public class Demo01 {
public static void main(String[] args) {
long da = 2022722;
Date date2 = new Date(da);
System.out.println(date2);
}
}
-
Date 类中 还未过时的方法 after、before
-
after 表示第一个时间是否在第二个时间之后,是返回true,否 返回false
-
before 表示第一个时间是否在第二个时间之前,是返回true,否 返回false
-
package com.commonClass.date;
import java.util.Date;
public class Demo01 {
public static void main(String[] args) {
Date date1 = new Date(); //今天
Date date2 = new Date(date1.getTime() - (60*60*24*1000)); //昨天
boolean result = date1.after(date2);
System.out.println("date1是否在date2之后:"+result);
boolean result2 = date1.before(date2);
System.out.println("date1是否在date2之前:"+result2);
}
}
-
方法getTime() 获取当前时间到1970年1月1日 00:00:00 的毫秒数
-
compareTo() 比较的是毫秒值,前一个时间的毫秒值减去后一个时间的毫秒值,为正数返回1,负数返回 -1 相等返回0
package com.commonClass.date;
import java.util.Date;
public class Demo01 {
public static void main(String[] args) {
Date date1 = new Date(); //今天
Date date2 = new Date(date1.getTime() - (60*60*24*1000)); //昨天
int d1 = date1.compareTo(date2);
int d2 = date2.compareTo(date1);
int d3 = date1.compareTo(date1);
System.out.println(d1);
System.out.println(d2);
System.out.println(d3);
}
}
-
equals() 比较两个时间是否相等,相等返回true,不相等返回false
package com.commonClass.date;
import java.util.Date;
public class Demo01 {
public static void main(String[] args) {
Date date1 = new Date(); //今天
Date date2 = new Date(date1.getTime() - (60*60*24*1000)); //昨天
boolean d1 = date1.equals(date2);
boolean d2 = date2.equals(date2);
System.out.println(d1);
System.out.println(d2);
}
}
Calendar 类
-
提供了获取或设置各种日历字段的方法
-
构造方法 protected Calendar()
-
由于修饰符是protected,所以无法直接创建对象
-
所以我们可以用这个类中的静态方法 static Calendar getInstance()
-
这个方法是使用默认时区和区域来获取日历
-
-
package com.commonClass.date;
import java.util.Calendar;
public class Demo02 {
public static void main(String[] args) {
//创建Calendar 对象
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime().toString());
}
}
-
其他的方法
-
int get(int file)
-
返回给定日历字段的值,字段比如年、月、日等
-
在Calendar类中,有许多常量,我们可以使用这些常量来作为参数
-
package com.commonClass.date; import java.util.Calendar; public class Demo02 { public static void main(String[] args) { //创建Calendar 对象 Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime().toString()); //年 int year = calendar.get(Calendar.YEAR); //月 int month = calendar.get(Calendar.MONTH); //月份是从0-11,所以要表示月份还要在加1 //日 int day = calendar.get(Calendar.DAY_OF_MONTH); //天可以用DATA 也可以用 DAY_OF_MONTH //小时 int hour = calendar.get(calendar.HOUR_OF_DAY); //HOUR_OF_DAY 表示24小时制,HOUR 表示12小时制 //分钟 int minute = calendar.get(calendar.MINUTE); //秒 int second = calendar.get(calendar.SECOND); System.out.println(year+"年"+(month+1)+"月"+day+"日"+hour+":"+minute+":"+second); } }
-
void set(int year,int month,int date,int hourofday,int minute,int second)
-
设置日历的年月日时分秒
-
set有许多重载方法,可以设置某一个值,也可以选择性设置
-
package com.commonClass.date; import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; import java.util.Calendar; public class Demo02 { public static void main(String[] args) { //创建Calendar 对象 Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime().toString()); Calendar calendar1 = Calendar.getInstance(); calendar1.set(calendar1.DAY_OF_MONTH,21); System.out.println(calendar1.getTime().toLocaleString()); } }
-
void add(int field,int amount)
-
按照日历的规则,给指定字段添加或减少时间量
-
package com.commonClass.date; import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; import java.util.Calendar; public class Demo02 { public static void main(String[] args) { //创建Calendar 对象 Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime().toLocaleString()); calendar.add(Calendar.HOUR_OF_DAY,1); //加1小时 System.out.println(calendar.getTime().toLocaleString()); } }
-
getActualMaximum(int field) getActualMinimum(int field)
-
获取某个时间字段中的最大值和最小值
package com.commonClass.date; import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; import java.util.Calendar; public class Demo02 { public static void main(String[] args) { //创建Calendar 对象 Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime().toLocaleString()); int max = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); int min = calendar.getActualMinimum(Calendar.DAY_OF_MONTH); System.out.println("这个月天数的最大值:"+max); System.out.println("这个月天数的最小值:"+min); } }
-
SimpleDateFormat 类
-
-
SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类
-
进行格式化(日期 -> 文本) 、解析(文本 -> 日期)
-
常用的时间模式字母
字母 日期或事件 示例 y 年 2019 M 年中月份 08 d 月中天数 10 H 1天中小时数(0-23) 22 m 分钟 16 s 秒 59 S 毫秒 655 -
把日期格式化为字符串
package com.commonClass.date; import java.text.SimpleDateFormat; import java.util.Date; public class Demo03 { public static void main(String[] args) { //创建SimpleDateFormat对象 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //创建Date Date date = new Date(); //格式化date String str = sdf.format(date); System.out.println(str); } }
-
把字符串解析成日期
package com.commonClass.date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Demo03 { public static void main(String[] args) throws ParseException { //创建SimpleDateFormat对象 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //创建Date Date date = new Date(); //格式化date(把日期转成字符串) String str = sdf.format(date); System.out.println(str); //解析(把字符串转成日期) Date date1 = sdf.parse("2019/09/07 11:11:11"); System.out.println(date1); } }
-
注意:解析字符串时,输入的字符串一定要符合上面规定的格式,否则会报错
System 类
-
System系统类,主要用于获取系统的属性数据和其他操作,构造方法私有
-
System中的方法
-
static void arraycopy(src,srcPos,dest,destPos,length) 复制数组
-
src:源数组
-
srcPos:从源数组的哪个位置开始复制
-
dest:目标数组
-
destPos: 把它放在目标数组的哪个位置
-
length:复制的长度
-
package com.commonClass.system; public class Demo01 { public static void main(String[] args) { int[] arr = {12,15,56,89,23,5,64}; int[] destArr = new int[7]; System.arraycopy(arr,0,destArr,3,3); for (int i: destArr) { System.out.println(i); } } }
Arrays.copyOf() 就是使用的arraycopy 来实现的
-
static long currentTimeMillis() 获取当前系统时间,返回的是毫秒值
-
是从1970年1月1日 00:00:00 到现在的毫秒数
-
package com.commonClass.system; public class Demo01 { public static void main(String[] args) { System.out.println(System.currentTimeMillis()); } }
这个方法可以用来计算代码运行的时间
package com.commonClass.system; public class Demo01 { public static void main(String[] args) { long start = System.currentTimeMillis(); for (int i = 0; i < 999999999; i++) { for (int j = 0; j < 999999999; j++) { int result = i+j; } } long end = System.currentTimeMillis(); System.out.println("用时:" + (end-start)); } }
-
static void gc() 告诉垃圾回收器回收垃圾 具体演示在 Object类的 finalize() 方法 中
-
static void exit(int status)
-
退出JVM,如果参数是0,则正常退出,非0表示异常退出
package com.commonClass.system; public class Demo01 { public static void main(String[] args) { System.out.println("程序正在运行....."); System.exit(0); System.out.println("程序退出了.."); } }
-
-