java8 日期常用方法总结

java8 新增日期类,使用方便

java8 中的日期分为 LocalDate 和 LocalDateTime,LocalDate是年月日格式的日期时间;LocalDateTime是年月日时分秒格式的日期时间,二者的用法API基本一致

除此之外还有 YearMonth,年月格式日期;MonthDay 月日格式日期

目录

1、创建日期

2、日期和字符串转换

2.1、将日期转成字符串

2.2、字符串转日期

3、获取日期年月日时分秒

4、日期比较

5、日期计算

6、时间戳


1、创建日期

获取当前日期

LocalDate localDate = LocalDate.now();

获取当前年月日时分秒时间

LocalDateTime localDateTime = LocalDateTime.now();

通过字符串创建时间

LocalDate localDate = LocalDate.of(2023, 8, 30);
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 30, 15, 31, 10);
LocalDate localDate = LocalDate.parse("2023-08-30");

 LocalDateTime 默认字符串格式年月日和时分秒中间有个T

LocalDateTime localDateTime = LocalDateTime.parse("2023-05-21T13:10:28");

获取当前年月

YearMonth yearMonth = YearMonth.now();
System.out.println(yearMonth);

获取当前月日

MonthDay monthDay = MonthDay.now();
System.out.println(monthDay);

2、日期和字符串转换

通过 DateTimeFormatter 类指定日期格式

2.1、将日期转成字符串

LocalDate localDate = LocalDate.now();

//将日期转换成字符串
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formatDate = dateTimeFormatter.format(localDate);

LocalDateTime

 LocalDateTime localDateTime = LocalDateTime.now();

 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 String formatDate = dateTimeFormatter.format(localDateTime);

2.2、字符串转日期

LocalDateTime

String formatDate = "2023-08-30 15:41:55";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse(formatDate, dateTimeFormatter);

LocalDate

String formatDate = "2023-08-30";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.parse(formatDate, dateTimeFormatter);

MonthDay 

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd");
MonthDay monthDay = MonthDay.parse("08-02", dateTimeFormatter);
System.out.println(monthDay);

3、获取日期年月日时分秒

LocalDate

LocalDate localDate = LocalDate.of(2021, 8, 1);

System.out.println(localDate.getYear());
System.out.println(localDate.getDayOfYear());
System.out.println(localDate.getMonth());
System.out.println(localDate.getMonthValue());
System.out.println(localDate.getDayOfMonth());
System.out.println(localDate.getDayOfWeek());
System.out.println(localDate.lengthOfYear());

LocalDateTime

LocalDateTime localDateTime = LocalDateTime.of(2023, 2, 1, 13, 10, 18);

System.out.println(localDateTime.getYear());
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfWeek());
//返回日期是一年中的第几天
System.out.println(localDateTime.getDayOfYear());
//返回小时
System.out.println(localDateTime.getHour());
System.out.println(localDateTime.getMinute());
System.out.println(localDateTime.getSecond());

4、日期比较

判断相等

LocalDateTime localDateTime1 = LocalDateTime.parse("2023-08-02T13:10:28");
LocalDateTime localDateTime2 = LocalDateTime.parse("2023-08-02T13:10:28");
boolean result = localDateTime1.isEqual(localDateTime2);
System.out.println(result);

比较之前

LocalDate localDate1 = LocalDate.parse("2023-08-15");
LocalDate localDate2 = LocalDate.parse("2023-08-31");
boolean result = localDate1.isBefore(localDate2);
System.out.println(result);

比较之后

LocalDateTime localDateTime1 = LocalDateTime.parse("2023-08-15T13:10:28");
LocalDateTime localDateTime2 = LocalDateTime.parse("2023-08-02T13:10:28");
boolean result = localDateTime1.isAfter(localDateTime2);
System.out.println(result);

5、日期计算

当前月份的前1个月

minus 方法是减,第1个参数是数量,第2个参数是单位

String nowMonthAgo = YearMonth.now().minus(1, ChronoUnit.MONTHS).toString();
System.out.println(nowMonthAgo);

指定月份的前2个月

//指定月份前2个月
String someMonthAgo = YearMonth.parse("2021-12").minus(2, ChronoUnit.MONTHS).toString();
System.out.println(someMonthAgo);

指定月份的后3个月

plus方法是加,参数同上

//指定月份的后3个月
String someMonthAgo = YearMonth.parse("2023-08").plus(3, ChronoUnit.MONTHS).toString();
System.out.println(someMonthAgo);

当前时间的10天前

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.now().minusDays(10);
System.out.println(dateTimeFormatter.format(localDate));

当前时间的1个月前

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.now().minusMonths(1);
System.out.println(dateTimeFormatter.format(localDate));

当前时间的3天前

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.now().minus(3, ChronoUnit.DAYS);
System.out.println(dateTimeFormatter.format(localDate));

当前时间的10天后

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.now().plusDays(10);
System.out.println(dateTimeFormatter.format(localDate));

当前时间后2个月

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.now().plusMonths(2);
System.out.println(dateTimeFormatter.format(localDate));

当前时间的2小时前

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.now().minusHours(2);
System.out.println(dateTimeFormatter.format(localDateTime));

当前时间的3小时后

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.now().plusHours(3);
System.out.println(dateTimeFormatter.format(localDateTime));

当前时间的10分钟后

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(10);
System.out.println(dateTimeFormatter.format(localDateTime));

指定时间的10秒钟前

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse("2023-08-30 16:55:47", dateTimeFormatter).minusSeconds(10);
System.out.println(dateTimeFormatter.format(localDateTime));

指定时间的20分钟前

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse("2023-08-30 16:55:47", dateTimeFormatter).minus(20, ChronoUnit.MINUTES);
System.out.println(dateTimeFormatter.format(localDateTime));

指定时间的8天后

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse("2023-08-30 16:55:47", dateTimeFormatter).plus(8, ChronoUnit.DAYS);
System.out.println(dateTimeFormatter.format(localDateTime));

计算2日期相差天数

方法1

LocalDate localDate1 = LocalDate.of(2023, 8, 1);
LocalDate localDate2 = LocalDate.of(2023,8,10);
long days = ChronoUnit.DAYS.between(localDate1, localDate2);
System.out.println(days);

方法2

Period period = Period.between(LocalDate.of(2022, 8, 1),
                LocalDate.of(2023,8,30)
        );
//相差年数
System.out.println(period.getYears());
//相差月份数
System.out.println(period.getMonths());
//相差天数
System.out.println(period.getDays());
//相差天数
System.out.println(period.get(ChronoUnit.DAYS));

方法3

LocalDateTime localDateTime1 = LocalDateTime.of(2022, 8, 1, 17, 54, 10);
LocalDateTime localDateTime2 = LocalDateTime.of(2023,8,1, 17, 54, 10);

Instant instant1 = localDateTime1.toInstant(ZoneOffset.of("+8"));
Instant instant2 = localDateTime2.toInstant(ZoneOffset.of("+8"));

Duration duration = Duration.between(instant1, instant2);
//相差天数
System.out.println(duration.toDays());
//相差小时
System.out.println(duration.toHours());

6、时间戳

当前时间获取时间戳 Instant.now().toEpochMilli(); 和 (new Date()).getTime(); 效果一样

long time = Instant.now().toEpochMilli();
Date date = new Date();
System.out.println(time);
System.out.println(date.getTime());

使用 LocalDateTime 获取时间戳

LocalDateTime localDateTime = LocalDateTime.now();
long time = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
System.out.println(time);

指定时间获取时间戳

LocalDateTime localDateTime = LocalDateTime.parse("2023-08-30T17:01:50.656123800");
long time = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
System.out.println(time);
LocalDateTime localDateTime = LocalDateTime.parse("2023-08-30T17:01:50");
long time = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
System.out.println(time);

至此完

你可能感兴趣的:(java,java)