0606
❤️用法介绍
在Java中,如果我们想获取当前时间,一般会使用Date类的无参构造函数,如下所示,我们获取到当前时间并输出:
1 2 3 4 5 6 7
| import java.util.Date; public class SimpleDateFormatDemo { public static void main(String[] args) { Date currentTime = new Date(); System.out.println(currentTime);
} }
|
此时我们会发现, 输出的格式并不是我们预期的格式,一般情况下,我们希望的格式都是类似于2019-02-18,2019-02-18 10:24:30,2019/02/18这样的,此时我们就需要用到java.text.SimpleDateFormat来自定义格式。
- 使用format()方法,我们可以将日期类型转换为自己自定义的字符串格式,如2019-02-18,2019/02/18,2019-02-18 10:24:30等,自定义格式如下表所示:
| 格式 |
释义 |
举例 |
| yyyy |
年 |
2019 |
| MM |
月 |
02 |
| dd |
日 |
18 |
| HH |
小时(24小时制) |
13,下午一点 |
| mm |
分钟 |
53 |
| ss |
秒 |
42 |
| SSS |
毫秒 |
629 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.zwwhnly.springbootdemo;
import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDateFormatDemo { public static void main(String[] args) {
Date currentTime = new Date();
System.out.println(currentTime);
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy/MM/dd"); System.out.println(simpleDateFormat1.format(currentTime));
System.out.println(simpleDateFormat2.format(currentTime));
System.out.println(simpleDateFormat3.format(currentTime)); } }
|
3.使用parse()方法将字符串转换为日期
- 在实际开发过程中,我们经常需要将字符串转换为日期类型,以进行后续操作,此时可以使用parse()方法,但需要注意:如果字符串与指定的格式不匹配,会报java.text.ParseException异常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.zwwhnly.springbootdemo; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDateFormatDemo { public static void main(String[] args) { try { SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm"); String strDate1 = "2019-02-18 13:58"; String strDate2 = "2019-02-18"; Date date1 = simpleDateFormat1.parse(strDate1); System.out.println(date1); Date date2 = simpleDateFormat1.parse(strDate2); System.out.println(date2); } catch (ParseException e) { e.printStackTrace(); } } }
|
strDate1格式匹配能正常转换为Date类型,而strDate2由于格式不匹配,抛出java.text.ParseException,正是因为如此,以上的代码才必须包括在try,catch语句中,否则IDEA会提示错误,代码也编译不通过。
Mine
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| package pers.dhx_.java0606;
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date;
public class DateTimeTest { public static void main(String[] args) { try { Test2(); } catch (ParseException e) { e.printStackTrace(); } }
static void Test1() { SimpleDateFormat sdf = new SimpleDateFormat(); Date date = new Date(); String format = sdf.format(date); System.out.println(format);
String str = "2012/6/6 上午8:57"; try { System.out.println(sdf.parse(str)); } catch (ParseException e) { e.printStackTrace(); }
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println(sdf1.format(date)); try { System.out.println(sdf1.parse("2042-05-29 20:24:42")); } catch (ParseException e) { e.printStackTrace(); } }
static void Test2() throws ParseException { String birth = "2021-08-05"; SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf1.parse(birth); java.sql.Date birthDate = new java.sql.Date(date.getTime()); System.out.println(birthDate); } }
|
Q:三天打鱼,两天晒网
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| static void Test3() { //计算总天数 String birth = "1990-01-01"; SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");//初始化规定的格式 Date date = null; try { date = sdf1.parse(birth); } catch (ParseException e) { e.printStackTrace(); } // System.out.println(date.getTime()); String keyIn; Scanner in = new Scanner(System.in); keyIn = in.nextLine(); Date date2 = null; try { date2 = sdf1.parse(keyIn); } catch (ParseException e) { e.printStackTrace(); } long result = ((date2.getTime() - date.getTime()) / 86400000) % 5; System.out.println(result); //2022-6-1 //4 }
|

🎈Calendar类
简介
Calendar 类是一个抽象类。
-
获取Calendar实例的方法
- 使用Calendar.getInstance()方法
- 调用它的子GregorianCalendar的构造器。
在java.util包下,Calendar是日历类,在Date后出现,替换掉了许多Date的方法。该类将所有可能用到的时间信息封装为静态成员变量,方便获取。
Calendar为抽象类,由于语言敏感性,Calendar类在创建对象时并非直接创建,而是通过静态方法创建,将语言敏感内容处理好,再返回子类对象。
一个Calendar的实例是系统时间的抽象表示,通过get(int field)方法来取得想要的时间信息。比如YEAR、MONTH、DAY_OF_WEEK、HOUR_OF_DAY 、MINUTE、SECOND
- public void set(int field,int value)
- public void add(int field,int amount)
- public final Date getTime()
- public final void setTime(Date date)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| package pers.dhx_.java0606;
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;
public class CalendarTest { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getClass()); System.out.println(calendar.get(calendar.WEEK_OF_YEAR));
calendar.set(calendar.WEEK_OF_YEAR, 25); System.out.println(calendar.WEEK_OF_YEAR); calendar.add(calendar.WEEK_OF_YEAR, 2); System.out.println(calendar.WEEK_OF_YEAR); calendar.add(calendar.WEEK_OF_YEAR, -2); System.out.println(calendar.WEEK_OF_YEAR);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(calendar.getTime()); System.out.println(sdf1.format(calendar.getTime())); SimpleDateFormat sdf2 = new SimpleDateFormat("YYYY-MM-DD"); System.out.println(sdf2.format(calendar.getTime()));
try { Date date = sdf1.parse("2010-04-01"); calendar.setTime(date); System.out.println(sdf1.format(calendar.getTime())); } catch (ParseException e) { e.printStackTrace(); } } }
|
🌟注意融会贯通,Date类是这几个API的桥梁
❤️JDK8之后的日期API
1.新日期时间API出现的背景
如果我们可以跟别人说:“我们在1502643933071见面,别晚了!”那么就再简单不过了。但是我们希望时间与昼夜和四季有关,于是事情就变复杂了。JDK 1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK 1.1引入Calendar类之后被弃用了。而Calendar并不比Date好多少。它们面临的问题是:
可变性:像日期和时间这样的类应该是不可变的。
偏移性:Date中的年份是从1900开始的,而月份都从0开始。
1 2 3 4 5
| public static void main(String[] args) { Date date1 = new Date(2000 - 1900, 9 - 1, 8); System.out.println(date1); }
|
格式化:格式化只对Date有用,Calendar则不行。
此外,它们也不是线程安全的;不能处理闰秒等。
总结:对日期和时间的操作一直是Java程序员最痛苦的地方之一。
2.LocalDate、LocalTime、LocalDateTime
LocalDate、LocalTime、LocalDateTime 类是其中较重要的几个类,它们的实例是不可变的对象,分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。它们提供了简单的本地日期或时间,并不包含当前的时间信息,也不包含与时区相关的信息。
🌟ISO-8601日历系统是国际标准化组织制定的现代公民的日期和时间的表示法,也就是公历。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| package pers.dhx_.java0606;
import org.junit.jupiter.api.Test;
import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime;
public class NewDateTest { public static void main(String[] args) {
}
@Test public void test() { LocalDate localdate = LocalDate.now(); LocalTime localTime = LocalTime.now(); LocalDateTime localdatetime = LocalDateTime.now(); System.out.println(localdate); System.out.println(localTime); System.out.println(localdatetime);
LocalDateTime l1 = LocalDateTime.of(2020, 10, 6, 22, 23); System.out.println(l1); System.out.println(localdatetime.getDayOfMonth()); System.out.println(localdatetime.getDayOfWeek()); System.out.println(localdatetime.getMonthValue()); System.out.println(localdatetime.getMonth()); System.out.println(localdatetime.getMinute()); LocalDateTime local1 = localdatetime.withDayOfMonth(22); System.out.println(localdate); System.out.println(local1); LocalDateTime local2 = localdatetime.withHour(18); System.out.println(local2);
LocalDateTime plus1 = localdatetime.plusDays(24); }
}
|

3.instant :瞬时
- Instant:时间线上的一个瞬时点。 这可能被用来记录应用程序中的事件时间戳。
- 在处理时间和日期的时候,我们通常会想到年,月,日,时,分,秒。然而,这只是时间的一个模型,是面向人类的。第二种通用模型是面向机器的,或者说是连续的。在此模型中,时间线中的一个点表示为一个很大的数,这有利于计算机处理。在UNIX中,这个数从1970年开始,以秒为的单位;同样的,在Java中,也是从1970年开始,但以毫秒为单位。
- java.time包通过值类型Instant提供机器视图,不提供处理人类意义上的时间单位。Instant表示时间线上的一点,而不需要任何上下文信息,例如,时区。概念上讲,它只是简单的表示自1970年1月1日0时0分0秒(UTC)开始的秒数。因为java.time包是基于纳秒计算的,所以Instant的精度可以达到纳秒级。
- (1 ns = 10-9 s) 1秒 = 10-3毫秒 =106微秒=109纳秒
1 2 3 4 5 6 7 8 9 10 11
| public void test() { Instant instant = Instant.now(); System.out.println(instant); System.out.println(instant.atOffset(ZoneOffset.ofHours(8))); System.out.println(instant.toEpochMilli()); Instant instant1 = Instant.ofEpochMilli(1654501322128l); System.out.println(instant1); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public void test() { DateTimeFormatter dtf = DateTimeFormatter.ISO_LOCAL_DATE_TIME; String format = dtf.format(LocalDateTime.now()); System.out.println(format);
TemporalAccessor parse = dtf.parse("2031-06-06T15:57:26.5844559"); System.out.println(parse);
DateTimeFormatter dtf1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG); DateTimeFormatter dtf2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL); DateTimeFormatter dtf3 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM); DateTimeFormatter dtf4 = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT); System.out.println(dtf1.format(LocalDateTime.now())); System.out.println(dtf2.format(LocalDateTime.now())); System.out.println(dtf3.format(LocalDateTime.now())); System.out.println(dtf4.format(LocalDateTime.now()));
DateTimeFormatter dtf5 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss E"); System.out.println(dtf5.format(LocalDateTime.now())); TemporalAccessor parse1 = dtf5.parse("2022-06-06 05:43:25 周一"); System.out.println(parse1); }
|
🗼其它API
- ZoneId**:该类中包含了所有的时区信息,一个时区的ID,如 Europe/Paris
- ZonedDateTime:**一个在ISO-8601日历系统时区的日期时间,如 2007-12-03T10:15:30+01:00 Europe/Paris。 其中每个时区都对应着ID,地区ID都为“{区域}/{城市}”的格式,例如:Asia/Shanghai等 **
- ** **Clock:**使用时区提供对当前即时、日期和时间的访问的时钟。
- 持续时间:Duration,用于计算两个“时间”间隔
- 日期间隔:Period,用于计算两个“日期”间隔
- TemporalAdjuster : 时间校正器。有时我们可能需要获取例如:将日期调整到“下一个工作日”等操作。
- TemporalAdjusters : 该类通过静态方法(firstDayOfXxx()/lastDayOfXxx()/nextXxx())提供了大量的常用TemporalAdjuster 的实现。
❤️Java比较器
1.在Java中经常会涉及到对象数组的排序问题,那么就涉及到对象之间
的比较问题。
2.Java实现对象排序的方式有两种:
自然排序:java.lang.Comparable
定制排序:java.util.Comparator
自然排序:java.lang.Comparable
Comparable 的典型实现:(默认都是从小到大排列的)
String:按照字符串中字符的Unicode值进行比较
1 2 3
| if(str1>str2) return 1;
|
Character:按照字符的Unicode值来进行比较
数值类型对应的包装类以及BigInteger、BigDecimal:按照它们对应的数值大小进行比较
1 2 3
| if(num1>num2) return 1;
|
Boolean:true 对应的包装类实例大于 false 对应的包装类实例
Date、Time等:后面的日期时间比前面的日期时间大
**定制排序:**java.util.Comparator
当元素的类型没有实现java.lang.Comparable接口而又不方便修改代码,
或者实现了java.lang.Comparable接口的排序规则不适合当前的操作,那
么可以考虑使用 Comparator 的对象来排序,强行对多个对象进行整体排
序的比较。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| package pers.dhx_.java0606;
import org.junit.jupiter.api.Test;
import java.util.Arrays; import java.util.Comparator; import java.util.Random;
public class CompareTest { public static void main(String[] args) { Goods[] arr = new Goods[4];
arr[0] = new Goods("Dagh", 624); arr[1] = new Goods("Agdh", 645); arr[2] = new Goods("Bgfdrt", 623); arr[3] = new Goods("Jfsd", 624); print(arr); Arrays.sort(arr, new Comparator<Goods>() { @Override public int compare(Goods g1, Goods g2) { if (g1.getPrice() > g2.getPrice()) return 1; else if (g1.getPrice() < g2.getPrice()) return -1; else return g1.getName().compareTo(g2.getName()); } }); print(arr);
}
@Test void Test() { Goods[] arr = new Goods[4]; for (int i = 0; i < 4; i++) { arr[i] = new Goods("UU" + i, (new Random()).nextInt() % 200); } print(arr); Arrays.sort(arr); print(arr); }
static void print(Goods[] arr) { System.out.println("----------------------------");
for (Goods s : arr) System.out.println(s.toString() + " "); System.out.println("----------------------------"); }
}
class Goods implements Comparable { private String name; private int price;
public void setName(String name) { this.name = name; }
public void setPrice(int price) { this.price = price; }
public String getName() { return name; }
public Goods(String name, int price) { this.name = name; this.price = price; }
Goods() { }
@Override public String toString() { return "Goods{" + "name='" + name + '\'' + ", price=" + price + '}'; }
public int getPrice() { return price; }
@Override public int compareTo(Object o) { if (o instanceof Goods) if (this.price > ((Goods) o).price) return 1; else return -1; else return 0; } }
|
对比:可以使用Comparator 为那些没有自然顺序的对象 collection 提供排序。
❤️System类
System类代表系统
- 系统级的很多属性和控制方法都放置在该类的内部。
- 位于java.lang。
- 构造器是private的,无法创建对象
- 其内部的成员变量和成员方法都是static,方便进行调用。
成员变量
- System类内部包含in、out和err三个成员变量,分别代表标准输入流(键盘输入),标准输出流(显示器)和标准错误输出流(显示器)。
成员方法
- native long currentTimeMillis()****: 该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数。
- void exit(int status):该方法的作用是退出程序。其中status的值为0代表正常退出,非零代表异常退出。使用该方法可以在图形界面编程中实现程序的退出功能等。9.5 System类
- void gc():该方法的作用是请求系统进行垃圾回收。至于系统是否立刻回收,则取决于系统中垃圾回收算法的实现以及系统执行时的情况。String getProperty(String key):该方法的作用是获得系统中属性名为key的属性对应的值。系统中常见的属性名以及属性的作用如下表所示:

🙉Math类
- java.lang.Math提供了一系列静态方法用于科学计算。其方法的参数和返回值类型一般为double型。
- abs 绝对值
- acos,asin,atan,cos,sin,tan 三角函数
- sqrt 平方根
- pow(double a,doble b) a的b次幂
- log自然对数
- exp e为底指数
- max(double a,double b)
- min(double a,double b)
- random()*返回0.0到1.0的随机数
- long round(double a) double型数据a转换为long型(四舍五入)
- toDegrees(double angrad)弧度—>角度
- toRadians(double angdeg) 角度—>弧度
❤️BigInteger类
- Integer类作为int的包装类,能存储的最大整型值为2 31-1,Long类也是有限的,最大为2 63-1。如果要表示再大的整数,不管是基本数据类型还是他们的包装类都无能为力,更不用说进行运算了。
java.math包的BigInteger可以表示不可变的任意精度的整数。BigInteger 提供
所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。
另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、
位操作以及一些其他操作。
构造器
BigInteger(String val):根据字符串构建BigInteger对象9.7 BigInteger****类
常用方法
-
public BigInteger abs():返回此 BigInteger 的绝对值的 BigInteger。
-
BigInteger add(BigInteger val) :返回其值为 (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[] divideAndRemainder(BigInteger val):返回包含 (this / val) 后跟(this % val) 的两个 BigInteger 的数组。
-
BigInteger pow(int exponent) :返回其值为 (thisexponent) 的 BigInteger。
💃 BigDecimal类
一般的Float类和Double类可以用来做科学计算或工程计算,但在**商业计算中,**要求数字精度比较高,故用到java.math.BigDecimal类.
BigDecimal类支持不可变的、任意精度的有符号十进制定点数。
👯构造器
👯常用方法
- public BigDecimal add(BigDecimal augend)
- public BigDecimal subtract(BigDecimal subtrahend)
- public BigDecimal multiply(BigDecimal multiplicand)
- public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)