获取当天时间段,前一天时间时间段,截取固定的时间段

//获取当天的时间段

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd ");
    Date date = new Date(System.currentTimeMillis());
    String fmtdt = simpleDateFormat.format(date);
    String StartTime = fmtdt +"00:00:00";
    String EndTime = fmtdt +"23:59:59";

//获取昨天的时间段

    Calendar ca = Calendar.getInstance();// 得到一个Calendar的实例
    ca.setTime(new Date());// 设置时间为当前时间
    //ca.add(Calendar.YEAR,-1);// 年份减1  
    //ca.add(Calendar.MONTH,-1);// 月份减1  
    ca.add(Calendar.DATE,-1);// 日期减1  
    Date resultDate = ca.getTime();//结果
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd ");
    String fmtdt = simpleDateFormat.format(resultDate);
    String StartTime = fmtdt+"00:00:00";
    String EndTime = fmtdt+"23:59:59";

//截取固定的时间段

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String sbstg = dateString.substring(11, 18);

//前几天的时间段

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    String startTime = df.format(new Date().getTime()-3*24*60*60*1000);
    String endTime = df.format(new Date());

你可能感兴趣的:(获取当天时间段,前一天时间时间段,截取固定的时间段)