LocalDateTime获取当日00:00、结束时间23.59与当月第一天00.00,月末最后一天23.59

本文介绍了如何使用Java的LocalDateTime获取当前日期的00:00和23:59,以及当月第一天和最后一天的00:00和23:59。详细阐述了获取这些时间点的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LocalDateTime获取当日00:00、结束时间23.59与当月第一天00.00,月末最后一天23.59

localDdate格式为字符串

//localDateTime格式为字符串
 DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 time.format(df)--------------2020-09-04 18:57:14

LocalDateTime 获取当天开始时间00.00 ~当天结束时间23.59

 /**
     * 获取当天开始时间00.00 ~当天结束时间23.59
     * @return
     */
    public static Map<String,String> today(){
        LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        DateTimeFormatter time = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String todayStart=time.format(today_start);
        String todayEnd = time.format(today_end);
        Map<String, String>  map = new HashMap<>();
        map.put("todayStart",todayStart);
        map.put("todayEnd",todayEnd);
        return map;
    }

LocalDateTime 获取本月第一天开始时间00.00 ~本月最后一天结束时间23.59

 /**
     *
     * 获取本月第一天开始时间00.00 ~本月最后一天结束时间23.59
     * @return
     */
    public static Map<String,String> moth(){
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime beginDateTime = now.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endDateTime = now.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
        DateTimeFormatter time = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startMoth = time.format(beginDateTime);
        String endMoth = time.format(endDateTime);
        Map<String, String>  map = new HashMap<>();
        map.put("startMoth",startMoth);
        map.put("endMoth",endMoth);
        return map;
    }

LocalDateTime 获取上个月第一天开始时间00.00 ~上个月最后一天结束时间23.59

 /**
     * 获取上个月第一天开始时间00.00 ~上个月最后一天结束时间23.59
     * @return
     */
public static Map<String,String> lastMoth(){
        LocalDateTime date = LocalDateTime.now().minusMonths(1);
        LocalDateTime firstDay = date.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
        LocalDateTime lastDay = date.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
        String startLastMonth = firstDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String endLastMonth = lastDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, String>  map = new HashMap<>();
        map.put("startLastMonth",startLastMonth);
        map.put("endLastMonth",endLastMonth);
        return map;
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值