java 获取指定日前的前一天

  /**
     * @Author:
     * @Description:更加输入日期,获取输入日期的前一天
     * @Date:
     * @strData:参数格式:yyyy-MM-dd
     * @return:返回格式:yyyy-MM-dd
     */
    public static String getPreDateByDate(String strData) {
        String preDate = "";
         Calendar c = Calendar.getInstance();
         SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
         Date date = null;
         try {
             date = sdf.parse(strData);
         } catch (java.text.ParseException e) {
             e.printStackTrace();
         }
         
         c.setTime(date);
         int day1 = c.get(Calendar.DATE);
         c.set(Calendar.DATE, day1 - 1);
         preDate = sdf.format(c.getTime());
        return preDate;
    }
    

 

 import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;

//获取从2018-02-05到2018-10-15号之间所有的周一到周日的日期
    public static void getWeekdays(){
        String monitorDate = "2018-02-";
        String startDate = "";
        String endDate = "";
        String dateRange = "";
        int mon = 2;
        for(int i=5;i<=260;) {
            if (endDate.length() == 0) {
                startDate = monitorDate + i;
            } else{
                startDate = getSixthDateByDate(endDate,1);
            }
            endDate = getSixthDateByDate(startDate,6);
            System.out.println(startDate+": "+endDate);
            dateRange = startDate + endDate;
            dateRange= dateRange.replace("-","");
            System.out.println(dateRange);
            System.out.println("\n");
            i+=7;
        }
    }
    /**
     * @Author:
     * @Description:更加输入日期,获取输入日期后的第一天
     * @startData:参数格式:yyyy-MM-dd
     * @day:day=1,获取输入日期的第一天;day=2,获取输入日期的第二天;day=n,获取输入日期的第n天
     * @return:返回格式:yyyy-MM-dd
     */
    public static String getSixthDateByDate(String startData,int day) {
        String preDate = "";
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(startData);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

        c.setTime(date);
        int day1 = c.get(Calendar.DATE);
        //c.set(Calendar.DATE, day1 +6);获取输入日期后的第一天
        c.set(Calendar.DATE, day1 +day);
        preDate = sdf.format(c.getTime());
        return preDate;
    }

 

  //获取今天是星期几. 1:周一;2:周二;3:周三....以此类推
    public static int getCurrentDayofweek() {
        //今天是几号 
        Date date=new Date(); 
        Calendar c=Calendar.getInstance(); 
        c.setTime(date); 
        //今天是这个星期的第几天 
        int week=c.get(Calendar.DAY_OF_WEEK)-1; 
        return week;
    }

 

posted on 2018-09-17 17:14  shaomine  阅读(11468)  评论(0编辑  收藏  举报