日期:2014-05-18 浏览次数:20807 次
/**
* 取得当前日期所在周的第一天
*/
public static String getFirstDayOfWeek(String date) throws ParseException {
Calendar c = new GregorianCalendar();
String []s=date.replace("-","/").split("/");
int year=Integer.parseInt(s[0]);
int m=Integer.parseInt(s[1]);
int day=Integer.parseInt(s[2]);
c.set(year, m-1, day);
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
String date2=c.getTime().toLocaleString().substring(0,9).trim();
String []s2=date2.replace("-","/").split("/");
int day2=Integer.parseInt(s2[2]);
// System.out.println("1"+day2);
if(day2<10)
{
return s2[0]+"-"+s2[1]+"-0"+s2[2];
}
return date2;
}
/**
* 取得当前日期所在上周的第一天
*/
public static String getLastWeek(String date) throws ParseException
{
date=getFirstDayOfWeek(date);
Calendar c = new GregorianCalendar();
String []s=date.replace("-","/").split("/");
int year=Integer.parseInt(s[0]);
int m=Integer.parseInt(s[1])-1;
int day=Integer.parseInt(s[2])-1;
if(day==0)//这里是由于可能指定的日期为这月的一号,所以他的上一周就在上个月的最后一周,因为28号是所有月份中比在最后一周的日子,所以取得28号所在的周就是上个月的最后一周了
{
m=m-1;
day=28;
}
c.set(year, m, day);
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
String date2=c.getTime().toLocaleString().substring(0,9).trim();
String []s2=date2.replace("-","/").split("/");
int day2=Integer.parseInt(s2[2]);
// System.out.println("2"+day2);
if(day2<10)
{
return s2[0]+"-"+s2[1]+"-"+s2[2];
}
return date2;
}
/**
* 取得指定日期所在周的最后一天
* @throws ParseException
*/
public static String getLastDayOfWeek(String date) throws ParseException {
Calendar c = new GregorianCalendar();
String []s=date.replace("-","/").split("/");
int year=Integer.parseInt(s[0]);
int m=Integer.parseInt(s[1]);
int day=Integer.parseInt(s[2]);
c.set(year, m-1, day);
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
String date2=c.getTime().toLocaleString().substring(0,9).trim();
String []s2=date2.replace("-","/").split("/");
in