Java获取三个月的时间

Date dNow = new Date();   //当前时间  
      Date dBefore = new Date();  
      Calendar calendar = Calendar.getInstance(); //得到日历  
      calendar.setTime(dNow);//把当前时间赋给日历  
      calendar.add(Calendar.MONTH, -3);  //设置为前3月  
      dBefore = calendar.getTime();   //得到前3月的时间  
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式  
      String defaultStartDate = sdf.format(dBefore);    //格式化前3月的时间  
      String defaultEndDate = sdf.format(dNow); //格式化当前时间  
      System.out.println("三个月之前时间======="+defaultStartDate);  
      System.out.println("当前时间==========="+defaultEndDate); 
      
      Date createdDate = new Date();
      Date endDate = new Date();
      Date beforeNowTenDay = new Date(endDate.getTime() - 1000 * 60 * 60 * 24 * 30);
      Date startDate = createdDate.before(beforeNowTenDay) ? beforeNowTenDay : createdDate;
      Date nowDate = new Date();
      Date afterStartDateTenDay = new Date(startDate.getTime() + 1000 * 60 * 60 * 24 * 30);
      endDate = nowDate.before(afterStartDateTenDay) ? nowDate : afterStartDateTenDay;
      System.out.println("beforeNowTenDay===="+beforeNowTenDay); 
      System.out.println("afterStartDateTenDay===="+afterStartDateTenDay); 

你可能感兴趣的:(Java获取三个月的时间)