LocalDataTime的使用

LocalDataTime

1、获取当前时间-构造时间-格式化时间

LocalDataTime data = LocalDataTime.now();//年月日时分秒

LocalData data = LocalData.now();//年月日

LocalTime data = LocalTime.now();//时分秒

System.out.println("构造时间(2023-05-05 01:01:01):" + LocalDateTime.of(2023, 5, 5, 12, 1, 1));

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

System.out.println("格式化后的日期:" + LocalDateTime.now().format(formatter));

2、获取当前时间的时间戳

LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());//毫秒

LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().getEpochSecond());//秒

3、年-月-日 拼接 时:分:秒

LocalData data = LocalData.now();

LocalTime timeEnd = LocalTime.of(23, 59, 59);

LocalDateTime monday =  LocalDateTime.of(data, timeEnd );

4、获取当前日期所在年、月、日、周

System.out.println("当前时间所属于的年份: " + LocalDateTime.now().getYear());

System.out.println("当前时间所属于的月份: " + LocalDateTime.now().getMonth().getValue());

System.out.println("当前时间所属于的天: " + LocalDateTime.now().getDayOfMonth());

System.out.println("当前时间所属于的周: " + WeekFields.of(DayOfWeek.MONDAY,1););

5、获取当前时间所在 周开始-结束时间--星期几

System.out.println("本周周一的日期:" + LocalDate.now().with(weekFields.dayOfWeek(), 1L));

System.out.println("本周周天的日期:" + LocalDate.now().with(weekFields.dayOfWeek(), 7L));

System.out.println("本周周一的日期:" + LocalDateTime.now().with(DayOfWeek.MONDAY).toLocalDate());

System.out.println("本周周天的日期:" + LocalDateTime.now().with(DayOfWeek.SUNDAY).toLocalDate());

System.out.println("今天是星期(英文): " + LocalDateTime.now().getDayOfWeek());

System.out.println("今天是星期(数字): " + LocalDateTime.now().getDayOfWeek().getValue());

6、获取当前时间所在月开始、结束时间 

LocalDateTime now = LocalDateTime.now();

LocalDateTime startDateTime = now.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);

LocalDateTime endDateTime = now.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);

//手动设置时分秒

LocalDateTime startDateTime = req.getBeginTime().with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);

LocalDateTime endDateTime = req.getBeginTime().with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);

7、判断时间前后关系和是否相等

LocalDateTime date = LocalDateTime.of(2022, 8, 14, 14, 10, 55);

System.out.println("当前时间是否在 date 之前 " + LocalDateTime.now().isBefore(date));

System.out.println("当前时间是否在 date 之后 " + LocalDateTime.now().isAfter(date));



LocalDateTime date1 = LocalDateTime.of(2022, 8, 14, 12, 12, 12);

LocalDateTime date2 = LocalDateTime.of(2022, 8, 14, 16, 12, 12);

System.out.println("date1 == date2  ? " + date1.isEqual(date2));

8、获取两个时间间隔

LocalDate time1 = LocalDate.of(2023,5, 1);

LocalDate time2 = LocalDate.of(2022, 5, 5);

System.out.println("time1 与 time2 相差:" + Period.between(time1, time2).getDays() + " 天");//相差月数:getMonths();相差年份:getYears()

9、时间的修改-增加-减少:年、月、日、周、时、分、秒

System.out.println("当前时间增加一年:" + LocalDateTime.now().plusYears(1));//减少使用minusYears

System.out.println("当前时间增加一个月:" + LocalDateTime.now().plusMonths(1));//减少使用minusMonths (增加的一个月不能比当前月天数多)

System.out.println("当前时间增加一日:" + LocalDateTime.now().plusDays(1));//减少使用minusDays

System.out.println("当前时间增加一周:" + LocalDateTime.now().plusWeeks(1));//减少使用minusYears

System.out.println("当前时间增加一个小时:" + LocalDateTime.now().plusHours(1));//减少使用minusHours

System.out.println("当前时间增加一分钟:" + LocalDateTime.now().plusMinutes(1));//减少使用minusMinutes

System.out.println("当前时间增加一秒钟:" + LocalDateTime.now().plusSeconds(1));//减少使用minusSeconds

withDayOfMonth(i) 设置默认日期

System.out.println("修改年份,修改成2023年:" + LocalDateTime.now().withYear(2023));

System.out.println("修改月份,修改成5月份:" + LocalDateTime.now().withMonth(5));

System.out.println("修改天数,修改成15号:" + LocalDateTime.now().withDayOfMonth(15));

System.out.println("修改小时,修改成15点:" + LocalDateTime.now().withHour(15));

System.out.println("修改分钟,修改成50分钟:" + LocalDateTime.now().withMinute(50));

System.out.println("修改秒,修改成50秒:" + LocalDateTime.now().withSecond(50));

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.aapoint.util; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAdjusters; public class LocalDateTimeUtil { /** * 比较 localDateTime2 是否在localDateTime1之前(比较大小) * @param localDateTime1 * @param localDateTime2 * @return */ public static Boolean compare(LocalDateTime localDateTime1,LocalDateTime localDateTime2){ return localDateTime1.isBefore(localDateTime2); } /** * 获取当前月份前/后的月份的第一天 * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String firstDay(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,0,i); //获取该月份的第一天 String firstDay = date.with(TemporalAdjusters.firstDayOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // System.out.println("第一天为:"+firstDay); return firstDay; } /** * 获取当前月份前/后的月份的最后一天 * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String lastDay(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,0,i); //获取该月份的最后一天 String lastDay = date.with(TemporalAdjusters.lastDayOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // System.out.println("最后一天为:"+lastDay); return lastDay; } /** * 获取当时间前/后的时间(天) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainDay(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,1,i); //获取天 String day = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // System.out.println("获取的时间为(天):"+day); return day; } /** * 获取当时间前/后的时间(小时) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainHours(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,2,i); //获取该月份的最后一天 String hours = date.format(DateTimeFormatter.ofPattern("HH:mm:ss")); // System.out.println("获取的时间为(小时):"+hours); return hours; } /** * 获取当时间前/后的时间(小时) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainMinutes(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,3,i); //获取该月份的最后一天 String minutes = date.format(DateTimeFormatter.ofPattern("HH:mm:ss")); // System.out.println("获取的时间为(分钟):"+minutes); return minutes; } /** * 获取当时间前/后的时间(小时) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainSeconds(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,4,i); //获取该月份的最后一天 String seconds = date.format(DateTimeFormatter.ofPattern("HH:mm:ss")); // System.out.println("获取的时间为(秒):"+seconds); return seconds; } public static void main(String[] args) { System.out.println("当前时间为:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); System.out.println("前一个月份的第一天为:"+LocalDateTimeUtil.firstDay(1,1)); System.out.println("前一个月份的最后一天为:"+LocalDateTimeUtil.lastDay(1,1)); System.out.println("当前时间的前一天为:"+LocalDateTimeUtil.obtainDay(1,1)); System.out.println("当前时间的后一天为:"+LocalDateTimeUtil.obtainDay(2,1)); System.out.println("当前时间的前一小时为:"+LocalDateTimeUtil.obtainHours(1,1)); System.out.println("当前时间的后一小时为:"+LocalDateTimeUtil.obtainHours(2,1)); System.out.println("当前时间的前一分钟为:"+LocalDateTimeUtil.obtainMinutes(1,1)); System.out.println("当前时间的后一分钟为:"+LocalDateTimeUtil.obtainMinutes(2,1)); System.out.println("当前时间的前一秒为:"+LocalDateTimeUtil.obtainSeconds(1,1)); System.out.println("当前时间的后一秒为:"+LocalDateTimeUtil.obtainSeconds(2,1)); } private static LocalDateTime getLocalDateTime(Integer state,Integer type,Integer i) { LocalDateTime date; if(state == 0){ date = LocalDateTime.now(); }else if(state == 1){ if(type == 0) { //获取月 date = LocalDateTime.now().minusMonths(i); }else if(type == 1){ //获取天 date = LocalDateTime.now().minusDays(i); }else if(type == 2){ //获取小时 date = LocalDateTime.now().minusHours(i); }else if(type == 3){ //获取分钟 date = LocalDateTime.now().minusMinutes(i);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值