得到本日的前几年,前几月时间

/**
    * 某一个月第一天和最后一天
    * @param number
    * @return
    */
   public static Map<String, String>  getBeforeMonthDate(int number) {
       Calendar cal = Calendar.getInstance();
       cal.add(Calendar.MONTH, -number);
       Map map = getFirstday_Lastday_Month(cal.getTime());
       SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT2);
       return map;
   }
   private static Map<String, String> getFirstday_Lastday_Month(Date date) {
       SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
       Calendar calendar = Calendar.getInstance();
       calendar.setTime(date);
       Date theDate = calendar.getTime();

       //上个月第一天
       GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
       gcLast.setTime(theDate);
       gcLast.set(Calendar.DAY_OF_MONTH, 1);
       String day_first = df.format(gcLast.getTime());
       StringBuffer str = new StringBuffer().append(day_first).append(" 00:00:00");
       day_first = str.toString();

       //上个月最后一天
       calendar.add(Calendar.MONTH, 1);    //加一个月
       calendar.set(Calendar.DATE, 1);        //设置为该月第一天
       calendar.add(Calendar.DATE, -1);    //再减一天即为上个月最后一天
       String day_last = df.format(calendar.getTime());
       StringBuffer endStr = new StringBuffer().append(day_last).append(" 23:59:59");
       day_last = endStr.toString();

       Map<String, String> map = new HashMap<String, String>();
       map.put("first", day_first);
       map.put("last", day_last);
       return map;
   }

   /**
    * 得到本日的前几年时间
    *
    * @return
    */
   public static Map getDateBeforeYear(int number) {
       Calendar cal = Calendar.getInstance();
       cal.add(Calendar.YEAR, -number);
       SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT2);
       int year = Integer.parseInt(sdf.format(cal.getTime()).substring(0,4));
       String first = getYearFirst(year);
       String last = getYearLast(year);
       Map<String, String> map = new HashMap<String, String>();
       map.put("first", first);
       map.put("last", last);
       return map;
   }
   /**
         * 获取某年第一天日期
         * @param year 年份
         * @return Date
         */
   public static String getYearFirst(int year){
       Calendar calendar = Calendar.getInstance();
       calendar.clear();
       calendar.set(Calendar.YEAR, year);
       Date currYearFirst = calendar.getTime();
       SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT2);
       return sdf.format(currYearFirst);
   }
   /**
     * 获取某年最后一天日期
     * @param year 年份
     * @return Date
     */
       public static String getYearLast(int year){
           Calendar calendar = Calendar.getInstance();
           calendar.clear();
           calendar.set(Calendar.YEAR,year);
           calendar.roll(Calendar.DAY_OF_YEAR, -1);
           Date currYearLast = calendar.getTime();SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT2);
           return sdf.format(currYearLast);
       }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值