Java 常用类

打印 上一主题 下一主题

主题 903|帖子 903|积分 2709

使用Scanner获取键盘输入


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

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() 获取当前时间戳
  1. public static void main(String[] args) {
  2.     // 获取系统所有的环境变量
  3.     Map<String,String> envs = System.getenv();
  4.     // 获取指定环境变量的值
  5.     String java_home = System.getenv("JAVA_HOME");
  6.     // 获取所有的系统属性
  7.     Properties pro = System.getProperties();
  8.     for (Object obj : pro.keySet()) {
  9.         System.out.println(obj.toString() + ":" + pro.get(obj));
  10.     }
  11.     // 获取指定属性的值
  12.     System.out.println(System.getProperty("java.vm.version"));
  13. }
复制代码
Runtime 类

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

  • availableProcessors() 处理器数量
  • freeMemory()  空闲内存数量
  • totalMemory() 总内存数
  • maxMemory() 可用最大内存数
  • exec() 单独启动一个进程来运行操作系统命令
  1. public static void main(String[] args) throws IOException {
  2.     Runtime rt = Runtime.getRuntime();
  3.     // 处理器输数量
  4.     System.out.println("处理器输数量:" + rt.availableProcessors());
  5.     // 空闲内存数
  6.     System.out.println("空闲内存数:" + rt.freeMemory());
  7.     // 总内存数
  8.     System.out.println("总内存数:" + rt.totalMemory());
  9.     // 可用最大内存数
  10.     System.out.println("可用最大内存数:" + rt.maxMemory());
  11.     // 启动Windows系统中的记事本
  12.     rt.exec("notepad.exe");
  13. }
复制代码
常用类

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修饰,因此该方法只能被子类重写或调用
自定义类实现“克隆”的步骤如下

  • 自定义类实现 Cloneable 接口。 这是一个标记性的接口,实现该接口的对象可以实现“自我克隆”
  • 自定义类实现自己的clone()方法
  • 实现clone()方法时通过super.clone()
  1. public class Person implements Cloneable {
  2.     private String name;
  3.     private int age;
  4.     public String getName() {
  5.         return name;
  6.     }
  7.     public void setName(String name) {
  8.         this.name = name;
  9.     }
  10.     public int getAge() {
  11.         return age;
  12.     }
  13.     public void setAge(int age) {
  14.         this.age = age;
  15.     }
  16.     // 调用父类克隆方法
  17.     public Person clone() throws CloneNotSupportedException {
  18.         return (Person) super.clone();
  19.     }
  20.     // 重写toString()
  21.     @Override
  22.     public String toString() {
  23.         return "Person{" +
  24.                 "name='" + name + '\'' +
  25.                 ", age=" + age +
  26.                 '}';
  27.     }
  28. }
复制代码
  1. public class Main {
  2.     public static void main(String[] args) throws CloneNotSupportedException {
  3.         Person person1 = new Person();
  4.         person1.setName("张三");
  5.         person1.setAge(15);
  6.         Person person2 = person1.clone();
  7.         System.out.println("person1:" + person1);
  8.         System.out.println("person2:" + person2);
  9.         System.out.println("person1 == person2 : " + (person1 == person2));
  10.     }
  11. }
复制代码
输出
  1. person1:Person{name='张三', age=15}
  2. person2:Person{name='张三', age=15}
  3. 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
  1. public class Main {
  2.     public static void main(String[] args) {
  3.         Person person = null;
  4.         System.out.println(Objects.hashCode(person));
  5.         System.out.println(Objects.toString(person));
  6.         System.out.println(Objects.requireNonNull(person));
  7.     }
  8. }
复制代码
输出
  1. 0
  2. null
  3. Exception in thread "main" java.lang.NullPointerException
  4.         at java.util.Objects.requireNonNull(Objects.java:203)
  5.         at Main.main(Main.java:11)
复制代码
Math 类

Math 类提供了大量的静态方法用于数学运算
绝对值
  1. Math.abs(-5.2);
复制代码
计算最大值
  1. Math.max(5.1,8.4);
复制代码
计算最小值
  1. Math.min(4,8.2);
复制代码
计算乘方
  1. Math.pow(20.5,5);
复制代码
计算立方根
  1. Math.cbrt(2.3)
复制代码
计算平方根
  1. Math.sqrt(2.3)
复制代码
取整,返回小于目标数的最大整数
  1. Math.floor(54.8) // 54.0
复制代码
取整,返回大于目标数的最大整数
  1. Math.ceil(54.2) // 55.0
复制代码
四舍五入取整
  1. Math.round(55.5) // 56
复制代码
返回一个伪随机数, 该值大于等于 0.0 且小于 1.0
  1. Math.random()
复制代码
更多方法参考Java API文档
Random

Random 类专门用于生成一个伪随机数, 它有两个构造器:一个构造器使用默认的种子(以当前时间作为种子),另一个构造器需要程序员显示传入一个long型整数的种子
获取对象后即可调用各种nextXxx()方法类获取伪随机数
  1. public static void main(String[] args) {
  2.     byte[] bytes = new byte[16];
  3.     Random random = new Random();
  4.     // 将生成的随机字节放入用户提供的字节数组中
  5.     random.nextBytes(bytes);
  6.     System.out.println("random.nextBytes(bytes):"+ Arrays.toString(bytes));
  7.     // 生成一个处于int整数取值范围的伪随机整数
  8.     System.out.println("random.nextInt():" + random.nextInt());
  9.     // 生成0~12 之间的伪随机整数
  10.     System.out.println("random.nextInt(12):" + random.nextInt(12));
  11.     // 生成0.0~1.0 之间的伪随机double数
  12.     System.out.println("random.nextDouble():" + random.nextDouble());
  13.     // 生成0.0~1.0 之间的伪随机float数
  14.     System.out.println("random.nextFloat():" + random.nextFloat());
  15.     // 生成一个处于long 证书取值范围的伪随机数
  16.     System.out.println("random.nextInt():" + random.nextLong());
  17. }
复制代码
输出
  1. random.nextBytes(bytes):[-43, 104, 98, -102, 99, 36, -67, -85, 111, -119, 45, 124, -112, -56, 48, 75]
  2. random.nextInt():153734012
  3. random.nextInt(12):3
  4. random.nextDouble():0.9250563428685509
  5. random.nextFloat():0.76055133
  6. random.nextInt():5933146265120450022
复制代码
Random使用一个48为的种子,如果这个类的两个实例使用同一个种子创建的,对它们以同样的顺序调用方法,则他们会产生相同的数字序列
  1. public static void main(String[] args) {
  2.     Random r1 = new Random(1234);
  3.     Random r2 = new Random(1234);
  4.     for (int i = 0; i < 5; i++) {
  5.         System.out.println("r1.nextInt():" + r1.nextInt());
  6.     }
  7.     for (int i = 0; i < 5; i++) {
  8.         System.out.println("r2.nextInt():" + r2.nextInt());
  9.     }
  10. }
复制代码
输出
  1. r1.nextInt():-1517918040
  2. r1.nextInt():1115789266
  3. r1.nextInt():-208917030
  4. r1.nextInt():1019800440
  5. r1.nextInt():-611652875
  6. r2.nextInt():-1517918040
  7. r2.nextInt():1115789266
  8. r2.nextInt():-208917030
  9. r2.nextInt():1019800440
  10. r2.nextInt():-611652875
复制代码
由此可见,Random产生的数字并不是真正随机的,而是一种伪随机
ThreadLocalRandom 类

ThreadLocalRandom 类是Java 7 新增的一个类,在并发访问的环境下,使用ThreadLocalRandom类来代替Random可以减少多线程资源竞争,最终保证系统具有更好的线程安全性。
用法与Random类的用法基本相似,它提供了一个current()方法来获取ThreadLocalRandom对象,获取对象后即可调用各种nextXxx()方法类获取伪随机数
  1. public static void main(String[] args) {
  2.     ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
  3.     System.out.println("threadLocalRandom.nextInt():" + threadLocalRandom.nextInt());
  4. }
复制代码
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对象
  1. public static void main(String[] args) throws ParseException {
  2.     BigDecimal decimal1 = new BigDecimal(0.1);
  3.     BigDecimal decimal2 = new BigDecimal(0.1 + "");
  4.     BigDecimal decimal3 = BigDecimal.valueOf(0.1);
  5.     System.out.println(decimal1);
  6.     System.out.println(decimal2);
  7.     System.out.println(decimal3);
  8. }
复制代码
输出
  1. 0.1000000000000000055511151231257827021181583404541015625
  2. 0.1
  3. 0.1
复制代码
比较两个BigDecimal的值是否相等时,要使用compareTo()来比较,equals不仅会比较值是否相等还会比较scale()也就是小数位数是否相等
  1. BigDecimal decimal1 = new BigDecimal("0.100");
  2. BigDecimal decimal2 = new BigDecimal( "0.1");
  3. System.out.println(decimal1.equals(decimal2));// false
  4. System.out.println(decimal1.compareTo(decimal2)); // 0
复制代码
可以通过设置scale进行截断小数
  1. public static void main(String[] args) throws ParseException {
  2.     BigDecimal decimal = new BigDecimal("0.150");
  3.     System.out.println(decimal.setScale(1,RoundingMode.HALF_UP)); // 四舍五入
  4.     System.out.println(decimal.setScale(1, RoundingMode.DOWN)); // 直接截断
  5. }
复制代码
输出
  1. 0.2
  2. 0.1
复制代码
字符串类

字符串就是一连串的字符序列,Java 提供了String,StringBuffer和StringBuilder三个类来封装字符串
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

StringBuilder 和StringBuffer 都是可变的字符序列,不同的是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对象和指定日期是否相等
  1. public static void main(String[] args) {
  2.     // 创建Date 对象传入当前时间戳
  3.     Date date1 = new Date(System.currentTimeMillis());
  4.     // 传入当前时间戳+1秒
  5.     Date date2 = new Date(System.currentTimeMillis() + 1000);
  6.     // 创建date3 传入date1 的时间戳
  7.     Date date3 = new Date(date1.getTime());
  8.     System.out.println(date1);
  9.     System.out.println(date2);
  10.     System.out.println("date1.after(date2):" + date1.after(date2));
  11.     System.out.println("date1.before(date2):" + date1.before(date2));
  12.     System.out.println("date1.compareTo(date2):" + date1.compareTo(date2));
  13.     System.out.println("date1.equals(date3):" + date1.equals(date3));
  14. }
复制代码
输出
  1. Thu Jul 06 09:15:12 CST 2023
  2. Thu Jul 06 09:15:13 CST 2023
  3. date1.after(date2):false
  4. date1.before(date2):true
  5. date1.compareTo(date2):-1
  6. date1.equals(date3):true
复制代码
注意,Date类的大部分方法(不包含以上列举的方法)都已经Deprecated(Java不推荐使用),而且对不同属性也使用了前后矛盾的偏移量, 比如月份和小时都是从0开始,月份中的天数则是从1开始, 年又是从1900年开始
Calendar 类

相比Date类, Calendar类 可以更好的处理日期时间,增加了日期时间的运算功能
Calendar 类本身是一个抽象类,它是所有日历类的模板,并提供了一些所有日历通用的方法。因为是抽象类所以不能被实例化,但它提供了几个静态getInstance()方法类获取Calendar对象,这项方法根据TimeZone,Locale 类来获取特定的Calendar,如果不指定,则采用默认的TimeZone,Locale来创建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.YEAR、Calendar.Month、Calendar.DATE 等 分别代表了年、月、日、时、分、秒等时间字段
需要指出的是Calendar.MONTH 字段代表月份,月份的起始值不是1,而是0
  1. public static void main(String[] args) {
  2.     Calendar calendar = Calendar.getInstance();
  3.     Date date = calendar.getTime();
  4.     System.out.println("YEAR:" + calendar.get(Calendar.YEAR));
  5.     System.out.println("MONTH:" + calendar.get(Calendar.MONTH));
  6.     System.out.println("calendar.getActualMaximum(Calendar.MONTH):" + calendar.getActualMaximum(Calendar.MONTH));
  7.     // 设置时分秒
  8.     calendar.set(2022, 10, 20, 5, 20, 10);
  9.     // 输出Date
  10.     System.out.println("calendar.set(2022, 10, 20, 5, 20, 10):" + calendar.getTime());
  11.     // 减去一个月
  12.     calendar.add(Calendar.MONTH, -13);
  13.     System.out.println("calendar.add(Calendar.MONTH, -13):" + calendar.getTime());
  14.     // roll() 当被修改的字段超出它允许范围时,上一级字段不会变大
  15.     calendar.roll(Calendar.MONTH, 13);
  16.     System.out.println("calendar.roll(Calendar.MONTH, 13):" + calendar.getTime());
  17.     //Calendar具有容错性,当设置不合理值时,上一级会自动进位
  18.     calendar.set(Calendar.MONTH,13);
  19.     System.out.println("calendar.set(Calendar.MONTH,13):" + calendar.getTime());
  20.     // 关闭容错性
  21.     calendar.setLenient(false);
  22.     calendar.set(Calendar.MONTH,13);
  23.     // 报错,由于set()方法具有延迟修改特性 所以下一行代码才会出错
  24.     // set() 延迟处理,当调用get()、getTime()、getTimeMIllis()、add() 或roll() 才会重新计算日历时间
  25.     System.out.println(calendar.getTime());
  26. }
复制代码
  1. YEAR:2023
  2. MONTH:6
  3. calendar.getActualMaximum(Calendar.MONTH):11
  4. calendar.set(2022, 10, 20, 5, 20, 10):Sun Nov 20 05:20:10 CST 2022
  5. calendar.add(Calendar.MONTH, -13):Wed Oct 20 05:20:10 CST 2021
  6. calendar.roll(Calendar.MONTH, 13):Sat Nov 20 05:20:10 CST 2021
  7. calendar.set(Calendar.MONTH,13):Sun Feb 20 05:20:10 CST 2022
  8. Exception in thread "main" java.lang.IllegalArgumentException: MONTH
  9.         at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2648)
  10.         at java.util.Calendar.updateTime(Calendar.java:3395)
  11.         at java.util.Calendar.getTimeInMillis(Calendar.java:1782)
  12.         at java.util.Calendar.getTime(Calendar.java:1755)
  13.         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 模板解析
  1. public static void main(String[] args) throws ParseException {
  2.     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  3.     System.out.println(simpleDateFormat.format(new Date())); // 输出当前时间
  4.     System.out.println(simpleDateFormat.parse("2023-07-17 23:28:17"));
  5. }
复制代码
输出
  1. 2023-07-17 23:31:38
  2. Mon 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类似),可以放心使用不必担心被修改。
LocalDate、LocalTime、LocalDateTime

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

  • LoacalDate  代表IOS格式(yyyy-MM-dd)的日期
  • LocalTime  表示一个时间
  • LocalDateTime 日期时间
常用方法
方法描述now(),now(ZoneId zone)静态方法,根据当前时间创建对象/指定时区of()静态方法,根据指定日期/时间创建对象getDayOfMonth,getDayOfYear()获得月份天数(1-31) / 获得年份天数(1-366)getDayOfWeek()获得星期几(返回一个DayOfWeek枚举值)getMonthValue()获得月份(1-12)getYear()获得年份withDayOfMonth将月份天数修改为指定的值并返回新的对象withDayOfYear()将年份天数修改为指定值并返回新的对象withMonth()将月份修改为指定的值并返回新的对象withYear()将年份修改为指定的值并返回新的对象plusXxx()向当前对象增加几年,几月,几日,几分,几秒等minusXxx()向当前对象减去几年,几月,几日,几分,几秒等
  1. public static void main(String[] args) {
  2.     LocalDate localDate = LocalDate.now();
  3.     LocalTime localTime = LocalTime.now();
  4.     LocalDateTime localDateTime = LocalDateTime.now();
  5.     System.out.println(localDate);
  6.     System.out.println(localTime);
  7.     System.out.println(localDateTime);
  8.     System.out.println("localTime.getSecond():" + localTime.getSecond());
  9.     System.out.println("localDate.getMonth():" + localDate.getMonth());
  10.     System.out.println("localDate.getYear():" + localDateTime.getYear());
  11.     System.out.println("localDateTime.withMinute(20):" + localDateTime.withMinute(20));
  12.     System.out.println("localDateTime.plusYears(20):" + localDateTime.plusYears(20));
  13. }
复制代码
  1. 2023-07-13
  2. 11:15:00.980
  3. 2023-07-13T11:15:00.980
  4. localTime.getSecond():0
  5. localDate.getMonth():JULY
  6. localDate.getYear():2023
  7. localDateTime.withMinute(20):2023-07-13T11:20:00.980
  8. localDateTime.plusYears(20):2043-07-13T11:15:00.980
复制代码
ZonedDateTime、ZonedId

ZonedDateTime 表示一个时区化的日期时间,ZonedId表示一个时区
ZonedDateTime使用起来和LocalDateTime几乎没有区别,可以理解为ZonedDateTime就是带有时区的LocalDateTime类
  1. public static void main(String[] args) {
  2.     ZonedDateTime zonedDateTime1 = ZonedDateTime.now();
  3.     ZonedDateTime zonedDateTime2 = ZonedDateTime.now(ZoneId.of("America/New_York"));
  4.     System.out.println(zonedDateTime1);
  5.     System.out.println(zonedDateTime2);
  6.     // 更改时区
  7.     System.out.println(zonedDateTime1.withZoneSameLocal(ZoneId.of("America/New_York")));
  8. }
复制代码
输出
  1. 2023-07-17T10:00:09.353+08:00[Asia/Shanghai]
  2. 2023-07-16T22:00:09.354-04:00[America/New_York]
  3. 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)结合即时的偏移来创建一个OffsetDateTimetoEpochMilli()返回时间戳(毫秒单位)getEpochSecond()返回时间戳(秒)plusMillis()增加毫秒plusSeconds(long secondsToAdd)增加秒minusMillis(long millisToSubtract)减去毫秒minusSeconds(long secondsToSubtract)减去秒
时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数
  1. public static void main(String[] args) {
  2.     Instant instant1 = Instant.now();
  3.     // 根据instant1的时间戳创建Instant实例
  4.     Instant instant2 = Instant.ofEpochSecond(instant1.getEpochSecond());
  5.     // 增加8小时偏移量
  6.     OffsetDateTime offsetDateTime = instant1.atOffset(ZoneOffset.ofHours(8));
  7.     System.out.println("当前时间戳(毫秒):" + instant1.toEpochMilli());
  8.     System.out.println("当前时间戳(秒):" + instant1.getEpochSecond());
  9.     System.out.println(instant1);
  10.     System.out.println(instant2);
  11.     System.out.println(offsetDateTime);
  12. }
复制代码
输出
  1. 当前时间戳(毫秒):1689561704104
  2. 当前时间戳(秒):1689561704
  3. 2023-07-17T02:41:44.104Z
  4. 2023-07-17T02:41:44Z
  5. 2023-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)修改指定时区,返回新的对象
  1. public static void main(String[] args) throws InterruptedException {
  2.     Clock clock = Clock.systemDefaultZone();
  3.     for (int i = 0; i < 5; i++) {
  4.         Thread.sleep(500);
  5.         System.out.println(clock.millis());
  6.     }
  7. }
复制代码
输出
  1. 1689585312412
  2. 1689585312919
  3. 1689585313428
  4. 1689585313936
  5. 1689585314443
复制代码
Duration

该类代表持续时间、一段时间,提供了ofXxx()方法表示几小时,几分钟,几天等,并且该类实现了TemporalAmount接口,因此LocalDateTime,LocalTime,Instant等都可以通过plus(TemporalAmount amountToAdd) 和 minus(TemporalAmount amount) 来对时间进行加减
  1. LocalDateTime localDateTime = LocalDateTime.now();
  2. localDateTime.minus(Duration.ofSeconds(20));
复制代码
DateTimeFormatter

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

  • 调用DateTimeFormatter 的format(TemporalAccessor temporal),其中LocalDate,LocalDateTime,LocalTime等类都是TemporalAccessor 的实现类
  • 调用LocalDate,LocalDateTime,LocalTime等日期、事件对象的format(DateTimeFormatter formatter) 方法进行格式化
  1. public static void main(String[] args) throws ParseException {
  2.     LocalDateTime localDateTime = LocalDateTime.now();
  3.     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  4.     System.out.println(localDateTime.format(formatter));
  5.     System.out.println(formatter.format(localDateTime));
  6. }
复制代码
输出
  1. 2023-07-18 09:08:45
  2. 2023-07-18 09:08:45
复制代码
将字符串解析成日期、事件对象时,可通过日期时间对象提供的LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) 方法进行解析
  1. public static void main(String[] args) throws ParseException {
  2.     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  3.     System.out.println(LocalDateTime.parse("2023-07-18 09:08:45",formatter));
  4.     System.out.println(LocalTime.parse("2023-07-18 09:08:45",formatter));
  5. }
复制代码
输出
  1. 2023-07-18T09:08:45
  2. 09:08:45
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

南飓风

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表