# Java8之时间API **Repository Path**: fpfgitmy_admin/java8-datetime-api ## Basic Information - **Project Name**: Java8之时间API - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-04-28 - **Last Updated**: 2021-04-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #### JDK8特性之时间API ##### LocalDate、LocalDateTIme、LocalTIme | 方法 |描述 | | --- | --- | | now()/* now(Zoneld zone) | 静态方法,根据当前时间创建对象/指定时区的对象 | | of() | 静态方法,根据指定日期/时间创建对象 | | getDayOfMonth()/getDayOfYear() |获取月份天数(1-31)/获取年份天数(1~366) | | getDayOfWeek() | 获取星期几(返回一个DayOfWeek枚举值) | | getMonth() | 获取月份,返回一个Month枚举值 | | getMonthValue()/getYear() | 获取鱼粉(1-12)/获取年份 | | getHour()/getMinute()/getSeconde() | 获取当前对象对应的小时、分钟、秒 | | withDayOfMonth()/withDayOfYear()/withMonty()/withYear() | 将月份天数、年份天数、月份、年份修改为指定的值并返回新的对象 | | plusDays()/plusWeeks()/plusMonths()/plusYears()/plusHours() | 向当前对象添加几天、几周、几个月、几年、几小时 | | minusMonths()/minusWeeks()/minusDays()/minusYears()/minusHours() | 从当前对象减去几月、几周、几天、几年、几小时 | + 代码示例 ``` package com.felixfei.study.test; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.util.Date; /** * 为什么会出现新的日期API * 1. 可变性:像日期和时间这样的类应该是不可变的(calendar是可变的,修改的是本身的值) * 2. 偏移性:Date中的年份是从1900年开始的,而月份都从0开始 * 3. 格式化:格式化只对Date有用,Calendar则不行 * 4. 此外,他们也不是线程安全的,不能处理闰秒等 */ public class LocalDateTimeTest { public static void main(String[] args) { // 获取2021年3月22号的时间,要进行偏移量计算 Date date = new Date(2021 - 1900, 3 - 1, 22); System.out.println("2021年3月22号时间为:" + date); // LocalDate、LocalDateTime、LocalTime LocalDate now = LocalDate.now(); LocalDateTime now1 = LocalDateTime.now(); LocalTime now2 = LocalTime.now(); System.out.println("当前日期:" + now); System.out.println("当时间:" + now1); System.out.println("当前日期时间:" + now2); // 获取指定的日期时间,没有偏移量 LocalDateTime of = LocalDateTime.of(2020, 3, 10, 22, 12, 13); System.out.println("指定的日期时:" + of); System.out.println("当月的第几天:" + now.getDayOfMonth()); System.out.println("这周的第几天:" + now.getDayOfWeek()); System.out.println("第几月:" + now.getMonth()); System.out.println("月份数值:" + now.getMonthValue()); System.out.println("今天的第几天:" + now.getDayOfYear()); // 不可变性,修改的返回是新的值 LocalDate localDate = now.withDayOfMonth(23); System.out.println("修改天数后的值:" + localDate); LocalDate localDate1 = now.plusDays(2); System.out.println("增加1天后的值:" + localDate1); LocalDate localDate2 = now.minusDays(6); System.out.println("减6天后的值:" + localDate2); } } ``` ##### Instant + Instant:时间线上的一个瞬时点。这可能被用来记录应用程序中的事件时间戳。 + 在处理时间和日期的时候,我们通常会想到年,月,日,时,分,秒。然而,这只是事件的一个模型,是面向人类的。第二种通用模型是面向机器的,或者说是连续的。在此模型中,时间线中的一个点表示为一个很大的数,这有利于计算机处理。在UNIX中,这个数从1970年开始,以秒为单位:同样的,在Java中,也是从1970年开始,但以毫秒为单位。 + java.time包通过值类型Instant提供机器试图,不提供处理人类意义上的时间单位。Instant表示时间线上的一点,而不需要任何上下文信息,例如:时区。概念上讲,它只是简单的表示自1970年1月1日0时0分0秒(UTC)开始的秒数,因为java.time包是基于纳秒计算的,所以Instant的精度可以达到纳秒级。 +  | 方法 | 描述 | | --- | --- | | now() | 静态方法,返回默认UTC时区的Instant类的对象 | | ofEpochMilli(long epochMilli) | 静态方法,返回在1970-01-01 00:00:00基础上加上指定毫秒数之后的Instant类的对象 | | atOffset(ZoneOffset offset) | 结合即时的创建一个OffsetDateTime | | toEpochMilli() | 返回1970-01-01 00:00:00到当前时间的毫秒数,即为时间戳 | + 时间戳是指格林威治时间1970年01月01日00时00分00起至现在的总秒数 ``` package com.felixfei.study.test; import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneOffset; /** * @describle Instant *
* Instant:时间线上的一个瞬时点,这可能被用来记录应用程序中的事件时间戳。 * 类似于java.util.Date类 */ public class LocalDateTimeTest { public static void main(String[] args) { Instant now = Instant.now(); // 获取的是本初子午线的时间 System.out.println("获取的是本初子午线的时间=" + now); OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.of("+8")); System.out.println("获取的东八区的时间=" + offsetDateTime); long l = now.toEpochMilli(); System.out.println("获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数=" + l); Instant instant = Instant.ofEpochMilli(l); System.out.println("通过给定的毫秒数,获取的Instant实例=" + instant); } } ``` ##### DateTimeFormatter类 + 该类提供了三种格式化时间方法 + 自动以格式,如:`ofPattern("yyyy-MM-dd hh:mm:ss E")` | 方法 |描述 | | --- | --- | | ofPattern(String pattern) |静态方法,返回一个指定字符串格式的DateTImeFormatter | | format(TemproalAccessor t) | 格式化一个日期、时间,返回字符串 | | parse(CharSequence text) | 将指定格式的字符序列解析为一个日期、时间 | ``` package com.felixfei.study.test; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.time.temporal.TemporalAccessor; /** * @describle
java.time.format.DateTimeFormatter类 */ public class LocalDateTimeTest { public static void main(String[] args) { DateTimeFormatter isoDate = DateTimeFormatter.ISO_DATE; String str1 = isoDate.format(LocalDateTime.now()); System.out.println("格式化后的字符串日期=" + str1); TemporalAccessor parse = isoDate.parse(str1); System.out.println("字符串解析后的日期=" + parse); // FormatStyle.LONG 2021年3月24日 下午08时53分19秒 可使用 ofLocalizedDateTime 方法 // FormatStyle.MEDIUM 2021-3-24 20:55:10 可使用 ofLocalizedDateTime 方法 // FormatStyle.SHORT 21-3-24 下午8:55 可使用 ofLocalizedDateTime 方法 // FormatStyle.FULL 2021年3月24日 星期三 这种要使用 ofLocalizedDate 方法 DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL); String format = formatter.format(LocalDateTime.now()); System.out.println("格式化对应样式后的日期时间字符串=" + format); DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String format1 = formatter1.format(LocalDateTime.now()); System.out.println("格式化对应样式后的日期字符串=" + format1); TemporalAccessor parse1 = formatter1.parse(format1); System.out.println("字符串解析后的日期=" + parse1); // 注意字符串的格式必须和ofPattern中的一致 System.out.println("获取解析后的年="+parse1.get(ChronoField.YEAR)); } } ```