/**
* 增加天数,减传负值
* @param format
* @param day
* @return
*/
public static String getDateBeforeForma(String format, int day){
DateFormat dateFormat = new SimpleDateFormat(format);
Calendar now = Calendar.getInstance();
now.add(Calendar.DATE, day);//增加一天 ,减传负值
Date date=now.getTime();
return dateFormat.format(date);
}
/**
* 获得本周第一天(周一)的日期
* @return
*/
public static String getWeekDay()
{
Calendar cal =Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //获取本周一的日期
String time = df.format(cal.getTime());
return time;
}
/**
* 获得上周时间
* @return
*/
public static String getLastWeek() {
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
Calendar date = Calendar.getInstance(Locale.CHINA);
date.setFirstDayOfWeek(Calendar.MONDAY);//将每周第一天设为星期一,默认是星期天
date.add(Calendar.WEEK_OF_MONTH,-1);//周数减一,即上周
date.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);//日子设为星期一
String time = df.format(date.getTime());
Calendar date2 = Calendar.getInstance(Locale.CHINA);
date2.setFirstDayOfWeek(Calendar.MONDAY);//将每周第一天设为星期一,默认是星期天
date2.add(Calendar.WEEK_OF_MONTH,-1);//周数减一,即上周
date2.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);//日子设为星期天
String time2 = df.format(date2.getTime());
return time + "-" + time2;
}
/**
* 获取某一时间段特定星期几的日期
* @param dateFrom 开始时间
* @param dateEnd 结束时间
* @param weekDays 星期
* @return 返回时间数组
*/
public static String[] getDates(String dateFrom, String dateEnd, String weekDays) {
long time = 1l;
long perDayMilSec = 24 * 60 * 60 * 1000;
List<String> dateList = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
//需要查询的星期系数
String strWeekNumber = weekForNum(weekDays);
try {
dateFrom = sdf.format(sdf.parse(dateFrom).getTime() - perDayMilSec);
while (true) {
time = sdf.parse(dateFrom).getTime();
time = time + perDayMilSec;
Date date = new Date(time);
dateFrom = sdf.format(date);
if (dateFrom.compareTo(dateEnd) <= 0) {
//查询的某一时间的星期系数
Integer weekDay = dayForWeek(date);
//判断当期日期的星期系数是否是需要查询的
if (strWeekNumber.indexOf(weekDay.toString())!=-1) {
dateList.add(dateFrom);
}
} else {
break;
}
}
} catch (ParseException e1) {
e1.printStackTrace();
}
String[] dateArray = new String[dateList.size()];
dateList.toArray(dateArray);
return dateArray;
}
//等到当期时间的周系数。星期日:1,星期一:2,星期二:3,星期三:4,星期四:5,星期五:6,星期六:7
public static Integer dayForWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_WEEK);
}
/**
* 得到对应星期的系数 星期日:1,星期一:2,星期二:3,星期三:4,星期四:5,星期五:6,星期六:7
* @param weekDays 星期格式 星期一|星期二
*/
public static String weekForNum(String weekDays){
//返回结果为组合的星期系数
String weekNumber = "";
//解析传入的星期
if(weekDays.indexOf("|")!=-1){//多个星期数
String []strWeeks = weekDays.split("\\|");
for(int i=0;i<strWeeks.length;i++){
weekNumber = weekNumber + "" + getWeekNum(strWeeks[i]).toString();
}
}else{//一个星期数
weekNumber = getWeekNum(weekDays).toString();
}
return weekNumber;
}
//将星期转换为对应的系数 星期日:1,星期一:2,星期二:3,星期三:4,星期四:5,星期五:6,星期六:7
public static Integer getWeekNum(String strWeek){
Integer number = 1;//默认为星期日
if("星期日".equals(strWeek)){
number = 1;
}else if("星期一".equals(strWeek)){
number = 2;
}else if("星期二".equals(strWeek)){
number = 3;
}else if("星期三".equals(strWeek)){
number = 4;
}else if("星期四".equals(strWeek)){
number = 5;
}else if("星期五".equals(strWeek)){
number = 6;
}else if("星期六".equals(strWeek)){
number = 7;
}else{
number = 1;
}
return number;
}
//根据年份和当年的第几周,获取这一周的日期
public static String getDayOfWeek(int year, int weekindex) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Calendar c = Calendar.getInstance();
c.setWeekDate(year, weekindex, 1);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 2;
c.add(Calendar.DATE, -dayOfWeek); // 得到本周的第一天
String begin = sdf.format(c.getTime());
c.add(Calendar.DATE, 6); // 得到本周的最后一天
String end = sdf.format(c.getTime());
String range = begin + "-" + end;
return range;
}