1.JDK8中新日期时间API
日期时间API的迭代
第一代:jdk 1.0 Date类
第二代:jdk 1.1 Calendar类,一定程度上替换Date类
第三代:jdk 1.8 提出了新的一套API
前两代存在的问题举例
可变性:像日期和时间这样的类应该是不可变的。
偏移性:Date中的年份是从1900开始的,而月份都从0开始。
格式化:格式化只对Date用,Calendar则不行。
此外,它们也不是线程安全的;不能处理闰秒等。


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
| public void APITest() { 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 localDateTime = LocalDateTime.of(2021,3,31,15,25,45); System.out.println(localDateTime); System.out.println(localDateTime.getDayOfMonth()); System.out.println(localDateTime.getDayOfWeek()); System.out.println(localDateTime.getMonth()); System.out.println(localDateTime.getMonthValue()); LocalDate localDate = localdate.withDayOfMonth(4); System.out.println(localDate); System.out.println(localdate); LocalDateTime localDateTime1 = localDateTime.plusDays(12); System.out.println(localDateTime); System.out.println(localDateTime1); }
|

Instant类,时间戳。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public void APITest() { Instant instant = Instant.now(); System.out.println(instant); OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(5)); System.out.println(offsetDateTime); long milli = instant.toEpochMilli(); System.out.println(milli); Instant instant1 = Instant.ofEpochMilli(1619956303369L); System.out.println(instant1); }
|

2.Java比较器(重要)
利用比较器对多个对象进行排序。
Java 实现对象排序的方式有两种:

重写compareTo()方法的一般规则如上图蓝色字体。
例子:
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
| public class Goods implements Comparable{ String name; int price; public Goods() { } public Goods(String name, int price) { this.name = name; this.price = price; } public String getName() { return name; }
public int getPrice() { return price; } public void setName(String name) { this.name = name; } public void setPrice(int price) { this.price = price; } @Override public String toString() { return "Goods{" + "name='" + name + '\'' + ", price=" + price + '}'; } @Override public int compareTo(Object o) { if (o instanceof Goods){ Goods goods = (Goods)o; if (this.price > goods.price){ return 1; } else if (this.price < goods.price){ return -1; }else{ return 0; } } throw new RuntimeException("传入的数据类型不一致!"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class CompareTest { public static void main(String[] args) { String[] arr = new String[]{"As","cd","sd1","12"}; Arrays.sort(arr); System.out.println(Arrays.toString(arr)); Goods[] goods = new Goods[4]; goods[0] = new Goods("xiaomi",19); goods[1] = new Goods("lenovo",25); goods[2] = new Goods("apple",199); goods[3] = new Goods("dell",35); Arrays.sort(goods); System.out.println(Arrays.toString(goods)); } }
|

接上面例子:
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
| public class CompareTest { public static void main(String[] args) { String[] arr = new String[]{"As","cd","sd1","12"}; Arrays.sort(arr,new Comparator()){ public int compare(Object o1,Object o2){ if(o1 instanceof String && o2 instanceof String){ String s1 = (String)o1; String s2 = (String)o2; return -s1.compareTo(s2); } throw new RuntimeException("传入的数据类型不一致!"); } }); System.out.println(Arrays.toString(arr));
Goods[] goods = new Goods[4]; goods[0] = new Goods("xiaomi",19); goods[1] = new Goods("lenovo",25); goods[2] = new Goods("xiaomi",199); goods[3] = new Goods("dell",35); Arrays.sort(goods,new Comparator(){ public int compare(Object o1,Object o2){ if(o1 instanceof Goods && o2 instanceof Goods){ Goods g1 = (Goods)o1; Goods g2 = (Goods)o2; if (g1.getName().equals(g2.getName())){ return -Integer.compare(g1.getPrice(),g2.getPrice()); }else{ return g1.getName().compareTo(g2.getName()); } } throw new RuntimeException("传入的数据类型不一致!"); } }); System.out.println(Arrays.toString(goods)); } }
|
比较
- Comparable接口的方式一旦一定,保证Comparable接口实现类的对象在任何位置都可以比较大小。
- Comparator接口属于临时性的比较。