博客
关于我
LocalDateTime基本使用(包括Data转化)
阅读量:90 次
发布时间:2019-02-26

本文共 4877 字,大约阅读时间需要 16 分钟。

Java LocalDateTime 时间操作指南

1. 时间初始化

在Java中,可以通过 LocalDateTime 类来获取和操作当前时间。以下是基本操作的示例:

// 获取当前时间LocalDateTime nowTime = LocalDateTime.now();System.out.println("当前时间:" + nowTime);// 设置指定时间LocalDateTime endTime = LocalDateTime.of(2020, 5, 20, 5, 20, 10, 0);System.out.println("指定时间:" + endTime);

2. 时间比较

可以通过 LocalDateTime 类提供的方法来比较时间:

// 比较当前时间与指定时间的关系boolean isBefore = nowTime.isBefore(endTime);System.out.println("当前时间是否比指定时间早:" + isBefore);// 判断当前时间是否与指定时间相等boolean equal = nowTime.equals(endTime);System.out.println("当前时间是否与指定时间相等:" + equal);// 判断当前时间是否比指定时间晚boolean isAfter = nowTime.isAfter(endTime);System.out.println("当前时间是否比指定时间晚:" + isAfter);

3. 时间获取

可以通过 LocalDateTime 类获取时间的各个部分:

// 获取时间的各个部分int year = nowTime.getYear();int monthValue = nowTime.getMonthValue();int dayofMonth = nowTime.getDayOfMonth();int hour = nowTime.getHour();int minute = nowTime.getMinute();int second = nowTime.getSecond();System.out.println("获取时间:" + year + "年" + monthValue + "月" + dayofMonth + "日" + hour + "时" + minute + "分" + second + "秒");// 获取星期几DayOfWeek dayofWeek = nowTime.getDayOfWeek();System.out.println("今天星期:" + dayofWeek);// 获取当前日子为年的第几天int dayofYear = nowTime.getDayOfYear();System.out.println("全年第:" + dayofYear + "天");

4. 时间操作

可以通过加减方法来调整时间:

// 日期偏移操作LocalDateTime minusYears = nowTime.minusYears(1);System.out.println("1年前的时间:" + minusYears);LocalDateTime plusYears = nowTime.plusYears(1);System.out.println("1年后的时间:" + plusYears);LocalDateTime minusMonths = nowTime.minusMonths(1);System.out.println("1个月前的时间:" + minusMonths);LocalDateTime plusMonths = nowTime.plusMonths(1);System.out.println("1个月后的时间:" + plusMonths);LocalDateTime minusWeeks = nowTime.minusWeeks(1);System.out.println("1周前的时间:" + minusWeeks);LocalDateTime plusWeeks = nowTime.plusWeeks(1);System.out.println("1周后的时间:" + plusWeeks);LocalDateTime minusDays = nowTime.minusDays(1);System.out.println("1天前的时间:" + minusDays);LocalDateTime plusDays = nowTime.plusDays(1);System.out.println("1天后的时间:" + plusDays);LocalDateTime minusHours = nowTime.minusHours(1);System.out.println("1小时前的时间:" + minusHours);LocalDateTime plusHours = nowTime.plusHours(1);System.out.println("1小时后的时间:" + plusHours);LocalDateTime minusMinutes = nowTime.minusMinutes(1);System.out.println("1分钟前的时间:" + minusMinutes);LocalDateTime plusMinutes = nowTime.plusMinutes(1);System.out.println("1分钟后的时间:" + plusMinutes);LocalDateTime minusSeconds = nowTime.minusSeconds(1);System.out.println("1秒前的时间:" + minusSeconds);LocalDateTime plusSeconds = nowTime.plusSeconds(1);System.out.println("1秒后的时间:" + plusSeconds);LocalDateTime minusTime = nowTime.minus(23, ChronoUnit.MONTHS);System.out.println("减少23个月后的时间:" + minusTime);

5. 时间做差

可以使用 Duration 类来计算时间差:

// 计算时间差Duration duration = Duration.between(nowTime, endTime);// 转换为天数long durationDays = duration.toDays();System.out.println("时间差:" + durationDays + "天");// 转换为小时数long durationHours = duration.toHours();System.out.println("时间差:" + durationHours + "小时");// 转换为分钟数long durationMinutes = duration.toMinutes();System.out.println("时间差:" + durationMinutes + "分钟");// 转换为毫秒数long durationMillis = duration.toMillis();System.out.println("时间差:" + durationMillis + "毫秒");// 转换为纳秒数long durationNanos = duration.toNanos();System.out.println("时间差:" + durationNanos + "纳秒");

6. Period 时间差

Period 类用于表示两个日期之间的时间间隔:

// 计算时间差Period period = Period.between(nowTime.toLocalDate(), endTime.toLocalDate());// 获取时间差的各个部分int periodYears = period.getYears();System.out.println("时间差:" + periodYears + "年");int periodMonths = period.getMonths();System.out.println("时间差:" + periodMonths + "月");int periodDays = period.getDays();System.out.println("时间差:" + periodDays + "日");

7. 获得特殊时间

可以通过 LocalTime 来获取特殊时间:

// 获取当天的零点LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);System.out.println("当天零点:" + todayStart);// 获取当天的结束时间LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);System.out.println("当天结束时间:" + todayEnd);

8. LocalDateTime 转化

可以通过 DateTimeFormatterLocalDateTime 转换为字符串:

// LocalDateTime 转 StringString localDataTimeString = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));System.out.println("LocalDateTime 转 String:" + localDataTimeString);// String 转 LocalDateTimeLocalDateTime stringLocalDataTime = LocalDateTime.parse("2017-09-28 17:07:05", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));System.out.println("String 转 LocalDateTime:" + stringLocalDataTime);

9. Date 转化

可以通过 ZoneId 来将 LocalDateTime 转换为 Date

// LocalDateTime 转 DateDate date = Date.from(nowTime.atZone(ZoneId.systemDefault()).toInstant());LocalDateTime dateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());System.out.println("Date 转 LocalDateTime:" + dateTime);// LocalDateTime 转 StringString dataString = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));System.out.println("LocalDateTime 转 String:" + dataString);

转载地址:http://frpk.baihongyu.com/

你可能感兴趣的文章
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>