概念:在一个类的内部再定义一个完整的类;
特点:
概念:在类的内部定义,和实例变量和实例方法同级的类;
作为外部类的一部分,在创建内部类的对象时,必须依赖外部类对象;
成员内部类中是不能定义静态成员的,但可以包含静态的常量private static final String name;
这样;
//Test类
public class Test {
private String name = "柴玲玲";
private int age = 21;
class Inner {
private String address = "甘肃";
private int phone = 120;
public void show() {
System.out.println(name);
System.out.println(age);
System.out.println(address);
System.out.println(phone);
}
}
}
//Demo 类
public class Demo {
public static void main(String[] args) {
//Test test = new Test();
//Test.Inner inner = test.new Inner();
Test.Inner inner = new Test().new Inner();
inner.show();
}
}
/*
柴玲玲
21
甘肃
120
*/
注意:当内部类和外部类存在重名属性的时候,优先访问内部类的属性,此时若想访问外部类的这个属性,则需要外部类名称.this.属性
才可以访问;
//Test类
public class Test {
private String name = "柴玲玲";
private int age = 21;
class Inner {
private String address = "甘肃";
private int phone = 120;
private String name = "李四";
public void show() {
System.out.println(name);//访问内部类的name
System.out.println(Test.this.name);//访问外部类的属性
System.out.println(age);
System.out.println(address);
System.out.println(phone);
}
}
}
//Demo类
public class Demo {
public static void main(String[] args) {
//Test test = new Test();
//Test.Inner inner = test.new Inner();
Test.Inner inner = new Test().new Inner();
inner.show();
}
}
/*
李四
柴玲玲
21
甘肃
120
*/
概念:不依赖外部类的对象,可直接创建或通过类名访问,可声明静态成员;(也就是说相当于一个外部类,级别和外部类相同外部类可以包含什么,这个内部类就可以包含什么)
//Test
public class Test {
private String name = "柴玲玲";
private int age = 21;
static class Inner {
private String address = "甘肃";
private int phone = 120;
private String name = "李四";
private static int count = 1000;//静态成员
public void show() {
System.out.println(name);//访问内部类的name
/*访问外部类的成员*/
Test test = new Test();
System.out.println(test.name);
System.out.println(test.age);
System.out.println(address);
System.out.println(phone);
System.out.println(count);
}
}
}
//Demo
public class Demo {
public static void main(String[] args) {
Test.Inner inner = new Test.Inner();//注意和成员内部类中的区别
inner.show();
}
}
/*
李四
柴玲玲
21
甘肃
120
1000
*/
注意:只有内部类才可以有static修饰,一般的类不能有static修饰;
概念:定义在外部类的方法中,作用范围和创建对象范围仅限于当前方法;
局部内部类不能加任何访问修饰符;
局部内部类访问外部类当前方法中的局部变量的时候,因无法保证变量的生命周期与自身相同,变量必须修饰为 final;
//Demo02
package OOP.Class;
public class Demo02 {
private String name = "dl";
private int age = 21;
public void show() {
String address = "辽宁";
//局部内部类
class Inner {
private String phone = "122";
private String email = "[email protected]";
public void test() {
//访问
System.out.println(Demo02.this.name);
System.out.println(Demo02.this.age);
System.out.println(this.phone);
System.out.println(this.email);
//局部变量 address 前面省略了 final 他其实是一个常量
System.out.println(address);
}
}
Inner inner = new Inner();
inner.test();
}
}
//Demo02_1
package OOP.Class;
public class Demo02_1 {
public static void main(String[] args) {
Demo02 demo02 = new Demo02();
demo02.show();
}
}
概念:没有类名的局部内部类(一切属性均和局部内部类相同);
接口不能实例化,但是通过匿名内部类可以实现实例化;
//Demo01 接口
package OOP.Class_1;
public interface Demo01 {
void service();
}
//Dmmo
package OOP.Class_1;
public class Dmmo {
public static void main(String[] args) {
Demo01 demo01 = new Demo01() {
@Override
public void service() {
System.out.println("连接成功了!!!");
}
};
demo01.service();
}
}
必须继承一个父类或者实现一个接口;
定义类,实现类,创建对象的语法合并,只能创建一个该类的对象;
超类、基类,所有类的直接或者间接父类,位于继承树的最顶层;
任何类,如果没有书写extends显示继承某个类,都默认直接继承Object类,否则为间接继承;
Object类可以存储任何对象:
public final Class> getClass() {}
应用:通常用于判断两个引用中实际存储对象类型是否一致;
//Student
package OOP.Class_2;
public 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;
}
}
//People
package OOP.Class_2;
public class People {
public static void main(String[] args) {
Student student = new Student("aaa", 11);
Student student1 = new Student();
Class s1 = student.getClass();
Class s2 = student1.getClass();
if(s1 == s2) {
System.out.println("是同一种类型");
}else{
System.out.println("不是同一种类型");
}
}
}
public int hashCode() {}
一般情况相同的对象返回相同的哈希码;
哈希值根据对象的地址或字符串或数字使用hash算法计算出来的int类型的数值;
//Student
package OOP.Class_2;
public 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;
}
}
//People
package OOP.Class_2;
public class People {
public static void main(String[] args) {
Student student = new Student("aaa", 11);
Student student1 = new Student();
Student student3 = student;
System.out.println(student.hashCode());
System.out.println(student1.hashCode());
System.out.println(student3.hashCode());
}
}
public String toString() {}
//Student
package OOP.Class_2;
public 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;
}
}
//People
package OOP.Class_2;
public class People {
public static void main(String[] args) {
Student student = new Student("aaa", 11);
Student student1 = new Student();
Student student3 = student;
System.out.println(student.hashCode());
System.out.println(student1.hashCode());
System.out.println(student3.hashCode());//十进制显示
System.out.println("=================");
System.out.println(student.toString());//十六进制显示
System.out.println(student1.toString());
}
}
/*
2129789493
668386784
2129789493
=================
OOP.Class_2.Student@7ef20235
OOP.Class_2.Student@27d6c5e0
*/
可以根据程序的需求覆盖该方法,如:展示对象的各个属性值;
//Student
package OOP.Class_2;
public 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 String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
//People
package OOP.Class_2;
public class People {
public static void main(String[] args) {
Student student = new Student("aaa", 11);
Student student1 = new Student();
Student student3 = student;
System.out.println(student.hashCode());
System.out.println(student1.hashCode());
System.out.println(student3.hashCode());
System.out.println("=================");
System.out.println(student.toString());
System.out.println(student1.toString());
}
}
/*
2129789493
668386784
2129789493
=================
Student{name='aaa', age=11}
Student{name='null', age=0}
*/
public boolean equals(Object obj) {}
//Teacher
package OOP.Class_3;
public interface Teacher {
void show();
}
//TeacherImple
package OOP.Class_3;
public class TeacherImple implements Teacher{
private String name;
private int age;
public TeacherImple() {
}
public TeacherImple(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 "TeacherImple{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public void show() {
}
}
//People
package OOP.Class_3;
public class People {
public static void main(String[] args) {
TeacherImple teacherImple = new TeacherImple("丁", 21);
TeacherImple teacherImple1 = new TeacherImple("柴", 21);
TeacherImple teacherImple2 = new TeacherImple("丁", 21);
TeacherImple teacherImple3 = teacherImple;
System.out.println(teacherImple.equals(teacherImple1));//false
System.out.println(teacherImple.equals(teacherImple2));//false
System.out.println(teacherImple.equals(teacherImple3));//true
}
}
可进行覆盖,比较两个对象的内容是否相同;
equals()方法覆盖步骤:
//Teacher
package OOP.Class_3;
public interface Teacher {
void show();
}
//TeacherImple
package OOP.Class_3;
public class TeacherImple implements Teacher{
private String name;
private int age;
public TeacherImple() {
}
public TeacherImple(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 "TeacherImple{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null) return false;
if(obj instanceof TeacherImple) {
TeacherImple teacher = (TeacherImple)obj;
if(teacher.name.equals(this.name) == true && teacher.age == this.age) {
return true;
}
}
return false;
}
@Override
public void show() {
}
}
//People
package OOP.Class_3;
public class People {
public static void main(String[] args) {
TeacherImple teacherImple = new TeacherImple("丁龙", 21);
TeacherImple teacherImple1 = new TeacherImple("柴玲玲", 21);
TeacherImple teacherImple2 = new TeacherImple("丁龙", 21);
TeacherImple teacherImple3 = teacherImple;
System.out.println(teacherImple.equals(teacherImple1));//false
System.out.println(teacherImple.equals(teacherImple2));//true
System.out.println(teacherImple.equals(teacherImple3));//true
}
}
当对象被判定为垃圾对象的时候,由JVM自动调用此方法,用以标记垃圾对象,进入回收队列;
垃圾对象:没有有效引用指向此对象,为垃圾对象;
垃圾回收:由GC销毁垃圾对象,释放数据存储空间;
自动回收机制:JVM的内存耗尽,一次性回收所有的垃圾对象;
手动回收机制:使用System.gc();通知JVM执行垃圾回收;
概念:基本数据类型所对应的引用类型;
基本数据类型 | 包装类型 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
装箱:把基本类型转换成引用类型;
拆箱:把引用类型转换成基本类型;
//Demo
package OOP.Class_4;
public class Demo {
public static void main(String[] args) {
//包装
int num = 1;
//第一种 构造方法
Integer integer = new Integer(num);
//第二种 valueof
Integer integer1 = Integer.valueOf(num);
System.out.println("装箱");
System.out.println(integer);
System.out.println(integer1);
//拆装
Integer integer2 = new Integer(100);
int num1 = integer2.intValue();
System.out.println("拆箱");
System.out.println(num1);
//JDK1.6 以后采用自动拆装 其实是系统帮咋们做
int num2 = 1;
Integer integer3 = num2;
System.out.println("自动装箱");
System.out.println(integer3);
int num3 = integer3;
System.out.println("自动拆箱");
System.out.println(num3);
}
}
/*
装箱
1
1
拆箱
100
自动装箱
1
自动拆箱
1
*/
注意:自动装箱、拆箱,调用的是Integer.valueof()
;
//Demo01
package OOP.Class_4;
public class Demo01 {
public static void main(String[] args) {
//把基本类型转换成字符串
int num = 10;
//第一种 + ""
String s1 = num + "";
System.out.println(s1);
//第二种 toString
String s2 = Integer.toString(num);
System.out.println(s2);
//按进制转换 十六进制 八进制 二进制
String s3 = Integer.toBinaryString(num);//十六进制 八进制 Integer.toHexString(); Integer.toOctalString();
System.out.println("二进制显示:" + s3);
//带基数的转换
String s4 = Integer.toString(num, 16);//后面的基数代表的是进制
System.out.println(s4);
//把字符类型转换成基本类型
//Integer.parseXXX()
String str = "150";
int num1 = Integer.parseInt(str);
System.out.println(num1);
//boolean字符串形式转换成基本类型boolean
//只有是 true 的时候转换后才是 true 非 true 的时候转换的都是 false
String str1 = "true";
String str2 = "cll";
boolean boolean1 = Boolean.parseBoolean(str1);
boolean boolean2 = Boolean.parseBoolean(str2);
System.out.println(boolean1);
System.out.println(boolean2);
}
}
/*
10
10
二进制显示:1010
a
150
true
false
*/
注意:要保证类型的兼容,否则会产生异常抛出的NumberFormatException
;
Java预先创建了256个常用的整数包装类型对象;
//Demo03
package OOP.Class_4;
public class Demo03 {
public static void main(String[] args) {
Integer integer = new Integer(100);
Integer integer1 = new Integer(100);
System.out.println(integer == integer1);//false
Integer integer2 = 101;
Integer integer3 = 101;
System.out.println(integer2 == integer3);//true
Integer integer4 = 200;
Integer integer5 = 200;
System.out.println(integer4 == integer5);//false
}
}
/*
false
true
false
*/
注意:这里的Integer integer3 = 101
是自动重装功能,也就是系统帮我们创建好了Integer integer3 = Integer.valueof(101)
,并没有使用构造函数所 new,但是当数值为200的时候就不可以了,因为这个时候就是使用了 new,虽然也是自动重装,主要是因为Java预先创建了256个常用的整数包装类型对象它的数值是 -128~127,当超过这个数值区间的时候,就会使用 new自己创建了,源码在下;
@HotSpotIntrinsicCandidate
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
看源码就会明白上面所述的问题了;
字符串是常量,创建之后不可改变;
字符串的字面值存储在字符串池中,可以共享;
String 创建的字符串存储在公共池中(在方法区),而 new 创建的字符串对象在堆上:
String s = "Hello"
;产生一个对象,字符串池中保存;当后面的赋值相同的时候,会指向同一个字符串池中的对象;这样创建的时候,会事先在字符串池中扫码是否有同一个,没有则创建,有的话直接用(地址也是相同的);
//Demo01
package OOP.Class_5;
public class Demo {
public static void main(String[] args) {
String name = "dl";
String name1 = "dl";
String name2 = "cll";
System.out.println(name + " " + name1 + " " + name2);
}
}
String s = new String(“Hello”)
;产生两个对象,堆、池各存储一个;当后面的赋值相同的时候,实际上是再次创建了一个对象,堆里面的不相同,字符串池中的相同;
//Demo01
package OOP.Class_5;
public class Demo {
public static void main(String[] args) {
String name = new String("dl");
String name1 = new String("dl");
String name2 = new String("cll");
System.out.println(name + " " + name1 + " " + name2);
}
}
String s1 = "Runoob"; // String 直接创建
String s2 = "Runoob"; // String 直接创建
String s3 = s1; // 相同引用
String s4 = new String("Runoob"); // String 对象创建
String s5 = new String("Runoob"); // String 对象创建
//Demo
package OOP.Class_5;
import java.nio.charset.StandardCharsets;
public class Demo {
public static void main(String[] args) {
String name = new String("dl");
String name1 = new String("dl");
System.out.println(name == name1);//false
System.out.println(name.equals(name1));//true
String name2 = "dl";
String name3 = "dl";
/*注意下面的一句*/
System.out.println(name2 == name3);//true
System.out.println(name2.equals(name3));//true
}
}
/*
false
true
true
true
*/
public int length()
;返回字符串的长度
public char charAt(int index)
;根据下标获取字符
public boolean contains(String str)
;判断当前字符串中是否包含str
//Demo02
package OOP.Class_5;
public class Demo02 {
public static void main(String[] args) {
String name = "我最爱 cll 要永远和她在一起";
System.out.println(name.length());
System.out.println(name.charAt(0));
System.out.println(name.contains("cll"));
}
}
/*
16
我
true
*/
public char[] toCharArray()
;将字符串转换成数组
public int indexOf(String str)
;查找str首次在字符串中出现的下标,若存在,则输出下标,若不存在则返回-1
public int lastIndexOf(Steing str)
;查找str在字符串中出现的最后一次下标索引
//Demo02
package OOP.Class_5;
import java.util.Arrays;
public class Demo02 {
public static void main(String[] args) {
String name = "我最爱 cll and cll 要永远和她在一起 cll";
System.out.println(Arrays.toString(name.toCharArray()));
System.out.println(name.indexOf("cll"));//找第一个 cll
System.out.println(name.lastIndexOf("cll"));//找最后一个 cll
System.out.println(name.indexOf("cll", 7));//找第二个 cll
}
}
/*
[我, 最, 爱, , c, l, l, , a, n, d, , c, l, l, , 要, 永, 远, 和, 她, 在, 一, 起, , c, l, l]
4
25
12
*/
public String trim()
;去掉字符串前后的空格
public String toUpperCase()
;将小写转换为大写
public String toLowerCase()
;将大写转换为小写
public boolean endsWith(String str)
;判断字符串是否以str结尾
public boolean startsWith(String str)
;判断字符串是否以str开头
//Demo03
package OOP.Class_5;
public class Demo03 {
public static void main(String[] args) {
String name = " 我最爱 cll and cll 要永远和她在一起 CLL";
System.out.println(name.trim());
System.out.println(name.toUpperCase());
System.out.println(name.toLowerCase());
System.out.println(name.endsWith("CLL"));
System.out.println(name.startsWith("cll"));
}
}
/*
我最爱 cll and cll 要永远和她在一起 CLL
我最爱 CLL AND CLL 要永远和她在一起 CLL
我最爱 cll and cll 要永远和她在一起 cll
true
false
*/
public String replace(char oldChar, char newChar)
;将旧字符转换成新字符
public String[] split(String str)
;根据str做拆分
package OOP.Class_5;
import java.util.Arrays;
public class Demo04 {
public static void main(String[] args) {
String name = " 我最爱 cll and cll 要永远和她在一起 C,LL";
System.out.println(name.replace("LL", "DL"));
String[] cll = name.split(" ");
for(String k : cll) {
System.out.println(k);
}
}
}
/*
我最爱 cll and cll 要永远和她在一起 C,DL
我最爱
cll
and
cll
要永远和她在一起
C,LL
*/
要是拆分的时候是多选,比如是按照空格或者,都可以进行拆分,则需要split([ ,])
;
package OOP.Class_5;
import java.util.Arrays;
public class Demo04 {
public static void main(String[] args) {
String name = " 我最爱 cll and cll 要永远和她在一起 C,LL";
System.out.println(name.replace("LL", "DL"));
String[] cll = name.split("[ ,]");
for(String k : cll) {
System.out.println(k);
}
}
}
/*
我最爱 cll and cll 要永远和她在一起 C,DL
我最爱
cll
and
cll
要永远和她在一起
C
LL
*/
当其中有多个空格的时候,想要拆分输出的时候不会出现上述的空格占一行的情况的话,可以使用split([ ,]+)
;
package OOP.Class_5;
import java.util.Arrays;
public class Demo04 {
public static void main(String[] args) {
String name = " 我最爱 cll and cll 要永远和她在一起 C,LL";
System.out.println(name.replace("LL", "DL"));
String[] cll = name.split("[ ,]+");
for(String k : cll) {
System.out.println(k);
}
}
}
/*
我最爱 cll and cll 要永远和她在一起 C,DL
我最爱
cll
and
cll
要永远和她在一起
C
LL
*/
equalsIgnoreCase
是忽略大小写的比较是否相同;
//Demo05
package OOP.Class_5;
public class Demo05 {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO";
System.out.println(s1.equals(s2));//false
System.out.println(s1.equalsIgnoreCase(s2));//true
String s3 = "abc";
String s4 = "def";
System.out.println(s3.compareTo(s4));//-3
String s5 = "abcaaa";
System.out.println(s3.compareTo(s5));//-3
String s6 = " abc";
System.out.println(s3.compareTo(s6));//65
}
}
//Demo06
package OOP.Class_5;
public class Demo06 {
public static void main(String[] args) {
String str = "this is a text";
String[] arr = str.split(" ");
for(String k : arr) {
System.out.println(k);
}
System.out.println(str.replace("text", "practice"));
System.out.println(str.replace("text", "easy text"));
for(int i = 0; i < arr.length; i ++) {
char a = arr[i].charAt(0);
char A = Character.toUpperCase(a);
arr[i] = A + arr[i].substring(1);
System.out.print(arr[i] + " ");
}
}
}
/*
this
is
a
text
this is a practice
this is a easy text
This Is A Text
*/
和String的区别:
StringBuffer:可变长字符串,JDK1.0提供,运行效率慢,线程安全;
StringBuilder:可变长字符串,JDK5.0提供,运行效率快,线程不安全;
方法演示:
//Demo01
package OOP.Class_6;
public class Demo01 {
public static void main(String[] args) {
//StringBuffer str = new StringBuffer();
StringBuilder str = new StringBuilder();
//追加append()
str.append("我爱 cll");
System.out.println(str.toString());
str.append("I Love cll");
System.out.println(str.toString());
//添加insert()
str.insert(6, " DL ");
System.out.println(str.toString());
//替换replace
str.replace(3, 6, "CLL");
System.out.println(str.toString());
//删除delete
str.delete(9, 12);
System.out.println(str.toString());
//反转reverse()
str.reverse();
System.out.println(str.toString());
//清空
str.delete(0, str.length());
System.out.println(str.length());
}
}
/*
我爱 cll
我爱 cllI Love cll
我爱 cll DL I Love cll
我爱 CLL DL I Love cll
我爱 CLL DLLove cll
llc evoLLD LLC 爱我
0
*/
运行效率比较:
//String
package OOP.Class_6;
public class Demo02 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
String str = "";
for(int i = 0; i < 99999; i ++) {
str += i;
}
System.out.println(str);
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
/*
4689
*/
//StringBuilder
package OOP.Class_6;
public class Demo02 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
StringBuilder str = new StringBuilder();
for(int i = 0; i < 99999; i ++) {
str.append(i);
}
System.out.println(str.toString());
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
/*
35
*/
作用:精确计算浮点数
import java.math.BigDecimal;
BigDecimal add(BigDecimal db);//加
BigDecimal subtract(BigDecimal db);//减
BigDecimal multiply(BigDecimal db);//乘
BigDecimal divide(BigDecimal db);//除
package OOP.Class_6;
public class Demo03 {
public static void main(String[] args) {
double num1 = 0.9;
double num2 = 0.8;
System.out.println(num1 - num2);//0.09999999999999998
double result = (1.4 - 0.5) / 0.9;
System.out.println(result);//0.9999999999999999
}
}
/*
0.09999999999999998
0.9999999999999999
*/
double是近似值存储;可以使用BigDecimal来解决这个问题;
package OOP.Class_6;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class Demo03 {
public static void main(String[] args) {
BigDecimal num1 = new BigDecimal("1.0");
BigDecimal num2 = new BigDecimal("0.9");
BigDecimal result = num1.subtract(num2);
System.out.println(result);//0.1
BigDecimal divide = new BigDecimal("1.4").subtract(new BigDecimal("0.5")).divide(new BigDecimal("0.9"));
System.out.println(divide);//1
BigDecimal divide1 = new BigDecimal("20").divide(new BigDecimal("3"), 2, RoundingMode.HALF_UP);
System.out.println(divide1);//6.67
}
}
/*
0.1
1
6.67
*/
package OOP.Class_6;
import java.util.Date;
public class Demo04 {
public static void main(String[] args) {
Date date1 = new Date();
System.out.println(date1.toString());
System.out.println(date1.toLocaleString());
Date date2 = new Date(date1.getTime() - 60 * 60 * 12 * 1000);
System.out.println(date2.toLocaleString());
System.out.println(date1.after(date2));
System.out.println(date1.before(date2));
System.out.println(date1.compareTo(date2));
System.out.println(date1.equals(date2));
}
}
/*
Sun Apr 03 10:15:12 CST 2022
2022年4月3日 上午10:15:12
2022年4月2日 下午10:15:12
true
false
1
false
*/
protected Calendar()
由于修饰符是protected,所以无法直接创建该对象方法名 | 说明 |
---|---|
static Calendar() | 设置默认时区和区域获取日历 |
void set(int year, int mouth, int date, int hourofday, int minute, int second) | 设置日历的年月日时分秒 |
int get(int field) | 返回给定日历字段的值,字段比如年月日等 |
void setTime(Date date) | 用给定的Date设置此日历的时间 |
Date getTime() | 返回一个Date表示日历的时间 |
void add(int field, int amount) | 按照日立的规则,给指定字段添加或减少时间量 |
long getTimeInMillies() | 毫秒为单位返回该日历的时间值 |
//Demo06
package OOP.Class_6;
import java.util.Calendar;
public class Demo05 {
public static void main(String[] args) {
//1.创建Calendar对象
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime().toLocaleString());
System.out.println(calendar.getTimeInMillis());
//2.获取时间信息
//获取年
int year = calendar.get(Calendar.YEAR);
//获取月 从 0 开始的 比正常小 1
int mouth = calendar.get(Calendar.MONTH);
//获取日
int day = calendar.get(Calendar.DAY_OF_MONTH);
//获取时
int hour = calendar.get(Calendar.HOUR);//12小时制
int hour1 = calendar.get(Calendar.HOUR_OF_DAY);//24小时制
//获取分
int minute = calendar.get(Calendar.MINUTE);
//获取秒
int second = calendar.get(Calendar.SECOND);
System.out.println(year + "," + (mouth + 1) + "," + day);
System.out.println(hour1 + ":" + minute + ":" + second);
//3.修改时间
Calendar calendar1 = Calendar.getInstance();
//修改日
calendar1.set(Calendar.DAY_OF_MONTH, 5);
System.out.println(calendar1.getTime().toLocaleString());
//4.add方法修改时间
calendar1.add(Calendar.HOUR_OF_DAY, 1);
System.out.println(calendar1.getTime().toLocaleString());
//补充方法
int max = calendar1.getActualMaximum(Calendar.DAY_OF_MONTH);
int min = calendar1.getActualMinimum(Calendar.DAY_OF_MONTH);
System.out.println(max + " : " + min);
}
}
/*
2022年4月3日 下午3:10:15
1648969815414
2022,4,3
15:10:15
2022年4月5日 下午3:10:15
2022年4月5日 下午4:10:15
30 : 1
*/
常用的时间模式字母:
//格式化
package OOP.Class_6;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo06 {
public static void main(String[] args) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
String str = simpleDateFormat.format(date);
System.out.println(str);
}
}
/*
2022/04/03 15:24:24
*/
把字符转换成日期;
//解析
package OOP.Class_6;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo06 {
public static void main(String[] args) throws Exception{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = simpleDateFormat.parse("2019/6/6 12:12:12");
System.out.println(date.toLocaleString());
}
}
/*
2019年6月6日 下午12:12:12
*/
注意:Arrays.copyOf()
和System.arraycopy
的区别就是,System.arraycopy
内部也是Arrays.copyOf()
实现的;
package OOP.Class_6;
import java.util.Arrays;
public class Demo07 {
public static void main(String[] args) {
int[] arr = {10, 11, 33, 44, 55, 11, 77, 100};
int[] dest = new int[8];
//arraycopy 数组的复制
//src 原数组 srcPos 从哪个位置开始复制 dest 目标数组 destPos 目标数组的位置 length 复制的长度
System.arraycopy(arr, 4, dest, 4, 4);
System.out.println(Arrays.toString(dest));
//System.currentTimeMillis()
System.out.println(System.currentTimeMillis());
//System.exit()
System.exit(0);
System.out.println("程序退出!");
}
}
/*
[0, 0, 0, 0, 55, 11, 77, 100]
1648973982247
*/