学习自:https://blog.csdn.net/dongyuxu342719/article/details/78131697

开发时遇到的,十二个月就到二月份有问题,刚开始以为是自己改时间戳整的,结果发现并不是,后加了一段代码解决(图中!!!!就是),好像意思是清缓存。

public Long getLastDayOfMonth(int year,int month){
        Long lastDays =0l;
        Calendar cal = Calendar.getInstance();
        cal.clear();  //!!!!!!!!!!!!!!!!!!!!!!!!!
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        try {
            lastDays = sdf.parse(lastDayOfMonth).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return lastDays;
    }

 

相关文章:

  • 2021-08-09
  • 2021-06-22
  • 2021-08-11
  • 2021-07-09
  • 2021-07-01
  • 2021-11-15
  • 2021-08-03
  • 2021-04-25
猜你喜欢
  • 2022-12-23
  • 2021-11-09
  • 2022-12-23
  • 2021-07-24
  • 2022-12-23
  • 2022-02-28
  • 2022-01-09
相关资源
相似解决方案