Java8提供了新的时间接口LocalDateTime。本文主要介绍了Java8中LocalDateTime的一些常用操作方法。不多说,直接上代码。欲知详情,可以看看官网。
Java8提供了新的时间接口LocalDateTime。本文主要介绍了Java8中LocalDateTime的一些常用操作方法。不多说,直接上代码。欲知详情,可以看看官网。
LocalDateTime localDateTime = LocalDateTime.now();
//时间运算,相加相减
System.out.println(localDateTime.plusYears(2)); //加2年
System.out.println(localDateTime.plusDays(2)); //加两天
System.out.println(localDateTime.minusYears(2)); //减两年
System.out.println(localDateTime.minusDays(2)); //减两天
System.out.println(localDateTime.toString()); // 转字符串 结果:2021-11-24T15:36:12.914
System.out.println(localDateTime.toLocalDate()); //获取日期(LocalDate) 结果:2021-11-24
System.out.println(localDateTime.toLocalTime()); //获取时间(LocalTime) 结果:15:36:12.914
System.out.println(localDateTime.getDayOfMonth()); //获取当前时间月份的第几天 结果:24
System.out.println(localDateTime.getDayOfWeek()); //获取当前周的第几天 结果:WEDNESDAY
System.out.println(localDateTime.getDayOfYear()); //获取当前时间在该年属于第几天 结果:328
System.out.println(localDateTime.getMonthValue()); // 获取当前时间的月份(阿拉伯文) 结果:11
System.out.println(localDateTime.getMonth()); // 获取当前时间的月份(英文) 结果:11
System.out.println(localDateTime.getHour()); // 获取当前时间的小时数 结果:15
System.out.println(localDateTime.getMinute()); // 获取当前时间的分钟数 结果:36
//格式化输出
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY/MM/dd HH:mm:ss");
System.out.println(localDateTime.format(formatter)); // 结果:2021/11/24 15:36:12
//构造时间
LocalDateTime startTime = LocalDateTime.of(2021, 1, 1, 20, 31, 20);
LocalDateTime endTime = LocalDateTime.of(2021, 1, 3, 20, 31, 20);
//比较时间
System.out.println(localDateTime.isAfter(startTime)); // 结果:true
System.out.println(localDateTime.isBefore(endTime)); // 结果:false
//获取毫秒数(使用Instant)
System.out.println(localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); // 结果:1637739570129
//获取秒数(使用Instant)
System.out.println(localDateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond()); // 结果:1637739570
// 获取当前时间的0点~23点
LocalDateTime beginDay = localDateTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
LocalDateTime endDay = localDateTime.withHour(23).withMinute(59).withSecond(59);
// 获取本月的第一天的0点0分0秒和最后一天的23点59分59秒
LocalDateTime beginMonth = localDateTime.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
LocalDateTime endMonth = localDateTime.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
// LocalDateTime转Date
Date date = Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));
// Date转LocalDateTime
date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
// LocalDateTime获取秒数
Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
// LocalDateTime获取毫秒数
Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
效果如下:
https://github.com/toutouge/javademosecond/tree/master/hellolearn
作 者: 请叫我头头哥
出 处:http://www.cnblogs.com/toutou/
关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信我
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角 【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!
Original: https://www.cnblogs.com/toutou/p/LocalDateTime.html
Author: 请叫我头头哥
Title: Java LocalDateTime实用方法
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/586990/
转载文章受原作者版权保护。转载请注明原作者出处!