Date date =newDate();Date date1 =newDate(2021-1900,5-1,10);//已经过时不建议使用(需要年份减1900,月份减1,有偏移量)System.out.println(date.toString());//Tue Jun 15 11:11:16 CST 2021System.out.println(date1.toString());//Mon May 10 00:00:00 CST 2021System.out.println(date.getTime());//1623726676035
//localDate======= 注意这里每次更改不会对原有的内容更改和String类似===========//实例化方法一LocalDate now =LocalDate.now();System.out.println(now);//2021-06-15//实例化方法二(没有偏移量)System.out.println(LocalDate.of(1931,9,18));//193-09-18这个没有偏移量设多少就是多少//方法System.out.println(now.getDayOfYear());//166,一年中第几天也有now.getDayOfMonth()和now.getDayOfWeek()方法System.out.println(now.getMonth());//JUNE,获取月份,也有now.getYear()方法System.out.println(now.withYear(2022));//2022-06-15更改年份了,更改用WithXXX()更改相对应属性System.out.println(now.plusYears(10));//2031-06-15年份增加十年,增加用plusXXX()增加相应属性System.out.println(now.minusDays(899));//2018-12-29天数减少了899,//localTime//时分秒。方法和localDate类似//localDateTime//年月日时分秒。方法和localDate类似
instant
Instant now1 =Instant.now();System.out.println(now1);//现在瞬时时间2021-06-15T04:43:03.631533200Z这个时间不是东八区的时间System.out.println(Instant.ofEpochMilli(12561261261l));//1970加上Milli数的时间1970-05-26T09:14:21.261ZSystem.out.println(now1.atOffset(ZoneOffset.ofHours(8)));//2021-06-15T12:44:20.445167600+08:00,创建了8小时的时间偏移量System.out.println(now1.toEpochMilli());//1623732344305,获取1970到当前时间的毫秒数
```
### DateTimeFormatter时间的格式化和解析
```java
//第一种标准化DateTimeFormatterFormatter=DateTimeFormatter.ISO_LOCAL_DATE_TIME;LocalDateTime now =LocalDateTime.now();//格式化System.out.println(Formatter.format(now));//2021-06-15T12:59:04.2164441//解析System.out.println(Formatter.parse("2021-06-15T12:59:04.2164441"));//{},ISO resolved to 2021-06-15T12:59:04.216444100// 第二种DateTimeFormatterFormatter1=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);//DateTime//除了FormatStyle.SHORT 还有FormatStyle.LONG等等DateTimeFormatterFormatter2=DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);//Date//格式化System.out.println(Formatter1.format(now));//2021/6/15 下午1:04System.out.println(Formatter2.format(LocalDate.now()));//2021年6月15日星期二// 第三种自定义//可以自己设置格式 类似SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");DateTimeFormatterFormatter3=DateTimeFormatter.ofPattern("yyyy-MM-dd-hh-mm-ss");System.out.println(Formatter3.format(now));//2021-06-15-01-16-23
java中的时间JDK8以前System静态方法获取时间戳(1970到当前时间毫秒数)Date类util.Date包下的sql.Date包下的SimpleDateFormat类Calendar抽象类JDK8.0新增LocalDate,LocalTime,LocalDateTimeinstantJDK8以前System静态方法获取时间戳(1970到当前时间毫秒数)long l = System.currentTimeMillis();Date类util.Date包下的 Date date = n