获取某年某月的月初和月末时间

本文介绍了一段Java代码,用于获取特定年份和月份的开始和结束日期,包括如何使用Calendar和SimpleDateFormat来精确到秒。
private static Map<String,String> getMonthStartAndEnd(Integer year,Integer month){

    SimpleDateFormat format = new SimpleDateFormat(DateUtils.DATETIME_PATTERN_DATE);
    //获取前月的第一天
    Calendar cal_1=Calendar.getInstance();//获取当前日期
    cal_1.set(Calendar.YEAR,year);
    cal_1.set(Calendar.MONTH,month-1);
    cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
    String firstDay = format.format(cal_1.getTime());
    firstDay += " 00:00:00";
    //获取前月的最后一天
    Calendar cale = Calendar.getInstance();
    cale.set(Calendar.YEAR,year);
    cale.set(Calendar.MONTH,month);
    cale.set(Calendar.DAY_OF_MONTH,0);
    String lastDay = format.format(cale.getTime());
    lastDay += " 23:59:59";

    Map<String, String> firstAndLast = new HashMap<>();
    firstAndLast.put("firstDay",firstDay);
    firstAndLast.put("lastDay",lastDay);
    return  firstAndLast;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值