博客
关于我
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中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>
MySQL中地理位置数据扩展geometry的使用心得
查看>>
Mysql中存储引擎简介、修改、查询、选择
查看>>
Mysql中存储过程、存储函数、自定义函数、变量、流程控制语句、光标/游标、定义条件和处理程序的使用示例
查看>>
mysql中实现rownum,对结果进行排序
查看>>
mysql中对于数据库的基本操作
查看>>
Mysql中常用函数的使用示例
查看>>
MySql中怎样使用case-when实现判断查询结果返回
查看>>
Mysql中怎样使用update更新某列的数据减去指定值
查看>>
Mysql中怎样设置指定ip远程访问连接
查看>>
mysql中数据表的基本操作很难嘛,由这个实验来带你从头走一遍
查看>>
Mysql中文乱码问题完美解决方案
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>