一些时间的工具类

1.获得今天剩余的秒
2.获得某天的开始时间和结束时间

//获得今天剩余的秒

    public static Integer getRemainSecondsOneDay(Date currentDate) {
        Calendar midnight= Calendar.getInstance();
        midnight.setTime(currentDate);
        midnight.add(midnight.DAY_OF_MONTH,1);//将日加1
        midnight.set(midnight.HOUR_OF_DAY,0);//控制时
        midnight.set(midnight.MINUTE,0);//控制分
        midnight.set(midnight.SECOND,0);//控制秒
        midnight.set(midnight.MILLISECOND,0);//毫秒
        //通过以上操作就得到了一个currentDate明天的0时0分0秒的Calendar对象,然后相减即可得到到明天0时0点0分0秒的时间差
        Integer seconds=(int)((midnight.getTime().getTime()-currentDate.getTime())/1000);
        return seconds;
    }


    public static String getFrontStartNHour(Date currentDate,int D,int H) {
        Calendar calendar= Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(calendar.DAY_OF_MONTH,D);
        calendar.add(calendar.HOUR_OF_DAY,H);//
        calendar.set(calendar.MINUTE,0);//控制分
        calendar.set(calendar.SECOND,0);//控制秒
        calendar.set(calendar.MILLISECOND,0);//毫秒
        Date time = calendar.getTime();
        return dateFormat(time);
    }
    public static String getFrontEndNHour(Date currentDate,int D,int H) {
        Calendar calendar= Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(calendar.DAY_OF_MONTH,D);
        calendar.add(calendar.HOUR_OF_DAY,H);//
        calendar.set(calendar.MINUTE,59);//控制分
        calendar.set(calendar.SECOND,59);//控制秒
        calendar.set(calendar.MILLISECOND,999);//毫秒
        Date time = calendar.getTime();
        return dateFormat(time);
    }

    // 返回时间格式如:2020-02-17 00:00:00
    public  static String getStartOfDay(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }
    // 返回时间格式如:2020-02-17 23:59:59
    public  static String getEndOfDay(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }
 public static void main(String[] args) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime start = LocalDateTime.parse("2023-09-01 20:00:00", formatter);
            LocalDateTime end = LocalDateTime.parse("2023-09-11 20:00:00", formatter);

            int n = 20; // 分成n段
            Duration duration = Duration.between(start, end).dividedBy(n);

            List<LocalDateTime> segments = new ArrayList<>();
            for (int i = 0; i <= n; i++) {
                segments.add(start.plus(duration.multipliedBy(i)));
            }

            // 打印所有段
            for (LocalDateTime segment : segments) {
                System.out.println(segment.format(formatter));
            }
        }

public static void main(String[] args) {
        LocalDate today = LocalDate.now();

        LocalDate startOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        LocalDate endOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

        // 确保开始和结束日期在本月内
        if (!startOfWeek.getMonth().equals(today.getMonth())) {
            startOfWeek = today.withDayOfMonth(1);
        }
        if (!endOfWeek.getMonth().equals(today.getMonth())) {
            endOfWeek = today.withDayOfMonth(today.lengthOfMonth());
        }

        System.out.println("Start of week: " + startOfWeek);
        System.out.println("End of week: " + endOfWeek);
    }
public static List<String> spiltDate (String from,String to,Integer n){
        ArrayList<String> strings = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime start = LocalDateTime.parse(from, formatter);
        LocalDateTime end = LocalDateTime.parse(to, formatter);
        Duration duration = Duration.between(start, end).dividedBy(n);
        List<LocalDateTime> segments = new ArrayList<>();
        for (int i = 0; i <= n; i++) {
            segments.add(start.plus(duration.multipliedBy(i)));
        }
        // 打印所有段
        for (LocalDateTime segment : segments) {
            strings.add((segment.format(formatter)));
        }
        return strings;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值