使用LocalDateTime的until方法计算相差的天,时,分,秒

    public static void main(String[] args) throws InterruptedException {
        LocalDateTime offWorkTime = LocalDateTime.now().withHour(17).withMinute(30).withSecond(0);
        for (; ; ) {
            Thread.sleep(1000);
            LocalDateTime currentTime = LocalDateTime.now();
            LocalDateTime tempDateTime = LocalDateTime.from(currentTime);
            long days = tempDateTime.until(offWorkTime, ChronoUnit.DAYS);
            tempDateTime = tempDateTime.plusDays(days);
            long hours = tempDateTime.until(offWorkTime, ChronoUnit.HOURS);
            tempDateTime = tempDateTime.plusHours(hours);
            long minutes = tempDateTime.until(offWorkTime, ChronoUnit.MINUTES);
            tempDateTime = tempDateTime.plusMinutes(minutes);
            long seconds = tempDateTime.until(offWorkTime, ChronoUnit.SECONDS);
            System.err.println("距离下班还有:" + days + " 天 " + hours + " 小时 " + minutes + " 分 " + seconds + " 秒。");
            if (currentTime.isAfter(offWorkTime)) {
                return;
            }
        }
    }
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值