根据当前系统时间获取时间区间

根据系统时间获取时间区间:上月23 00:00:00-当月22 23:59:59
	DateTime dateTime = new DateTime(2018,1,29);
        Integer year = dateTime.getYear();
        Integer month = dateTime.getMonth();
        Integer day = dateTime.getDay();

        Integer lastYear = year,lastMonth = month;
        if(day < 23) {
            month = month == 1 ? 12 : month -1;
            year = month == 12 ? year - 1 : year;
        }
        lastMonth = month == 1 ? 12 : month - 1;
        lastYear = lastMonth == 12 ? year - 1 : year;
        String lastMonthStr = lastMonth < 10 ? "0" + lastMonth : "" + lastMonth;
        String startDate = "" + lastYear + "-" + lastMonthStr + "-23 00:00:00" ;
        String monthStr = month < 10 ? "0" + month : "" + month;
        String endDate = "" + year + "-" + monthStr + "-22 23:59:59";

        System.out.println(startDate + "-" + endDate);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要获当天的时间区间,可以使用LocalDateTime和LocalTime类来实现。首先,我们可以使用LocalDateTime类的now()方法获当前日期和时间。然后,我们可以将时间部分设置为当天的最早时间,使用LocalTime类的MIN常量来表示00:00:00。接下来,我们可以将时间部分设置为当天的最晚时间,使用LocalTime类的MAX常量来表示23:59:59.99999999。最后,我们可以将日期和时间组合成一个完整的时间区间。以下是代码示例: ```java LocalDateTime startOfDay = LocalDateTime.of(LocalDate.now(), LocalTime.MIN); LocalDateTime endOfDay = LocalDateTime.of(LocalDate.now(), LocalTime.MAX); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String startOfDayFormatted = startOfDay.format(formatter); String endOfDayFormatted = endOfDay.format(formatter); System.out.println("当天的时间区间是:" + startOfDayFormatted + " - " + endOfDayFormatted); ``` 这段代码会输出当前日期的时间区间,格式为"yyyy-MM-dd HH:mm:ss"。其中,startOfDayFormatted表示当天的最早时间,endOfDayFormatted表示当天的最晚时间。 请注意,这个时间区间是基于系统默认时区的。如果你想使用其他时区,可以在now()方法中指定一个不同的时钟(Clock)对象来获得对应时区的时间。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [LocalDateTime类获当日00:00与当月第一天](https://blog.csdn.net/qq_41267618/article/details/102914243)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值