文章目录

  • 常用类的概述和使用
  • 在线api https://docs.oracle.com/en/java/javase/11/docs/api/index.html
  • 常用的包
  • Object类的概述
  • 1. 基本概念
  • 2. 常用的方法
  • Scanner类
  • 创建Scanner对象
  • 包装类
  • 1. 概念:
  • 2. 分类:
  • Interger类的概述
  • Double的概述
  • Boolean类的概述
  • Character类的概述
  • 包装类使用总结
  • 数学处理类
  • 1. 基本概念
  • 2. 常用的方法
  • BigDecimal类
  • 1. 基本概念
  • 2. 常用的方法
  • BigInteger类
  • 1. 基本概念
  • 2. 常用的方法
  • 日期相关类
  • System类
  • Data类
  • SimpleDateFormat类的概述
  • Calendar类的概述
  • Java8日期类


常用类的概述和使用
在线api https://docs.oracle.com/en/java/javase/11/docs/api/index.html
常用的包
  • java.lang包 - 该包是Java语言的核心包,并且该包中的所有内容由Java虚拟机自动导入。 如:System类、String类、…
  • java.util包 - 该包是Java语言的工具包,里面提供了大量工具类以及集合类等。 如:Scanner类、Random类、List集合、…
  • java.io包 - 该包是Java语言中的输入输出包,里面提供了大量读写文件相关的类等。 如:FileInputStream类、FileOutputStream类、…
  • java.net包 - 该包是Java语言中的网络包,里面提供了大量网络编程相关的类等。 如:ServerSocket类、Socket类、…
  • java.sql 包 - 该包是Java语言中的数据包,里面提供了大量操作数据库的类和接口等。 如:DriverManager类、Connection接口、… … …
Object类的概述
1. 基本概念
  • java.lang.Object类是Java语言中类层次结构的根类,也就是说任何一个类都是该类的直接或者间 接子类。
  • 如果定义一个Java类时没有使用extends关键字声明其父类,则其父类为 java.lang.Object 类。
  • Object类定义了“对象”的基本行为, 被子类默认继承。
2. 常用的方法

方法声明

功能介绍

Object()

使用无参方式构造对象

boolean equals(Object obj)

用于判断调用对象是否与参数对象相等。 该方法默认比较两个对象的地址是否相等与 == 运算符的结果一致若希望比较两个对象的内容,则需要重写该方法。 若该方法被重写后,则应该重写hashCode方法来保证结果的一致性。

int hashCode()

用于获取调用对象的哈希码值(内存地址的编号)。 若两个对象调用equals方法相等,则各自调用该方法的结果必须相同 若两个调用对象equals方法不相等,则各自调用该方法的结果应该不相同。 为了使得该方法与equals方法保持一致,需要重写该方法。

String toString()

用于获取调用对象的字符串形式 该方法默认返回的字符串为:包名.类名@哈希码值的十六进制 为了返回更有意义的数据,需要重写该方法 使用print或println打印引用或字符串拼接引用都会自动调用该方法

Class getClass()

用于返回调用对象执行时的Class实例,反射机制使用

Scanner类
创建Scanner对象
  • 可以从你指定的文件,流中读取文本数据
  • 创建格式:Scanner sc = new Scanner(System.in);
  • 通过Scanner类获取数据,常用方法
  • nextInt() / nextFloat() / nextDouble() 这种只会读取一个数值,并且不会读取最后的换行符’\n’
  • hasNext()方法判断输入(文件、字符串、键盘等输入流)是否还有下一个输入项,若有,返回true,反之false。
  • next() 和 nextLine() 的区别
  • next() 遇到空白符就认为输入结束
  • nextLine() 遇到换行符就认为输入结束
  • 需要从键盘输入多个参数,尽量把nextLine类型放前面,如果前面有使用Scanner接受其他参数,回车时 nextLine() 会接受不到输入,可以改成两行nextLIne() 或者换成next() 即可
包装类
1. 概念:

通常情况下基本数据类型的变量不是对象,为了满足万物皆对象的理念就需要对基本数据类型的变量进行打包封装处理变成对象,而负责将这些变量声明为成员变量进行对象化处理的相关类,叫做包装 类。 如: Person p = new Person(); int num = 10;

2. 分类:

包装类

对应的基本类型

java.lang.Byte

byte

java.lang.Short

short

java.lang.Integer

int

java.lang.Long

long

java.lang.Float

float

java.lang.Double

double

java.lang.Boolean

boolean

java.lang.Character

char

Interger类的概述
  1. 基本概念
  • java.lang.Integer类内部包装了一个int类型的变量作为成员变量,主要用于实现对int类型的包装并 提供int类型到String类之间的转换等方法。
  1. 常用的常量

常量类型和名称

功能介绍

public static final int MAX_VALUE

表示int类型可以描述的最大值,即2^31-1

public static final int MIN_VALUE

表示int类型可以描述的最小值,即-2^31

public static final int SIZE

表示int类型采用二进制补码形式的位数

public static final int BYTES

表示int类型所占的字节个数

public static final Class TYPE

表示int类型的Class实例

  1. 常用的方法

方法声明

功能介绍

Integer(int value)

根据参数指定的整数来构造对象(已过时)

Integer(String s)

根据参数指定的字符串来构造对象 (已过时)

int intValue()

获取调用对象中的整数值并返回

static Integer valueOf(int i)

根据参数指定整数值得到Integer类型对象

boolean equals(Object obj)

比较调用对象与参数指定的对象是否相等

String toString()

返回描述调用对象数值的字符串形式

static int parseInt(String s)

将字符串类型转换为int类型并返回

static String toString(int i)

获取参数指定整数的十进制字符串形式

static String toBinaryString(int i)

获取参数指定整数的二进制字符串形式

static String toHexString(int i)

获取参数指定整数的十六进制字符串形式

static String toOctalString(int i)

获取参数指定整数的八进制字符串形式

  1. 装箱和拆箱的概念
    在Java5发布之前使用包装类对象进行运算时,需要较为繁琐的“拆箱”和“装箱”操作;即运算前先将 包装类对象拆分为基本类型数据,运算后再将结果封装成包装类对象。 从Java5开始增加了自动拆箱和自动装箱的功能。
  2. 自动装箱池
    在Integer类的内部提供了自动装箱池技术,将 -128到127 之间的整数已经装箱完毕,当程序中使用 该范围之间的整数时,无需装箱直接取用自动装箱池中的对象即可,从而提高效率。
public class IntegerTest {

    public static void main(String[] args) {

        // 1.打印Integer类中常用的常量数值
        System.out.println("最大值是:" + Integer.MAX_VALUE); // 2^31-1
        System.out.println("最小值是:" + Integer.MIN_VALUE); // -2^31
        System.out.println("所表示二进制的位数是:" + Integer.SIZE); // 32
        System.out.println("所占字节的个数是:" + Integer.BYTES); // 4
        System.out.println("对应int类型的Class实例是:" + Integer.TYPE); // int

        System.out.println("------------------------------------------------------");
        // 2.使用构造方法来构造Integer类型的对象并打印
        //Integer it1 = new Integer(123);
        //System.out.println("it1 = " + it1); // 自动调用toString方法   123
        //Integer it2 = new Integer("456");
        //System.out.println("it2 = " + it2); // 456
        // 上述方法已过时,建议使用valueOf方法取代之,相当于从int类型到Integer类型的转换,叫做装箱
        Integer it3 = Integer.valueOf(123);
        System.out.println("it3 = " + it3); // 123
        // 相当于从String类型到Integer类型的转换
        Integer it4 = Integer.valueOf("456");
        System.out.println("it4 = " + it4); // 456   自动调用toString方法得到的是String类型
        // 获取调用对象中的整数数值,相当于从Integer类型到int类型的转换,叫做拆箱
        int ia = it4.intValue();
        System.out.println("获取到的整数数据是:" + ia); // 456  得到的是int类型

        System.out.println("------------------------------------------------------");
        // 3.从Java5开始增加了自动装箱和自动拆箱的机制
        Integer it5 = 100;  // 直接通过赋值运算符实现自动装箱
        int ib = it5;       // 直接通过赋值运算符实现自动拆箱

        System.out.println("------------------------------------------------------");
        // 4.装箱和拆箱的笔试考点
        Integer it6 = 127; //128;
        Integer it7 = 127; //128;
        Integer it8 = new Integer(127); //new Integer(128);
        Integer it9 = new Integer(127); //new Integer(128);
        System.out.println(it6 == it7);      // 比较地址  true  false  地址一样
        System.out.println(it6.equals(it7)); // 比较内容  true
        System.out.println(it8 == it9);      // 比较地址  false
        System.out.println(it8.equals(it9)); // 比较内容  true

        System.out.println("------------------------------------------------------");
        // 5.实现静态方法的调用
        int ic = Integer.parseInt("200");
        //int ic = Integer.parseInt("200a"); // 编译ok,运行发生NumberFormatException数字格式异常,因为有字母
        System.out.println("字符串转换为整数的结果是:" + ic); // 200
        System.out.println("根据参数指定的整数获取对应的十进制字符串是:" + Integer.toString(200));
        System.out.println("根据参数指定的整数获取对应的二进制字符串是:" + Integer.toBinaryString(200));
        System.out.println("根据参数指定的整数获取对应的十六进制字符串是:" + Integer.toHexString(200));
        System.out.println("根据参数指定的整数获取对应的八进制字符串是:" + Integer.toOctalString(200));
    }
}
Double的概述
  1. 基本概念

java.lang.Double类型内部包装了一个double类型的变量作为成员变量,主要用于实现对double 类型的包装并提供double类型到String类之间的转换等方法。

  1. 常用的常量

常量类型和名称

功能介绍

public static final int SIZE

表示double类型的二进制位数

public static final int BYTES

表示double类型的字节个数

public static final Class TYPE

表示double类型的Class实例

  1. 常用的方法

方法声明

功能介绍

Double(double value)

根据参数指定的浮点数据来构造对象(已过时)

Double(String s)

根据参数指定的字符串来构造对象 (已过时)

double doubleValue()

获取调用对象中的浮点数据并返回

static Double valueOf(double d)

根据参数指定浮点数据得到Double类型对象

boolean equals(Object obj)

比较调用对象与参数指定的对象是否相等

String toString()

返回描述调用对象数值的字符串形式

static double parseDouble(String s)

将字符串类型转换为double类型并返回

boolean isNaN()

判断调用对象的数值是否为非数字

  • 扩展: java.lang.Number类是个抽象类,是上述类的父类来描述所有类共有的成员。
public class DoubleTest {

    public static void main(String[] args) {

        // 1.在Java5之前装箱和拆箱的实现
        // 实现了从double类型到Double类型的转换,装箱
        Double db1 = Double.valueOf(3.14);
        System.out.println("db1 = " + db1); // 3.14
        // 实现了从Double类型到double类型的转换,拆箱
        double d1 = db1.doubleValue();
        System.out.println("d1 = " + d1); // 3.14

        System.out.println("---------------------------------------------");
        // 2.从Java5开始实现自动装箱和自动拆箱
        Double db2 = 3.14;
        double d2 = db2;

        System.out.println("---------------------------------------------");
        // 3.实现静态方法和成员方法的调用
        double d3 = Double.parseDouble("13.14");
        System.out.println("d3 = " + d3); // 13.14

        System.out.println("db2对象的判断结果是:" + db2.isNaN()); // false 不是非数字
        Double db3 = Double.valueOf(0/0.0);
        System.out.println("db2对象的判断结果是:" + db3.isNaN()); // true  是非数字
    }
}
Boolean类的概述
  1. 基本概念
    java.lang.Boolean类型内部包装了一个boolean类型的变量作为成员变量,主要用于实现对 boolean类型的包装并提供boolean类型到String类之间的转换等方法。
  2. 常用的常量

常量类型和名称

功能介绍

public static final Boolean FALSE

对应基值为false的对象

public static final Boolean TRUE

对应基值为true的对象

public static final Class TYPE

表示boolean类型的Class实例

  1. 常用的方法

方法声明

功能介绍

Boolean(boolean value)

根据参数指定的布尔数值来构造对象(已过时)

Boolean(String s)

根据参数指定的字符串来构造对象 (已过时)

boolean booleanValue()

获取调用对象中的布尔数值并返回

static Boolean valueOf(boolean b)

根据参数指定布尔数值得到Boolean类型对象

boolean equals(Object obj)

比较调用对象与参数指定的对象是否相等

String toString()

返回描述调用对象数值的字符串形式

static boolean parseBoolean(String s)

将字符串类型转换为boolean类型并返回

public class BooleanTest {

    public static void main(String[] args) {

        // 1.在Java5之前采用方法进行装箱和拆箱
        // 相当于从boolean类型到Boolean类型的转换,装箱
        Boolean bo1 = Boolean.valueOf(true);
        System.out.println("bo1 = " + bo1); // true
        boolean b1 = bo1.booleanValue();
        System.out.println("b1 = " + b1); // true

        System.out.println("----------------------------------------------");
        // 2.从Java5开始支持自动装箱和拆箱
        Boolean bo2 = false;
        boolean b2 = bo2;
        System.out.println("b2 = " + b2); // false

        System.out.println("----------------------------------------------");
        // 3.实现从String类型到boolean类型的转换
        //boolean b3 = Boolean.parseBoolean("112");
        // 该方法的执行原理是:只要参数数值不为true或者TRUE时,则结果就是false,查手册和源码
        boolean b3 = Boolean.parseBoolean("TRUE");
        System.out.println("b3 = " + b3); // true
    }
}
Character类的概述
  1. 基本概念
    java.lang.Character类型内部包装了一个char类型的变量作为成员变量,主要用于实现对char类型 的包装并提供字符类别的判断和转换等方法。
  2. 常用的常量

常量类型和名称

功能介绍

public static final int SIZE

表示char类型的二进制位数

public static final int BYTES

表示char类型的字节个数

public static final Class TYPE

表示char类型的Class实例

  1. 常用的方法

方法声明

功能介绍

Character(char value)

根据参数指定的字符数据来构造对象(已过时)

char charValue()

获取调用对象中的字符数据并返回

static Character valueOf(char c)

根据参数指定字符数据得到Character类型对象

boolean equals(Object obj)

比较调用对象与参数指定的对象是否相等

String toString()

返回描述调用对象数值的字符串形式

static boolean isUpperCase(char ch)

判断参数指定字符是否为大写字符

static boolean isLowerCase(char ch)

判断参数指定字符是否为小写字符

static boolean isDigit(char ch)

判断参数指定字符是否为数字字符

static char toUpperCase(char ch)

将参数指定的字符转换为大写字符

static char toLowerCase(char ch)

将参数指定的字符转换为小写字符

public class CharacterTest {

    public static void main(String[] args) {

        // 1.在Java5之前调用方法实现装箱和拆箱机制
        // 相当于从char类型到Character类型的转换,装箱
        Character ca1 = Character.valueOf('a');
        System.out.println("ca1 = " + ca1); // a
        // 从Character类型向char类型的转换,拆箱
        char c1 = ca1.charValue();
        System.out.println("c1 = " + c1); // a

        System.out.println("----------------------------------------");
        // 2.从Java5开始支持自动装箱和拆箱
        Character ca2 = 'b';
        char c2 = ca2;
        System.out.println("c2 = " + c2); // b

        System.out.println("----------------------------------------");
        // 3.实现字符类型的判断以及转换
        System.out.println(Character.isUpperCase(c2)); // 判断是否为大写字母  false
        System.out.println(Character.isLowerCase(c2)); // 判断是否为小写字母  true
        System.out.println(Character.isDigit(c2));     // 判断是否为数字字符  false
        System.out.println("转换为大写字符是:" + Character.toUpperCase(c2)); // B
        System.out.println("转换为小写字符是:" + Character.toLowerCase(c2)); // b
    }
}
包装类使用总结
  • 基本数据类型转换为对应包装类的方式 :调用包装类的构造方法或静态方法即可
  • 获取包装类对象中基本数据类型变量数值的方式 :调用包装类中的xxxValue方法即可
  • 字符串转换为基本数据类型的方式 :调用包装类中的parseXxx方法即可
数学处理类
1. 基本概念

java.lang.Math类主要用于提供执行数学运算的方法,如:对数,平方根。

2. 常用的方法

方法声明

功能介绍

static int max(int a, int b)

返回两个参数中的最大值

static int min(int a, int b)

返回两个参数中的最小值

static double pow(double a, double b)

返回第一个参数的幂

static int abs(int a)

返回参数指定数值的绝对值

static long round(double a)

返回参数四舍五入的结果

static double sqrt(double a)

返回参数的平方根

static double random()

返回0.0到1.0的随机数

Random类中也可以直接生成随机数,比这个好用

/**
 * 编程实现对Math类中常用方法的测试
 */
public class MathTest {

    public static void main(String[] args) {

        System.out.println("获取两个整数中最大值的结果是:" + Math.max(10, 20)); // 20
        System.out.println("获取两个整数中最小值的结果是:" + Math.min(10, 20)); // 10
        System.out.println("获取次方的结果是:" + Math.pow(2, 3)); // 8.0  体现double类型
        System.out.println("获取绝对值的结果是:" + Math.abs(-5)); // 5
        System.out.println("进行四舍五入的结果是:" + Math.round(3.14)); // 3
        System.out.println("该整数的平方根是:" + Math.sqrt(16)); // 4.0
        System.out.println("生成的随机数是:" + Math.random()); // 随机数
    }
}
BigDecimal类
1. 基本概念

由于float类型和double类型在运算时可能会有误差,若希望实现精确运算则借助 java.math.BigDecimal类型加以描述。

2. 常用的方法

方法声明

功能介绍

BigDecimal(String val)

根据参数指定的字符串来构造对象

BigDecimal add(BigDecimal augend)

用于实现加法运算

BigDecimal subtract(BigDecimal subtrahend)

用于实现减法运算

BigDecimal multiply(BigDecimal multiplicand)

用于实现乘法运算

BigDecimal divide(BigDecimal divisor)

用于实现除法运算

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalTest {   
	public static void main(String[] args) {

        // 1.构造BigDecimal类型的两个对象
        BigDecimal bd1 = new BigDecimal("5.2");
        BigDecimal bd2 = new BigDecimal("1.3");
        // 2.使用构造完毕的对象实现加减乘除运算
        System.out.println("实现加法运算的结果是:" + bd1.add(bd2)); // 6.5
        System.out.println("实现减法运算的结果是:" + bd1.subtract(bd2)); // 3.9
        System.out.println("实现乘法运算的结果是:" + bd1.multiply(bd2)); // 6.76
        System.out.println("实现除法运算的结果是:" + bd1.divide(bd2)); // 4

        System.out.println("---------------------------------------------------------------");
        // 3.实现精确运算
        System.out.println(0.1 + 0.2); // 0.30000000000000004
        BigDecimal bd3 = new BigDecimal("0.1");
        BigDecimal bd4 = new BigDecimal("0.2");
        System.out.println("精确计算的结果是:" + bd3.add(bd4)); // 0.3

        System.out.println("---------------------------------------------------------------");
        // 4.注意事项
        BigDecimal bd5 = new BigDecimal("2");
        BigDecimal bd6 = new BigDecimal("0.3");
        //四舍五入
        System.out.println("除法运算的结果是:" + bd5.divide(bd6, RoundingMode.HALF_UP)); // 7
    }
}
BigInteger类
1. 基本概念

若希望表示比long类型范围还大的整数数据,则需要借助java.math.BigInteger类型描述。

2. 常用的方法

方法声明

功能介绍

BigInteger(String val)

根据参数指定的字符串来构造对象

BigInteger add(BigInteger val)

用于实现加法运算

BigInteger subtract(BigInteger val)

用于实现减法运算

BigInteger multiply(BigInteger val)

用于实现乘法运算

BigInteger divide(BigInteger val)

用于实现除法运算

BigInteger remainder(BigInteger val)

用于实现取余运算

BigInteger[] divideAndRemainder(BigInteger val)

用于实现取商和余数的运算

import java.math.BigInteger;

public class BigIntegerTest {

    public static void main(String[] args) {

        // 1.构造两个BigInteger类型的对象并指定初始值
        BigInteger bi1 = new BigInteger("20");
        BigInteger bi2 = new BigInteger("8");
        // 2.实现加减乘除取余操作并打印
        System.out.println("实现加法运算的结果是:" + bi1.add(bi2)); // 28
        System.out.println("实现减法运算的结果是:" + bi1.subtract(bi2)); // 12
        System.out.println("实现乘法运算的结果是:" + bi1.multiply(bi2)); // 160
        System.out.println("实现除法运算的结果是:" + bi1.divide(bi2)); // 2
        System.out.println("实现取余运算的结果是:" + bi1.remainder(bi2)); // 4

        System.out.println("-----------------------------------------------------");
        // 3.一次性得到商和余数
        BigInteger[] arr = bi1.divideAndRemainder(bi2);
        for (int i = 0; i < arr.length; i++) {
            System.out.println("下标为" + i + "的元素是:" + arr[i]); // 2 4
        }
    }
}
日期相关类
System类
  • 概念:Java.lang.System类中提供了一些有用的类字段和方法
  • 方法:static long currentTimeMillis(),返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差
Data类
  1. 基本概念:java.util.Date类主要用于描述特定的瞬间,也就是年月日时分秒,可以精确到毫秒。
  2. 常用的方法

方法声明

功能介绍

Date()

使用无参的方式构造对象,也就是当前系统时间

Date(long date)

根据参数指定毫秒数构造对象, 参数为距离1970年1月1日0时0分0秒 的毫秒数

long getTime()

获取调用对象距离1970年1月1日0时0分0秒的毫秒数

void setTime(long time)

设置调用对象为距离基准时间time毫秒的时间点

import java.util.Date;

public class DateTest {

    public static void main(String[] args) {

        // 1.使用无参方式构造Date对象并打印
        Date d1 = new Date();
        System.out.println("d1 = " + d1); // 获取当前系统时间

        System.out.println("------------------------------------");
        // 2.使用参数指定的毫秒数来构造Date对象并打印  1秒 = 1000毫秒  东八区
        Date d2 = new Date(1000);
        System.out.println("d2 = " + d2); // 1970 1 1 8 0 1

        System.out.println("------------------------------------");
        // 3.获取调用对象距离1970年1月1日0时0分0秒的毫秒数
        long msec = d2.getTime();
        System.out.println("获取到的毫秒数是:" + msec); // 1000

        // 4.设置调用对象所表示的时间点为参数指定的毫秒数
        d2.setTime(2000);
        System.out.println("修改后的时间是:" + d2); // 1970 1 1 8 0 2
    }
}
SimpleDateFormat类的概述
  1. 概念:java.text.SimpleDateFormat类主要用于实现日期和文本之间的转换。
  2. 常用方法:

方法声明

方法介绍

SimpleDateFormat()

使用无参方式构造对象

SimpleDateFormat(String pattern)

根据参数指定的模式来构造对象,模式主要有: y-年 M-月 d-日 H-时 m-分 s-秒

final String format(Date date)

用于将日期类型转换为文本类型

Date parse(String source)

用于将文本类型转换为日期类型

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

public class DateTimeFormatterTest {

    public static void main(String[] args) {

        // 1.获取当前系统的日期时间并打印
        LocalDateTime now = LocalDateTime.now();
        System.out.println("now = " + now);

        // 2.按照指定的格式准备一个DateTimeFormatter类型的对象
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 3.实现日期时间向字符串类型的转换并打印
        String str = dateTimeFormatter.format(now);
        System.out.println("调整格式后的结果是:" + str);
        // 4.实现字符串类型到日期时间类型的转换并打印
        TemporalAccessor parse = dateTimeFormatter.parse(str);
        System.out.println("转回去的结果是:" + parse);
    }
}
Calendar类的概述
  1. 概念:java.util.Calender类主要用于描述特定的瞬间,取代Date类中的过时方法实现全球化。该类是个抽象类,因此不能实例化对象,其具体子类针对不同国家的日历系统,其中应用最广泛的 是GregorianCalendar(格里高利历),对应世界上绝大多数国家/地区使用的标准日历系统。
  2. 常用的方法

方法声明

方法介绍

static Calendar getInstance()

用于获取Calendar类型的引 用

void set(int year, int month, int date, int hourOfDay, int minute, int second)

用于设置年月日时分秒信息

Date getTime()

用于将Calendar类型转换为 Date类型

void set(int field, int value)

设置指定字段的数值

void add(int field, int amount)

向指定字段增加数值

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarTest {

    public static void main(String[] args) {

        // 1.使用过时的方法按照指定的年月日时间分来构造对象
        Date d1 = new Date(2008-1900, 8-1, 8, 20, 8, 8);
        // 2.设置日期对象的格式并打印
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(d1);
        System.out.println("获取到的时间是:" + format); // 2008 8 8 20 8 8

        System.out.println("-----------------------------------------------------");
        // 2.使用取代的方式按照指定的年月日时分秒来构造对象
        // 2.1 获取Calendar类型的引用
        // 考点:既然Calendar是个抽象类不能创建对象,那么下面的方法为啥可以获取Calendar类型的引用呢?
        // 解析:由源码可知,返回的并不是Calendar类型的对象,而是Calendar类的子类GregorianCalendar等对象,形成了多态
        // 多态的使用场合之三
        Calendar instance = Calendar.getInstance();
        // 2.2 设置指定的年月日时分秒信息
        instance.set(2008, 8-1, 8, 20, 8, 8);
        // 2.3 转换为Date类型的对象
        Date d2 = instance.getTime();
        String format1 = sdf.format(d2);
        System.out.println("获取到的时间是:" + format1); // 2008 8 8 20 8 8

        System.out.println("-----------------------------------------------------");
        // 3.向指定的字段设置以及增加指定的数值
        instance.set(Calendar.YEAR, 2018);
        // 转换为Date类型并按照指定的格式打印
        Date d3 = instance.getTime();
        System.out.println("设置年份后的结果是:" + sdf.format(d3)); // 2018 8 8 20 8 8

        instance.add(Calendar.MONTH, 2);
        Date d4 = instance.getTime();
        System.out.println("增加月份后的结果是:" + sdf.format(d4)); // 2018 10 8 20 8 8
    }
}
Java8日期类
  1. 由来
  • JDK 1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK 1.1引入Calendar类之后被 弃用 了。而Calendar并不比Date好多少。它们面临的问题是:
  • Date类中的年份是从1900开始的,而月份都从0开始。
  • 格式化只对Date类有用,对Calendar类则不能使用。
  • 非线程安全等。
  1. 概述:
  • Java 8通过发布新的Date-Time API来进一步加强对 日期与时间的处理。
  • java.time包:该包日期/时间API的基础包。
  • java.time.chrono包:该包提供对不同日历系统的访问。
  • java.time.format包:该包能够格式化和解析日期时间对象。
  • java.time.temporal包:该包包含底层框架和扩展特性。
  • java.time.zone包:该包支持不同时区以及相关规则的类。
  1. LocalData类
  1. 概述:java.time.LocalDate类主要用于描述年-月-日格式的日期信息,该类不表示时间和时区信息。
  2. static LocalDate now() ,在默认时区中从系统时钟获取当前日期
  1. LocalTime类
  1. 概述:java.time.LocalTime 类主要用于描述时间信息,可以描述时分秒以及纳秒。
  2. 常用方法

方法声明

方法介绍

static LocalTime now()

从默认时区的系统时间中获取当前时间

static LocalTime now(ZoneId zone)

获取指定时区的当前时间

  1. LocalDataTime类
  1. 概述:java.time.LocalDateTime类主要用于描述ISO-8601日历系统中没有时区的日期时间,如2007-12- 03T10:15:30。
  2. 常用方法

方法声明

方法说明

static LocalDateTime now()

从默认时区的系统时间中获取 当前日期时间

static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)

根据参数指定的年月日时分秒 信息来设置日期时间

int getYear()

获取年份字段的数值

int getMonthValue()

获取1到12之间的月份字段

int getDayOfMonth()

获取日期字段

int getHour()

获取小时数

int getMinute()

获取分钟数

int getSecond()

获取秒数

LocalDateTime withYear(int year)

设置为参数指定的年

LocalDateTime withMonth(int month)

设置为参数指定的月

LocalDateTime withDayOfMonth(int dayOfMonth)

设置为参数指定的日

LocalDateTime withHour(int hour)

设置为参数指定的时

LocalDateTime withMinute(int minute)

设置为参数指定的分

LocalDateTime withSecond(int second)

设置为参数指定的秒

LocalDateTime plusYears(long years)

加上参数指定的年

LocalDateTime plusMonths(long months)

加上参数指定的月

LocalDateTime plusDays(long days)

加上参数指定的日

LocalDateTime plusHours(long hours)

加上参数指定的时

LocalDateTime plusMinutes(long minutes)

加上参数指定的分

LocalDateTime plusSeconds(long seconds)

加上参数指定的秒

LocalDateTime minusYears(long years)

减去参数指定的年

LocalDateTime minusMonths(long months)

减去参数指定的月

LocalDateTime minusDays(long days)

减去参数指定的日

LocalDateTime minusHours(long hours)

减去参数指定的时

LocalDateTime minusMinutes(long minutes)

减去参数指定的分

LocalDateTime minusSeconds(long seconds)

减去参数指定的秒

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class LocalDateTimeTest {

    public static void main(String[] args) {

        // 1.获取当前日期信息并打印
        LocalDate now = LocalDate.now();
        System.out.println("获取到的当前日期是:" + now);
        // 2.获取当前时间信息并打印
        LocalTime now1 = LocalTime.now();
        System.out.println("获取到的当前时间是:" + now1);
        // 3.获取当前日期时间信息并打印,使用最多
        LocalDateTime now2 = LocalDateTime.now();
        System.out.println("获取到的当前日期时间是:" + now2);

        System.out.println("-------------------------------------------------------");
        // 4.使用参数指定的年月日时分秒信息来获取对象并打印
        // 使用ctrl+F12来查找指定的方法
        LocalDateTime of = LocalDateTime.of(2008, 8, 8, 20, 8, 8);
        System.out.println("指定的日期时间是:" + of); // 自动调用toString方法
        System.out.println("获取到的年是:" + of.getYear()); // 2008
        System.out.println("获取到的月是:" + of.getMonthValue()); // 8
        System.out.println("获取到的日是:" + of.getDayOfMonth()); // 8
        System.out.println("获取到的时是:" + of.getHour()); // 20
        System.out.println("获取到的分是:" + of.getMinute()); // 8
        System.out.println("获取到的秒是:" + of.getSecond()); // 8

        System.out.println("-------------------------------------------------------");
        // 5.实现特征的设置并打印
        // 与String类型相似,调用对象本身的数据内容不会改变,返回值相当于创建了一个新的对象,由此证明了不可变性
        LocalDateTime localDateTime = of.withYear(2012);
        System.out.println("localDateTime = " + localDateTime); // 2012-08-08T20:08:08
        System.out.println("of = " + of); // 2008-08-08T20:08:08
        LocalDateTime localDateTime1 = localDateTime.withMonth(12);
        System.out.println("localDateTime1 = " + localDateTime1); // 2012 12 8 20 8 8

        System.out.println("-------------------------------------------------------");
        // 6.实现特征的增加并打印
        LocalDateTime localDateTime2 = localDateTime1.plusDays(2);
        System.out.println("localDateTime2 = " + localDateTime2); // 2012 12 10 20 8 8
        System.out.println("localDateTime1 = " + localDateTime1); // 2012 12 8 20 8 8
        LocalDateTime localDateTime3 = localDateTime2.plusHours(3);
        System.out.println("localDateTime3 = " + localDateTime3); // 2012 12 10 23 8 8

        System.out.println("-------------------------------------------------------");
        // 7.实现特征的减少并打印
        LocalDateTime localDateTime4 = localDateTime3.minusMinutes(1);
        System.out.println("localDateTime4 = " + localDateTime4); // 2012 12 10 23 7 8
        System.out.println("localDateTime3 = " + localDateTime3); // 2012 12 10 23 8 8
        LocalDateTime localDateTime5 = localDateTime4.minusSeconds(3);
        System.out.println("localDateTime5 = " + localDateTime5); // 2012 12 10 23 7 5

    }
}
  1. Instant类
  1. 概述:java.time.Instant类主要用于描述瞬间的时间点信息。
  2. 常用方法

方法声明

方法介绍

static Instant now()

从系统时钟上获取当前时间

OffsetDateTime atOffset(ZoneOffset offset)

将此瞬间与偏移量组合以创建偏移日期时间

static Instant ofEpochMilli(long epochMilli)

根据参数指定的毫秒数来构造对象,参数为距离1970年1月1 日0时0分0秒的毫秒数

long toEpochMilli()

获取距离1970年1月1日0时0分0秒的毫秒数

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class InstantTest {

    public static void main(String[] args) {

        // 1.使用Instant类来获取当前系统时间  并不是当前系统的默认时区  本初子午线   差8小时  东八区
        Instant now = Instant.now();
        System.out.println("获取到的当前时间为:" + now);

        // 2.加上时区所差的8个小时
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println("偏移后的日期时间为:" + offsetDateTime);

        System.out.println("--------------------------------------------------------");
        // 3.获取当前调用对象距离标准基准时间的毫秒数
        long g1 = now.toEpochMilli();
        System.out.println("获取到的毫秒差为:" + g1);

        // 4.根据参数指定的毫秒数来构造对象
        Instant instant = Instant.ofEpochMilli(g1);
        System.out.println("根据参数指定的毫秒数构造出来的对象为:" + instant);
    }
}
  1. DataTimeFormatter类
  1. 概述:java.time.format.DateTimeFormatter类主要用于格式化和解析日期
  2. 常用方法

方法声明

方法介绍

static DateTimeFormatter ofPattern(String pattern)

根据参数指定的模式来获取对象

String format(TemporalAccessor temporal)

将参数指定日期时间转换为字符串

TemporalAccessor parse(CharSequence text)

将参数指定字符串转换为日期时间

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

public class DateTimeFormatterTest {

    public static void main(String[] args) {

        // 1.获取当前系统的日期时间并打印
        LocalDateTime now = LocalDateTime.now();
        System.out.println("now = " + now);

        // 2.按照指定的格式准备一个DateTimeFormatter类型的对象
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 3.实现日期时间向字符串类型的转换并打印
        String str = dateTimeFormatter.format(now);
        System.out.println("调整格式后的结果是:" + str);
        // 4.实现字符串类型到日期时间类型的转换并打印
        TemporalAccessor parse = dateTimeFormatter.parse(str);
        System.out.println("转回去的结果是:" + parse);
    }
}