java8 LocalDate 常用方法
获取实例
- LocalDate:日期
- LocalDateTime:日期和时间
- LocalTIme:时间
System.out.println(LocalDate.now());
System.out.println(LocalTime.now());
System.out.println(LocalDateTime.now());
/* 打印结果:*/
// 2022-02-25
// 10:19:50.142
// 2022-02-25T10:19:50.143
LocalDateTime 获取 LocalDate 和 LocalTime
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
LocalDate localDate = now.toLocalDate();
System.out.println(localDate);
LocalTime localTime = now.toLocalTime();
System.out.println(localTime)
/* 打印结果:*/
// 2022-02-25T10:56:10.369
// 2022-02-25
// 10:56:10.369
更改时间
LocalTime.of(int hour, int minute, int second)
指定 时、分、秒LocalDate.of(int year, int mouth, int day)
指定年、月、日。这里指定月份的话是从1开始,与Calendar不同,Calendar的月份是从0开始。LocalDate.of(int year, Month mouth, int day)
指定年、月、日。这里的月份是一个枚举类,通过枚举类指定1到12月。LocalDateTime.of(int year, int mouth, int day, int hour, int minute, int second, int nanoOfSecond)
指定年、月、日、时、分、秒、纳秒LocalDateTime.of(int year, MOUTH mouth, int day, int hour, int minute, int second, int nanoOfSecond)
指定年、月、日、时、分、秒、纳秒。这里的月份是一个枚举类,通过枚举类指定1到12月。LocalDateTime.of(LocalDate localDate, LocalTime localTime)
从 LocalDate 和 LocalTime 中获取 LocalDateTime 。
System.out.println(LocalDate.of(2020,12,1));
System.out.println(LocalDate.of(2020, Month.APRIL,23));
System.out.println(LocalTime.of(12,23,33));
System.out.println(LocalDateTime.of(LocalDate.now(),LocalTime.now()));
System.out.println(LocalDateTime.of(2322,12,22,23,12,33,232));
System.out.println(LocalDateTime.of(2322,Month.JULY,22,23,12,33,232));
/* 打印结果 */
// 2022-02-25
// 2022-02-25
// 11:08:02
// 2022-02-25T11:09:25.870
// 2022-02-25T11:08:02.000000132
// 2022-02-25T11:08:02.000000132
plus 增加时间
- plusYears:增加指定年数。
- plusMonths:增加指定月数。
- plusWeeks:增加指定星期数。
- plusDays:增加指定天数。
- plusHours:增加指定小时数。
- plusMinutes:增加指定分钟数。
- plusSeconds:增加指定秒数。
- plusNanos:增加指定纳秒数。
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
System.out.println(now.plusYears(1)
.plusMonths(1L)
.plusWeeks(1)
.plusDays(1)
.plusHours(1)
.plusMinutes(1)
.plusSeconds(1)
.plusNanos(10000L));
/* 打印结果 */
// 2022-02-25T13:44:29.461
// 2023-04-02T14:45:30.461010
with 更改时间
- withDayOfMonth:月的第几天更改为新值后的拷贝
- withDayOfYear:年的第几天更改为新值后的拷贝
- withwithHour:小时数更改为新值后的拷贝
- withMinute:分钟数更改为新值后的拷贝
- withMonth:月份更改为新值后的拷贝
- withNano:纳秒数更改为新值后的拷贝
- withSecond:秒数更改为新值后的拷贝
- withYear:年份更改为新值后的拷贝
get 获取时间
- getDayOfMonth:获取月的第几天
- getDayOfWeek:获取星期几
- getDayOfYear:获取年的第几天
- getHour:获取小时
- getMinute:获取分钟
- getMonth:获取月份,获取的结果是月份的枚举值
- getMonthValue:获取月份,从1到12
- getNano:获取纳秒数
- getSecond:获取秒数
- getYear:获取年份
类型转换
LocalDateTime 与 String 互转
// 时间格式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
// 当前LocalDateTime
LocalDateTime localDateTime = LocalDateTime.now();
// LocalDateTime 格式化成 字符串
String dateStr = dateTimeFormatter.format(localDateTime);
System.out.println(dateStr);
// 字符串 格式化成 LocalDateTime
LocalDateTime localDateTime2 = LocalDateTime.parse(dateStr, dateTimeFormatter);
System.out.println(localDateTime2);
// 字符串 格式化成 LocalDate
LocalDate localDate = LocalDate.parse(dateStr, dateTimeFormatter);
System.out.println(localDate);
// 字符串 格式化成 LocalTime
LocalTime localTime = LocalTime.parse(dateStr, dateTimeFormatter);
System.out.println(localTime);
/* 打印结果 */
// 2022-02-25 14:12:06:383
// 2022-02-25T14:12:06.383
// 2022-02-25
// 14:12:06.383
时间戳 与 LocalDate、LocalDateTime 互转
// 获取当前时间戳
long timestamp = Instant.now().toEpochMilli();
System.out.println(timestamp);
// 时间戳 转 Instant 转 LocalDateTime
LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atOffset(ZoneOffset.ofHours(8)).toLocalDateTime();
System.out.println(localDateTime);
// LocalDateTime 转 Instant 转 时间戳
timestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
System.out.println(timestamp);
/* 打印结果 */
// 1645770922037
// 2022-02-25T14:35:22.037
// 1645770922037
Date 与LocalDate、LocalDateTime 互转
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
// LocalDateTime 转 Date
Date date = Date.from(localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant());
System.out.println(date);
// Date 转 LocalDateTime
localDateTime = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
System.out.println(localDateTime);
/* 打印结果 */
// 2022-02-25T15:10:37.537
// Fri Feb 25 15:10:37 CST 2022
// 2022-02-25T15:10:37.537
时区
新的时区类java.time.ZoneId是原有的java.util.TimeZone类的替代品。ZoneId对象可以通过ZoneId.of()方法创建,也可以通过ZoneId.systemDefault()获取系统默认时区。
// 指定城市时区
ZoneId shanghaiZoneId = ZoneId.of("Asia/Shanghai");
System.out.println(shanghaiZoneId);
// 获取系统默认时区
ZoneId systemZoneId = ZoneId.systemDefault();
System.out.println(systemZoneId);
LocalDateTime localDateTime = LocalDateTime.now();
// localDateTime 转换 ZonedDateTime
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, shanghaiZoneId);
System.out.println(zonedDateTime);
/* 打印结果 */
// Asia/Shanghai
// Asia/Shanghai
// 2022-02-25T15:31:53.335+08:00[Asia/Shanghai]
获取合法的时区 “区域/城市” 列表
// 获取所有合法的“区域/城市”
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
TimeZone 转 ZoneId
ZoneId oldToNewZoneId = TimeZone.getDefault().toZoneId();
Instant
Instant是时间线上的一个点,表示一个时间戳。Instant可以精确到纳秒,这超过了long的最大表示范围,所以在Instant的实现中是分成了两部分来表示,一部分是seconds,表示从1970-01-01 00:00:00开始到现在的秒数,另一个部分是nanos,表示纳秒部分。
//获取当前时刻的时间戳
Instant now = Instant.now();
System.out.println(now);
// 1970-01-01 00:00:00 之后的60秒,之后的100万纳秒。100万纳秒=1毫秒
Instant instant = Instant.ofEpochSecond(60, 1000000);
System.out.println(instant);
/* 打印结果 */
// 2022-02-25T07:18:12.764Z
// 1970-01-01T00:01:00.001Z
