zoukankan      html  css  js  c++  java
  • java 获取当月第一天和最后一天 获取前一个月第一天和最后一天

      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
            
            //获取前月的第一天
            Calendar   cal_1=Calendar.getInstance();//获取当前日期 
            cal_1.add(Calendar.MONTH, -1);
            cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
            firstDay = format.format(cal_1.getTime());
            System.out.println("-----1------firstDay:"+firstDay);
            //获取前月的最后一天
            Calendar cale = Calendar.getInstance();   
            cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天 
            lastDay = format.format(cale.getTime());
            System.out.println("-----2------lastDay:"+lastDay);
            
            
            //获取当前月第一天:
            Calendar c = Calendar.getInstance();    
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
            String first = format.format(c.getTime());
            System.out.println("===============first:"+first);
            
            //获取当前月最后一天
            Calendar ca = Calendar.getInstance();    
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));  
            String last = format.format(ca.getTime());
            System.out.println("===============last:"+last);

  • 相关阅读:
    winform 剔除空格与换行显示
    编码
    todo
    react高阶函数组件
    Docker-compose Setup for Self-hosting Development & Deployment Tools
    Self-hosting Sentry With Docker and Docker-compose
    how does Array.prototype.slice.call() work?
    todo reading
    a better git log
    https://coderwall.com/p/7smjkq/multiple-ssh-keys-for-different-accounts-on-github-or-gitlab
  • 原文地址:https://www.cnblogs.com/huideng/p/4465180.html
Copyright © 2011-2022 走看看