JAVA使用Localtime_java8新特性 LocalDateTime,LocalDate ,LocalTime 使用

LocalDateTime使用

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

//获取当前时间

LocalDateTime date=LocalDateTime.now();

System.out.println("YEAR"+date.getYear());

System.out.println("MONTH"+date.getMonth());

System.out.println("DAY"+date.getDayOfMonth());

System.out.println("HOUR"+date.getHour());

System.out.println("MINUTES"+date.getMinute());

System.out.println("SECOND"+date.getSecond());

System.out.println("localDateTime"+dateTimeFormatter.format(date));

//当前年的第一天

LocalDateTime firstdayOfYear =date.with(TemporalAdjusters.firstDayOfNextYear());

System.out.println("firstdayOfYear"+dateTimeFormatter.format(firstdayOfYear));

//当前年的最后一天

LocalDateTime enddayOfYear =date.with(TemporalAdjusters.lastDayOfYear());

System.out.println("enddayOfYear"+dateTimeFormatter.format(enddayOfYear));

//获取当前月的第一天

LocalDateTime firstdayOfMonth =date.with(TemporalAdjusters.firstDayOfMonth());

System.out.println("firstdayOfMonth"+dateTimeFormatter.format(firstdayOfMonth));

//获取当前月的最后一天

LocalDateTime endDayOfMonth=date.with(TemporalAdjusters.lastDayOfMonth());

System.out.println("endDayOfMonth"+dateTimeFormatter.format(endDayOfMonth));

//获取下个月第一天

LocalDateTime firstdayOfNextMonth=date.with(TemporalAdjusters.firstDayOfNextMonth());

System.out.println("firstdayOfNextMonth"+dateTimeFormatter.format(firstdayOfNextMonth));

//月增加一个月

LocalDateTime addMonth=date.plusMonths(1);

System.out.println("addMonth"+dateTimeFormatter.format(addMonth));

//月减少一个月

LocalDateTime lessenMonth=date.minusMonths(1);

System.out.println("lessenMonth"+dateTimeFormatter.format(lessenMonth));

//天增加一天

LocalDateTime addOfDay=date.plusDays(1);

System.out.println("addOfDay"+dateTimeFormatter.format(addOfDay));

//天减少一天

LocalDateTime lessOfDay=date.minusDays(1);

System.out.println("lessOfDay"+dateTimeFormatter.format(lessOfDay));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值