Calendar获取当前季度、月、周的开始时间结束时间

java在很多场合下会根据当前时间计算本季度的开始时间结束时间、当前月份的开始时间结束时间、当前周的开始时间以及结束时间。我这里整理下一个时间加工类,作为工具类记录下。

一、获取计算相关时间的方法如下:(如果有错误请指出,感谢!)

public class TimeUtils {

    /**
     * 初始化时间盒子
     * @return timeBox
     */
    public static TimeBox initTimeBox() throws ParseException {
        TimeBox timeBox = new TimeBox();
        Date currentDate = new Date();
        timeBox.setCurrentDate(currentDate);
        Date lastDate = getLastDate(currentDate);
        timeBox.setLastDate(lastDate);

        timeBox.setThisWeekStartDate(getCurrentWeekDateBegin(lastDate));
        timeBox.setThisWeekEndDate(getCurrentWeekDateEnd(lastDate));

        timeBox.setThisMonthStartDate(getCurrentMonthDateBegin(lastDate));
        timeBox.setThisMonthEndDate(getCurrentMonthDateEnd(lastDate));

        timeBox.setLastWeekStartDate(getLastWeekDateBegin(lastDate));
        timeBox.setLastWeekEndDate(getLastWeekDateEnd(lastDate));

        timeBox.setLastMonthStartDate(getLastMonthDateBegin(lastDate));
        timeBox.setLastMonthEndDate(getLastMonthDateEnd(lastDate));

        timeBox.setThisQuarterStartDate(getCurrentQuarterDateBegin(lastDate));
        timeBox.setThisQuarterEndDate(getCurrentQuarterDateEnd(lastDate));

        timeBox.setThisSundayDate(getCurrentWeekNumberDay(lastDate,0));
        timeBox.setLastSundayDate(getLastWeekDate(getCurrentWeekNumberDay(lastDate,0)));

        timeBox.setThisMondayDate(getCurrentWeekNumberDay(lastDate,1));
        timeBox.setLastMondayDate(getLastWeekDate(getCurrentWeekNumberDay(lastDate,1)));

        timeBox.setThisTuesdayDate(getCurrentWeekNumberDay(lastDate,2));
        timeBox.setLastTuesdayDate(getLastWeekDate(getCurrentWeekNumberDay(lastDate,2)));

        timeBox.setThisWednesdayDate(getCurrentWeekNumberDay(lastDate,3));
        timeBox.setLastWednesdayDate(getLastWeekDate(getCurrentWeekNumberDay(lastDate,3)));

        timeBox.setThisThursdayDate(getCurrentWeekNumberDay(lastDate,4));
        timeBox.setLastThursdayDate(getLastWeekDate(getCurrentWeekNumberDay(lastDate,4)));

        timeBox.setThisFridayDate(getCurrentWeekNumberDay(lastDate,5));
        timeBox.setLastFridayDate(getLastWeekDate(getCurrentWeekNumberDay(lastDate,5)));

        timeBox.setThisSaturdayDate(getCurrentWeekNumberDay(lastDate,6));
        timeBox.setLastSaturdayDate(getLastWeekDate(getCurrentWeekNumberDay(lastDate,6)));

        //确认当前周几,获取本周的所有日期


        timeBox.setStartDate(getEarliestDate(getLastMonthDateBegin(lastDate),getCurrentQuarterDateBegin(lastDate)));
        timeBox.setEndDate(getCurrentQuarterDateEnd(lastDate));



        return timeBox;
    }

    public static Date getEarliestDate(Date lastMonthStartDate, Date thisQuarterDateBegin) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = lastMonthStartDate;
        if (getDayDiffer( simpleDateFormat.parse(simpleDateFormat.format(thisQuarterDateBegin)),simpleDateFormat.parse(simpleDateFormat.format(lastMonthStartDate))) > 0){
            date = thisQuarterDateBegin;
        }
        return date;
    }

    /**
     * 获取昨天日期
     * @return date
     */
    public static Date getLastDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.se
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 可以使用datetime模块来获取当前季度的开始时间结束时间,具体实现如下: ```python import datetime # 获取当前日期 today = datetime.date.today() # 计算当前季度的开始时间 quarter_start_month = (today.month - 1) // 3 * 3 + 1 quarter_start_date = datetime.date(today.year, quarter_start_month, 1) # 计算当前季度结束时间 quarter_end_month = quarter_start_month + 2 last_day_of_quarter = datetime.date(today.year, quarter_end_month, 1) - datetime.timedelta(days=1) quarter_end_date = last_day_of_quarter print("当前季度的开始时间:", quarter_start_date) print("当前季度结束时间:", quarter_end_date) ``` 运行以上代码,可以得到当前季度的开始时间结束时间。例如,如果今天是2022年71日,那么输出结果如下: ``` 当前季度的开始时间: 2022-07-01 当前季度结束时间: 2022-09-30 ``` 注意,上述代码中计算季度的方式是按照1-3为第一季度,4-6为第二季度,7-9为第三季度,10-12为第四季度。如果需要按照其他方式计算季度,可以根据具体情况修改代码。 ### 回答2: 要获取当前季度的开始时间结束时间,可以使用Python的datetime模块来实现。 首先,我们需要导入datetime模块: ```python import datetime ``` 接下来,我们可以使用datetime模块中的now()函数获取当前日期时间: ```python current_date = datetime.datetime.now() ``` 然后,我们可以使用当前日期的份来计算当前季度的开始时间结束时间。假设我们采用季度计算如下: - 第一季度:11日至331日 - 第二季度:41日至630日 - 第三季度:71日至930日 - 第四季度:101日至1231日 我们可以按照上述定义来计算当前季度的开始时间结束时间: ```python quarter_start = datetime.datetime(current_date.year, 3 * ((current_date.month - 1) // 3) + 1, 1) quarter_end = quarter_start + datetime.timedelta(days=89) ``` 在上述代码中,我们首先计算季度的开始份,即`3 * ((current_date.month - 1) // 3) + 1`。然后,我们使用datetime.datetime函数传入年份、份和日期来构造开始时间:`datetime.datetime(current_date.year, 3 * ((current_date.month - 1) // 3) + 1, 1)`。 接着,我们使用timedelta函数来计算结束时间,根据季度开始时间的基础上加上89天:`quarter_start + datetime.timedelta(days=89)`。 最后,我们可以打印出当前季度的开始时间结束时间: ```python print("当前季度的开始时间:", quarter_start) print("当前季度结束时间:", quarter_end) ``` 这样就可以获取当前季度的开始时间结束时间了。 完整代码如下: ```python import datetime current_date = datetime.datetime.now() quarter_start = datetime.datetime(current_date.year, 3 * ((current_date.month - 1) // 3) + 1, 1) quarter_end = quarter_start + datetime.timedelta(days=89) print("当前季度的开始时间:", quarter_start) print("当前季度结束时间:", quarter_end) ``` 注意:上述代码中,我们假设了一年恰好有365天,没有考虑闰年的情况。在实际应用中,需要根据具体需求进行适当的修改。 ### 回答3: 要获取当前季度的开始时间结束时间,我们可以使用Python的datetime库来操作日期和时间。以下是一种实现方法: 首先,我们需要导入datetime和calendar模块: ``` import datetime import calendar ``` 然后,我们可以使用datetime.now()函数来获取当前日期和时间: ``` current_date = datetime.datetime.now() ``` 接下来,我们可以使用current_date的month属性来获取当前份: ``` current_month = current_date.month ``` 然后,我们可以使用当前份来计算当前季度的开始份和结束份: ``` start_month = (current_month - 1) // 3 * 3 + 1 end_month = start_month + 2 ``` 接着,我们可以使用calendar.monthrange()函数来获取指定份的天数: ``` start_day = 1 end_day = calendar.monthrange(current_date.year, end_month)[1] ``` 最后,我们可以使用datetime.datetime()函数来构造当前季度的开始时间结束时间: ``` start_date = datetime.datetime(current_date.year, start_month, start_day) end_date = datetime.datetime(current_date.year, end_month, end_day) ``` 现在,start_date和end_date分别是当前季度的开始时间结束时间。你可以根据需要对其进行进一步处理或打印输出。 注意:以上方法假设当前日期是以公历计算的。如需在其他历法中计算,请使用适当的模块和方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值