localtime转为localdatetime 总有你会用到的
System.out.println("当前时间:" LocalDateTime.now());
,下面我们就来聊聊关于localtime转为localdatetime 总有你会用到的?接下来我们就一起去了解一下吧!
localtime转为localdatetime 总有你会用到的
获取当前时间
System.out.println("当前时间:" LocalDateTime.now());
System.out.println("LocalDateTime 转时间戳(毫秒):" LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
System.out.println("LocalDateTime 转时间戳(秒):" LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().getEpochSecond());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println("格式化后的日期:" LocalDateTime.now().format(formatter));
System.out.println("当天的零点: " LocalDateTime.of(LocalDateTime.now().toLocalDate(), LocalTime.MIN));
System.out.println("今天是星期(英文): " LocalDateTime.now().getDayOfWeek());
System.out.println("今天是星期(数字): " LocalDateTime.now().getDayOfWeek().getValue());
System.out.println("当前时间所属于的年份: " LocalDateTime.now().getYear());
System.out.println("当前时间所属于的月份: " LocalDateTime.now().getMonth().getValue());
System.out.println("当前时间所属于的天: " LocalDateTime.now().getDayOfMonth());
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));
System.out.println("本周周一的日期:" LocalDateTime.now().with(DayOfWeek.MONDAY).toLocalDate());
System.out.println("本周周一的日期:" LocalDateTime.now().with(DayOfWeek.SUNDAY).toLocalDate());
System.out.println("上周周一:" LocalDate.now().minusDays(7).with(DayOfWeek.MONDAY));
System.out.println("上周周日:" LocalDate.now().minusDays(7).with(DayOfWeek.SUNDAY));
System.out.println("当月第一天:" LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()));
System.out.println("当年第一天:" LocalDate.now().with(TemporalAdjusters.firstDayOfYear()));
System.out.println("下个月第一天:" LocalDate.now().with(TemporalAdjusters.firstDayOfNextMonth()));
System.out.println("明年第一天:" LocalDate.now().with(TemporalAdjusters.firstDayOfNextYear()));
System.out.println("构造时间(2022-12-12 12:12:12):" LocalDateTime.of(2022, 12, 12, 12, 12, 12));
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));
long timestamp = System.currentTimeMillis();
//时间戳转LocalDate
LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();
System.out.println("时间戳转LocalDate:" localDate);
//时间戳转LocalDateTime
ocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
System.out.println("时间戳转LocalDateTime:" localDateTime);
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().plusHours(1));//减少使用minusHours
System.out.println("当前时间增加一分钟:" LocalDateTime.now().plusMinutes(1));//减少使用minusMinutes
System.out.println("当前时间增加一秒钟:" LocalDateTime.now().plusSeconds(1));//减少使用minusSeconds
System.out.println("修改年份,修改成2023年:" LocalDateTime.now().withYear(2023));
System.out.println("修改月份,修改成1月份:" LocalDateTime.now().withMonth(1));
System.out.println("修改天数,修改成15号:" LocalDateTime.now().withDayOfMonth(15));
System.out.println("修改小时,修改成18点:" LocalDateTime.now().withHour(18));
System.out.println("修改分钟,修改成50分钟:" LocalDateTime.now().withMinute(50));
System.out.println("修改秒,修改成30秒:" LocalDateTime.now().withSecond(30));
LocalDate time1 = LocalDate.of(2022, 1, 1);
LocalDate time2 = LocalDate.of(2022, 1, 9);
System.out.println("time1 与 time2 相差:" Period.between(time1, time2).getDays() " 天");//相差月数:getMonths();相差年份:getYears()
Instant instant = new Date().toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDate nowDate = instant.atZone(zoneId).toLocalDate();
System.out.println("Date 转LocalDateTime:" nowDate);
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt = LocalDate.now().atStartOfDay().atZone(zoneId);
System.out.println("LocalDateTime 转 Date :" Date.from(zdt.toInstant()));
免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com