使用Scanner获取键盘输入

  • hasNextXxx() 是否还有下个输入项,其中Xxx可以是intLong等代表基本数据类型的字符串。如果只是判断是否包含下一个字符串,则直接使用hasNext()
  • nextXxx() 获取下一个输入项
public class ScannerTest {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        while (scanner.hasNext()){            System.out.println(scanner.next());        }    }}
123123 // 控制台输入123123 // 控制台输出hellohello

系统相关System 类

System类代表当前Java程序的运行平台,程序不能创建System类的对象,System类提供了一系列的静态变量和静态方法,允许直接通过System类来调用这些静态变量和静态方法

静态变量

  • System.in 标准输入
  • System.out 标注输出
  • System.err 错误输出流

静态方法

  • System.getenv() 获取所有环境变量,返回java.util.Map对象
  • System.getenv(String name) 获取指定环境变量的值
  • System.getProperties() 获取所有的系统属性
  • System.getProperty(String name) 获取指定系统属性
  • System.identityHashCode(Object x) 获取对象的hashCode
  • System.currentTimeMillis() 获取当前时间戳
public static void main(String[] args) {    // 获取系统所有的环境变量    Map envs = System.getenv();    // 获取指定环境变量的值    String java_home = System.getenv("JAVA_HOME");    // 获取所有的系统属性    Properties pro = System.getProperties();    for (Object obj : pro.keySet()) {        System.out.println(obj.toString() + ":" + pro.get(obj));    }    // 获取指定属性的值    System.out.println(System.getProperty("java.vm.version"));}

Runtime 类

Runtime 类代表Java程序的运行时环境,每个Java程序都有一个与之对应的Runtime实例,应用程序通过该对象与其运行时环境相连。应用程序不能创建自己的Runtime实例,但可以通过getRuntime()获取与之相关的Runtime对象

Runtime类常用实例方法

  • availableProcessors() 处理器数量
  • freeMemory() 空闲内存数量
  • totalMemory() 总内存数
  • maxMemory() 可用最大内存数
  • exec() 单独启动一个进程来运行操作系统命令
public static void main(String[] args) throws IOException {    Runtime rt = Runtime.getRuntime();    // 处理器输数量    System.out.println("处理器输数量:" + rt.availableProcessors());    // 空闲内存数    System.out.println("空闲内存数:" + rt.freeMemory());    // 总内存数    System.out.println("总内存数:" + rt.totalMemory());    // 可用最大内存数    System.out.println("可用最大内存数:" + rt.maxMemory());    // 启动Windows系统中的记事本    rt.exec("notepad.exe");}

常用类Object 类

Object 类是所有类、数组、枚举类的父类,也就是说Java允许把任何类型的对象付给Object类型的变量

因为所有的Java类都是Object的子类,所以任何Java对象都可以调用Object类的方法。

Object类童工了如下几个常用方法

  • boolean equals(Object obj) 判断指定对象与该对象是否相等
  • protected void finalize() 当没有变量引用该对象时,垃圾回收器调用此方法来清理该对象的资源
  • Class getClass() 返回该对象的运行时类
  • int hashCode() 返回该对象的hashCode值
  • String toString() 返回该对象的字符串表示

Object 类还提供了一个protected 修饰的clone() 方法,可以帮助其他对象实现“克隆”,由于Object 类提供的clone()方法用了protected修饰,因此该方法只能被子类重写或调用

自定义类实现“克隆”的步骤如下

  1. 自定义类实现 Cloneable 接口。 这是一个标记性的接口,实现该接口的对象可以实现“自我克隆”
  2. 自定义类实现自己的clone()方法
  3. 实现clone()方法时通过super.clone()
public class Person implements Cloneable {    private String name;    private int 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;    }    // 调用父类克隆方法    public Person clone() throws CloneNotSupportedException {        return (Person) super.clone();    }    // 重写toString()    @Override    public String toString() {        return "Person{" +                "name='" + name + '\'' +                ", age=" + age +                '}';    }}
public class Main {    public static void main(String[] args) throws CloneNotSupportedException {        Person person1 = new Person();        person1.setName("张三");        person1.setAge(15);        Person person2 = person1.clone();        System.out.println("person1:" + person1);        System.out.println("person2:" + person2);        System.out.println("person1 == person2 : " + (person1 == person2));    }}

输出

person1:Person{name='张三', age=15}person2:Person{name='张三', age=15}person1 == person2 : false

必须实现Cloneable接口才可以调用Object类的clone()方法,否则会报错 Exception in thread "main" java.lang.CloneNotSupportedException: Person

Objects 工具类

Java 7新增了一个Objects 工具类,它提供了一些工具方法来操作对象,这些工具方法大多是“空指针”安全的。 例如使用Objects 类提供的toString(Object o)方法,就不会引发空指针异常,当o为null时,程序将返回一个“null” 字符串

Objects 提供了以下常用静态方法

  • int hashCode(Object o) 返回对象的hashCode值,如果对象为null,则返回0
  • String toString(Object o) 返回对象的toString,如果对象为null,则返回“null”
  • T requireNonNull(T obj) 返回对象本身,如果为null,则抛出异常
  • boolean isNull(Object obj) 判断对象是否为null,是则返回true
public class Main {    public static void main(String[] args) {        Person person = null;        System.out.println(Objects.hashCode(person));        System.out.println(Objects.toString(person));        System.out.println(Objects.requireNonNull(person));    }}

输出

0nullException in thread "main" java.lang.NullPointerExceptionat java.util.Objects.requireNonNull(Objects.java:203)at Main.main(Main.java:11)

Math 类

Math 类提供了大量的静态方法用于数学运算

绝对值

Math.abs(-5.2);

计算最大值

Math.max(5.1,8.4);

计算最小值

Math.min(4,8.2);

计算乘方

Math.pow(20.5,5);

计算立方根

Math.cbrt(2.3)

计算平方根

Math.sqrt(2.3)

取整,返回小于目标数的最大整数

Math.floor(54.8) // 54.0

取整,返回大于目标数的最大整数

Math.ceil(54.2) // 55.0

四舍五入取整

Math.round(55.5) // 56

返回一个伪随机数, 该值大于等于 0.0 且小于 1.0

Math.random()

更多方法参考Java API文档

Random

Random 类专门用于生成一个伪随机数, 它有两个构造器:一个构造器使用默认的种子(以当前时间作为种子),另一个构造器需要程序员显示传入一个long型整数的种子

获取对象后即可调用各种nextXxx()方法类获取伪随机数

public static void main(String[] args) {    byte[] bytes = new byte[16];    Random random = new Random();    // 将生成的随机字节放入用户提供的字节数组中    random.nextBytes(bytes);    System.out.println("random.nextBytes(bytes):"+ Arrays.toString(bytes));    // 生成一个处于int整数取值范围的伪随机整数    System.out.println("random.nextInt():" + random.nextInt());    // 生成0~12 之间的伪随机整数    System.out.println("random.nextInt(12):" + random.nextInt(12));    // 生成0.0~1.0 之间的伪随机double数    System.out.println("random.nextDouble():" + random.nextDouble());    // 生成0.0~1.0 之间的伪随机float数    System.out.println("random.nextFloat():" + random.nextFloat());    // 生成一个处于long 证书取值范围的伪随机数    System.out.println("random.nextInt():" + random.nextLong());}

输出

random.nextBytes(bytes):[-43, 104, 98, -102, 99, 36, -67, -85, 111, -119, 45, 124, -112, -56, 48, 75]random.nextInt():153734012random.nextInt(12):3random.nextDouble():0.9250563428685509random.nextFloat():0.76055133random.nextInt():5933146265120450022

Random使用一个48为的种子,如果这个类的两个实例使用同一个种子创建的,对它们以同样的顺序调用方法,则他们会产生相同的数字序列

public static void main(String[] args) {    Random r1 = new Random(1234);    Random r2 = new Random(1234);    for (int i = 0; i < 5; i++) {        System.out.println("r1.nextInt():" + r1.nextInt());    }    for (int i = 0; i < 5; i++) {        System.out.println("r2.nextInt():" + r2.nextInt());    }}

输出

r1.nextInt():-1517918040r1.nextInt():1115789266r1.nextInt():-208917030r1.nextInt():1019800440r1.nextInt():-611652875r2.nextInt():-1517918040r2.nextInt():1115789266r2.nextInt():-208917030r2.nextInt():1019800440r2.nextInt():-611652875

由此可见,Random产生的数字并不是真正随机的,而是一种伪随机

ThreadLocalRandom 类

ThreadLocalRandom 类是Java 7 新增的一个类,在并发访问的环境下,使用ThreadLocalRandom类来代替Random可以减少多线程资源竞争,最终保证系统具有更好的线程安全性。

用法与Random类的用法基本相似,它提供了一个current()方法来获取ThreadLocalRandom对象,获取对象后即可调用各种nextXxx()方法类获取伪随机数

public static void main(String[] args) {    ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();    System.out.println("threadLocalRandom.nextInt():" + threadLocalRandom.nextInt());}

BigInteger 类

java.math包的BigInteger可以表示不可变的任意精度的整数。BigInteger提供了所有Java 的基本整数的操作符的对应物, 并提供java.lang.Math的所有相关方法

另外,BigInteger还提供一下运算: 模算符,GCD计算,指数测试,素数生成,位操作以及一些其他操作

构造器

  • BigInteger(String val) 根据字符串构造BigInteger 对象

常用方法

  • public BigInteger abs() 返回此BigInteger的绝对值的BigInteger
  • BigInteger add(BigInteger) 返回其值为(this + val)BigInteger
  • BigInteger subtract(BigInteger val) 返回其值为(this - val)BigInteger
  • BigInteger multiply(BigInteger val) 返回其值为(this * val)BigInteger
  • BigInteger divide(BigInteger val) 返回其值为(this / val)BigInteger,整数相除只保留整数部分
  • BigInteger remainder(BigInteger val) 返回其值为(this % val)BigInteger
  • BigInteger pow(int exponent) 返回其值为(this^exponent)BigInteger

BigDecimal 类

float、double容易引起精度丢失问题,为了能精确表示、计算浮点数, Java提供了BigDecimal对象

构造器

  • public BigDecimal(double val)
  • public BigDecimal(String val)

BigDecimal 类同样提供了add()subtract()multiply()divide(),pow()等方法对精确浮点数进行常规算术运算

不推荐使用BigDecimal(double val) 构造器,会造成精度丢失问题,如果必须使用double 浮点数作为BigDecimal 构造器的参数时,应通过BigDecimal.valueOf(double value) 静态方法来创建BigDecimal对象

public static void main(String[] args) throws ParseException {    BigDecimal decimal1 = new BigDecimal(0.1);    BigDecimal decimal2 = new BigDecimal(0.1 + "");    BigDecimal decimal3 = BigDecimal.valueOf(0.1);    System.out.println(decimal1);    System.out.println(decimal2);    System.out.println(decimal3);}

输出

0.10000000000000000555111512312578270211815834045410156250.10.1

比较两个BigDecimal的值是否相等时,要使用compareTo()来比较,equals不仅会比较值是否相等还会比较scale()也就是小数位数是否相等

BigDecimal decimal1 = new BigDecimal("0.100");BigDecimal decimal2 = new BigDecimal( "0.1");System.out.println(decimal1.equals(decimal2));// falseSystem.out.println(decimal1.compareTo(decimal2)); // 0

可以通过设置scale进行截断小数

public static void main(String[] args) throws ParseException {    BigDecimal decimal = new BigDecimal("0.150");    System.out.println(decimal.setScale(1,RoundingMode.HALF_UP)); // 四舍五入    System.out.println(decimal.setScale(1, RoundingMode.DOWN)); // 直接截断}

输出

0.20.1

字符串类

字符串就是一连串的字符序列,Java 提供了StringStringBufferStringBuilder三个类来封装字符串

String

String 是一个final类,是一个不可变类

常用构造器

  • String() 创建一个包含0个字符序列的String对象
  • String(String original) 根据字符串来床架还你一个String 对象
  • String(StringBuffer) 根据StringBuffer对象来创建对应得String对象
  • String(StringBuilder builder) 根据StringBuilder 对象来创建对应的String 对象
  • String(byte[] bytes,Charset charset) 使用指定得字符集将指定的byte[] 数组解码成一个新的String 对象
  • String(byte bytes[], int offset, int length) 使用默认字符集将指定得byte[]数组从 offset 开始,长度为length 得子数组解码成一个新的String对象
  • String(byte bytes[], int offset, int length, Charset charset) 使用指定字符集将指定得byte[]数组从 offset 开始,长度为length 得子数组解码成一个新的String对象

String 类构造器指定字符集也可直接使用字符串指定,例如String(byte bytes[], Charset charset)

常用方法

  • int length() 返回当前字符串长度

  • String replace(char oldChar,char newChar) 将字符串种地第一个oldChar 替换成newChar

  • String substring(int beginIndex)beginIndex位置开始截取字符串

  • String substring(int beginIndex,int endIndex)beginIndex位置开始到endIndex位置结束截取字符串

  • char charAt(int index) 获取字符串中指定位置的字符。

  • int compareTo(String anotherString) 比较两个字符串的大小。相等返回0,否则两个字符串从第0个字符开始比较,返回第一个不相等的字符差。如果较短字符串是较长字符串的前面部分,则返回它们的长度差

  • String concat(String str) 将该String 对象 与 str 连接在一起。

  • boolean contentEquals(StringBuffer sb) 将字符串与StringBuffer对象进行比较

  • static String copyValueOf(char[] data) 将字符数组拼接成字符串

  • boolean startWith(String prefix) 该字符串是否已prefix 开头

  • boolean startWith(String prefix,int toffset) 该字符串从toffset位置算起,是否已prefix开头

  • boolean endsWith(String suffix)String 对象是否以suffix 结尾

  • boolean equals(Object anObject) 判断该字符串与指定对象是否相等

  • boolean equalsIgnoreCase(String str) 判断该字符串与指定对象是否相等(忽略大小写)

  • byte[] getBytes() 将该String 对象转换成byte数组

  • void getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin) 将字符串从srcBegin开始到srcEnd结束的字符复制到dst字符数组中, 其中dstBegin为目标字符数组的起始复制位置

  • int indexOf(int ch) 找出 ch 字符在该字符串第一次出现的位置

  • int indexOf(int ch,int fromIndex) 找出ch在该字符串从fromIndex开始后第一次出现的位置

  • int indexOf(String str) 找出str 在该字符串中第一次出现的位置

  • int indexOf(String str,int fromIndex) 找出str 字符串在该字符串从fromIndex开始后第一次出现的位置

  • int lastIndexOf(int ch) 找出 ch 字符在该字符串最后一次出现的位置

  • int lastIndexOf(int ch,int fromIndex) 找出ch在该字符串从fromIndex开始后最后一次出现的位置

  • int lastIndexOf(String str) 找出str 在该字符串中最后一次出现的位置

  • int lastIndexOf(String str,int fromIndex) 找出str 字符串在该字符串从fromIndex开始后最后一次出现的位置

  • toCharArray 将字符串转换成 char数组

  • toLowerCase() 将字符串转换为小写

  • toUpperCase() 将字符串转换为大写

  • valueOf(X x) 将基本类型转为字符串类型

StringBuilder 和 StringBuffer

StringBuilderStringBuffer 都是可变的字符序列,不同的是StringBuffer 是线程安全,而且效率相对较低

常用方法

  • append(xxx) 追加字符串
  • delete(int start,int end) 删除指定位置的内容
  • replace(int start,int end,String str) 把[start,end]位置替换为str
  • insert(int offset,xxx) 在指定位置插入xxx
  • reverse() 把当前字符序列逆转

日期、时间类Date 类

java.util 包提供了Date类来处理日期和时间

Date 类提供了六个构造器,其中4个已经废弃,剩下两个构造器如下

  • Date() 生成一个代表当前日期时间的Date对象。 该构造器在底层调用System.currentTimeMIllis()获取long 整数作为日期参数
  • Date(long date) 根据指定的long 型整数来生成一个Date 对象,。该参数是从1970年1月1日起的毫秒数(时间戳)

实例方法

  • boolean after(Date when) 测试该日期是否在指定日期when之后
  • boolean before(Date when) 测试该日期是否在指定日期when之前
  • long getTime() 返回时间戳
  • void setTime() 设置该Date 对象的时间
  • int compareTo(Date when) 比较当调用此方法的Date对象和指定日期。两者相等时候返回0。调用对象在指定日期之前则返回负数。调用对象在指定日期之后则返回正数。
  • boolean equals(Object when) 判断调用此方法的Date对象和指定日期是否相等
public static void main(String[] args) {    // 创建Date 对象传入当前时间戳    Date date1 = new Date(System.currentTimeMillis());    // 传入当前时间戳+1秒    Date date2 = new Date(System.currentTimeMillis() + 1000);    // 创建date3 传入date1 的时间戳    Date date3 = new Date(date1.getTime());    System.out.println(date1);    System.out.println(date2);    System.out.println("date1.after(date2):" + date1.after(date2));    System.out.println("date1.before(date2):" + date1.before(date2));    System.out.println("date1.compareTo(date2):" + date1.compareTo(date2));    System.out.println("date1.equals(date3):" + date1.equals(date3));}

输出

Thu Jul 06 09:15:12 CST 2023Thu Jul 06 09:15:13 CST 2023date1.after(date2):falsedate1.before(date2):truedate1.compareTo(date2):-1date1.equals(date3):true

注意,Date类的大部分方法(不包含以上列举的方法)都已经Deprecated(Java不推荐使用),而且对不同属性也使用了前后矛盾的偏移量, 比如月份和小时都是从0开始,月份中的天数则是从1开始, 年又是从1900年开始

Calendar 类

相比Date类, Calendar类 可以更好的处理日期时间,增加了日期时间的运算功能

Calendar 类本身是一个抽象类,它是所有日历类的模板,并提供了一些所有日历通用的方法。因为是抽象类所以不能被实例化,但它提供了几个静态getInstance()方法类获取Calendar对象,这项方法根据TimeZone,Locale 类来获取特定的Calendar,如果不指定,则采用默认的TimeZoneLocale来创建Calendar

实例方法

  • Date getTime() 返回与当前对象时间相同的Date对象
  • void add(int field, int amount) 为给定的日历字段添加或减去指定的时间量
  • int getActualMaximum(int field) 返回指定日历字段可能拥有的最大值
  • int getActualMinimum(int field) 返回指定日历字段可能拥有的最小值
  • void roll(int field, int amount)add()方法类似,区别在于加上amount 后超过该字段所能表示的最大范围时,也不会像上一个字段进位
  • void set(int field, int value) 将给定的日历字段设置为给定值
  • void set(int year, int month, int date) 设置Calendar 对象的 年、月、日三个字段的值
  • void set(int year, int month, int date, int hourOfDay, int minute) 设置 Calendar 对象的 年、月、日、时、分、秒六个字段的值
  • void setLenient(boolean lenient) 关闭Calendar的容错性,通过set()方法传入不合理的值的时候,Calendar 直接报错

上面很多方法需要一个int 类型的参数, filed 时Calendar 类的静态变量,如Calendar.YEARCalendar.MonthCalendar.DATE 等 分别代表了年、月、日、时、分、秒等时间字段

需要指出的是Calendar.MONTH 字段代表月份,月份的起始值不是1,而是0

public static void main(String[] args) {    Calendar calendar = Calendar.getInstance();    Date date = calendar.getTime();    System.out.println("YEAR:" + calendar.get(Calendar.YEAR));    System.out.println("MONTH:" + calendar.get(Calendar.MONTH));    System.out.println("calendar.getActualMaximum(Calendar.MONTH):" + calendar.getActualMaximum(Calendar.MONTH));    // 设置时分秒    calendar.set(2022, 10, 20, 5, 20, 10);    // 输出Date    System.out.println("calendar.set(2022, 10, 20, 5, 20, 10):" + calendar.getTime());    // 减去一个月    calendar.add(Calendar.MONTH, -13);    System.out.println("calendar.add(Calendar.MONTH, -13):" + calendar.getTime());    // roll() 当被修改的字段超出它允许范围时,上一级字段不会变大    calendar.roll(Calendar.MONTH, 13);    System.out.println("calendar.roll(Calendar.MONTH, 13):" + calendar.getTime());    //Calendar具有容错性,当设置不合理值时,上一级会自动进位    calendar.set(Calendar.MONTH,13);    System.out.println("calendar.set(Calendar.MONTH,13):" + calendar.getTime());    // 关闭容错性    calendar.setLenient(false);    calendar.set(Calendar.MONTH,13);    // 报错,由于set()方法具有延迟修改特性 所以下一行代码才会出错    // set() 延迟处理,当调用get()、getTime()、getTimeMIllis()、add() 或roll() 才会重新计算日历时间    System.out.println(calendar.getTime());}
YEAR:2023MONTH:6calendar.getActualMaximum(Calendar.MONTH):11calendar.set(2022, 10, 20, 5, 20, 10):Sun Nov 20 05:20:10 CST 2022calendar.add(Calendar.MONTH, -13):Wed Oct 20 05:20:10 CST 2021calendar.roll(Calendar.MONTH, 13):Sat Nov 20 05:20:10 CST 2021calendar.set(Calendar.MONTH,13):Sun Feb 20 05:20:10 CST 2022Exception in thread "main" java.lang.IllegalArgumentException: MONTHat java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2648)at java.util.Calendar.updateTime(Calendar.java:3395)at java.util.Calendar.getTimeInMillis(Calendar.java:1782)at java.util.Calendar.getTime(Calendar.java:1755)at Date.CalendarMain.main(CalendarMain.java:33)

set()方法延迟修改

set(f,value) 方法将日历字段 f 更改为value,此外它还设置了一个内部成员变量,一直是日历字段 f 已被更改。尽管日历字段 f 时立即更改的, 但该 Calendar 所代表的时间却不会立即修改, 直到下次调用 get()、getTime()、getTimeMIllis()、add() 或roll()时 才会重新计算日历的时间。这样的优势时多次调用set()不会触发多次不必要的计算

使用 SimpleDateFormat格式化日期

SimpleDateFormat 可以非常灵活的格式化 Date,也可以用于解析各种格式的日期字符串

创建实例时需传入一个pattern 日期模板字符串

  • yyyy:年
  • MM:月
  • dd: 日
  • HH: 小时
  • mm: 分钟
  • ss: 秒

格式化方法

  • String format(Date date) 将Date对象格式化
  • Date parse(String source) 将指定文本按照pattern 模板解析
public static void main(String[] args) throws ParseException {    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    System.out.println(simpleDateFormat.format(new Date())); // 输出当前时间    System.out.println(simpleDateFormat.parse("2023-07-17 23:28:17"));}

输出

2023-07-17 23:31:38Mon Jul 17 23:28:17 CST 2023

Java 8 新增的日期、事件包

Java 8 专门新增了一个java.time包,此包下提供了新的时间和API

和旧的API相比,新API严格区分了时刻、本地日期、本地时间和带时区的日期时间,并且,对日期和时间进行运算更加方便。

此外,新API修正了旧API不合理的常量设计:

  • Month的范围用1~12表示1月到12月;
  • Week的范围用1~7表示周一到周日。

新API的类型几乎全部是不变类型(和String类似),可以放心使用不必担心被修改。

LocalDateLocalTimeLocalDateTime

LocalDateLocalTimeLocalDateTime 分别表示使用 ISO-8601日历系统的日期、时间、日期时间,它们提供了简单的本地日期或事件,并不包含当前的时间信息,也不包含与失去相关的信息

  • LoacalDate 代表IOS格式(yyyy-MM-dd)的日期
  • LocalTime 表示一个时间
  • LocalDateTime 日期时间

常用方法

方法描述
now()now(ZoneId zone)静态方法,根据当前时间创建对象/指定时区
of()静态方法,根据指定日期/时间创建对象
getDayOfMonthgetDayOfYear()获得月份天数(1-31) / 获得年份天数(1-366)
getDayOfWeek()获得星期几(返回一个DayOfWeek枚举值
getMonthValue()获得月份(1-12)
getYear()获得年份
withDayOfMonth将月份天数修改为指定的值并返回新的对象
withDayOfYear()将年份天数修改为指定值并返回新的对象
withMonth()将月份修改为指定的值并返回新的对象
withYear()将年份修改为指定的值并返回新的对象
plusXxx()向当前对象增加几年,几月,几日,几分,几秒等
minusXxx()向当前对象减去几年,几月,几日,几分,几秒等
public static void main(String[] args) {    LocalDate localDate = LocalDate.now();    LocalTime localTime = LocalTime.now();    LocalDateTime localDateTime = LocalDateTime.now();    System.out.println(localDate);    System.out.println(localTime);    System.out.println(localDateTime);    System.out.println("localTime.getSecond():" + localTime.getSecond());    System.out.println("localDate.getMonth():" + localDate.getMonth());    System.out.println("localDate.getYear():" + localDateTime.getYear());    System.out.println("localDateTime.withMinute(20):" + localDateTime.withMinute(20));    System.out.println("localDateTime.plusYears(20):" + localDateTime.plusYears(20));}
2023-07-1311:15:00.9802023-07-13T11:15:00.980localTime.getSecond():0localDate.getMonth():JULYlocalDate.getYear():2023localDateTime.withMinute(20):2023-07-13T11:20:00.980localDateTime.plusYears(20):2043-07-13T11:15:00.980

ZonedDateTimeZonedId

ZonedDateTime 表示一个时区化的日期时间,ZonedId表示一个时区

ZonedDateTime使用起来和LocalDateTime几乎没有区别,可以理解为ZonedDateTime就是带有时区的LocalDateTime

public static void main(String[] args) {    ZonedDateTime zonedDateTime1 = ZonedDateTime.now();    ZonedDateTime zonedDateTime2 = ZonedDateTime.now(ZoneId.of("America/New_York"));    System.out.println(zonedDateTime1);    System.out.println(zonedDateTime2);    // 更改时区    System.out.println(zonedDateTime1.withZoneSameLocal(ZoneId.of("America/New_York")));}

输出

2023-07-17T10:00:09.353+08:00[Asia/Shanghai]2023-07-16T22:00:09.354-04:00[America/New_York]2023-07-17T10:00:09.353-04:00[America/New_York]

Instant

代表一个具体的时刻,可以精确到纳秒,表示时间戳

1秒 = 1000毫秒 = 10^6 微秒 = 10^9 纳秒

方法描述
now()静态方法,返回默认UTC时区的instant类的对象
ofEpochMilli()静态方法,返回指定时间戳(毫秒单位)的instant类对象
Instant ofEpochSecond(long epochSecond)静态方法,返回指定时间戳(秒单位)的instant类对象
atOffset(ZoneOffset offset)结合即时的偏移来创建一个OffsetDateTime
toEpochMilli()返回时间戳(毫秒单位)
getEpochSecond()返回时间戳(秒)
plusMillis()增加毫秒
plusSeconds(long secondsToAdd)增加秒
minusMillis(long millisToSubtract)减去毫秒
minusSeconds(long secondsToSubtract)减去秒

时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数

public static void main(String[] args) {    Instant instant1 = Instant.now();    // 根据instant1的时间戳创建Instant实例    Instant instant2 = Instant.ofEpochSecond(instant1.getEpochSecond());    // 增加8小时偏移量    OffsetDateTime offsetDateTime = instant1.atOffset(ZoneOffset.ofHours(8));    System.out.println("当前时间戳(毫秒):" + instant1.toEpochMilli());    System.out.println("当前时间戳(秒):" + instant1.getEpochSecond());    System.out.println(instant1);    System.out.println(instant2);    System.out.println(offsetDateTime);}

输出

当前时间戳(毫秒):1689561704104当前时间戳(秒):16895617042023-07-17T02:41:44.104Z2023-07-17T02:41:44Z2023-07-17T10:41:44.104+08:00

Clock

该类用于获取指定时区的当前日期、时间(相当于钟表,每次获取时间都是当前的系统时间)

方法描述
systemUTC()静态方法,获取UTC时区的Clock对象
systemDefaultZone()静态方法,获取系统默认的Clock对象
Clock system(ZoneId zone)静态方法,获取指定时区的Clock对象
Instant instant()获取当前时间的Instant类实例
long millis()返回当前时间戳(毫秒单位)
ZoneId getZone()返回当前时区
Clock withZone(ZoneId zone)修改指定时区,返回新的对象
public static void main(String[] args) throws InterruptedException {    Clock clock = Clock.systemDefaultZone();    for (int i = 0; i < 5; i++) {        Thread.sleep(500);        System.out.println(clock.millis());    }}

输出

16895853124121689585312919168958531342816895853139361689585314443

Duration

该类代表持续时间、一段时间,提供了ofXxx()方法表示几小时,几分钟,几天等,并且该类实现了TemporalAmount接口,因此LocalDateTimeLocalTime,Instant等都可以通过plus(TemporalAmount amountToAdd)minus(TemporalAmount amount) 来对时间进行加减

LocalDateTime localDateTime = LocalDateTime.now();localDateTime.minus(Duration.ofSeconds(20));

DateTimeFormatter

使用DateTimeFOrmatter将日期、时间格式化字符串可通过如下两种方式

  • 调用DateTimeFormatterformat(TemporalAccessor temporal),其中LocalDateLocalDateTimeLocalTime等类都是TemporalAccessor 的实现类
  • 调用LocalDateLocalDateTimeLocalTime等日期、事件对象的format(DateTimeFormatter formatter) 方法进行格式化
public static void main(String[] args) throws ParseException {    LocalDateTime localDateTime = LocalDateTime.now();    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");    System.out.println(localDateTime.format(formatter));    System.out.println(formatter.format(localDateTime));}

输出

2023-07-18 09:08:452023-07-18 09:08:45

将字符串解析成日期、事件对象时,可通过日期时间对象提供的LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) 方法进行解析

public static void main(String[] args) throws ParseException {    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");    System.out.println(LocalDateTime.parse("2023-07-18 09:08:45",formatter));    System.out.println(LocalTime.parse("2023-07-18 09:08:45",formatter));}

输出

2023-07-18T09:08:4509:08:45