*测试
*@paramargs
*/
publicstaticvoid main(String[] args) {
// 定义输出日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date currentDate = new Date();
List
String nowDate =sdf.format(currentDate);
System.out.println("今天的日期: " + nowDate);
for (Date date : days) {
StringtempDate = sdf.format(date);
if(compare(nowDate,tempDate ,"yyyy-MM-dd") == 1){
System.out.println(tempDate);
}
}
}
/**
*根据日期获得所在周的日期
*@parammdate
*@return
*/
@SuppressWarnings("deprecation")
publicstatic List
int b = mdate.getDay();
Date fdate;
List
Long fTime = mdate.getTime() - b * 24 *3600000;
for (int a = 1; a <= 7; a++) {
fdate = new Date();
fdate.setTime(fTime + (a * 24 *3600000));
list.add(a-1, fdate);
}
return list;
}
/**
*两个字符串格式的日期(能转化为java.util.Date的格式)大小比较
*@paramdate1日期一(格式:yyyy-MM-dd或yyyy-MM)
*@paramdate2日期二(格式:yyyy-MM-dd或yyyy-MM)
*@paramformat指定的日期格式
*@returnint1:date1>date2,-1:date1
publicstaticint compare(String date1, String date2, String format)
{
if (null == date1 || null == date2)
return 0;
DateFormat df = new SimpleDateFormat(format);
Date jDate1 = null;
Date jDate2 = null;
try
{
if (format.equals("yyyy-MM-dd"))//
{
jDate1 = df.parse(date1 + "-01");
jDate2 = df.parse(date2 + "-01");
}
else
{
jDate1 = df.parse(date1);
jDate2 = df.parse(date2);
}
}
catch (ParseException e)
{
return -2;
}
if (jDate1.getTime() > jDate2.getTime())
return 1;
elseif (jDate1.getTime() < jDate2.getTime())
return -1;
else
return 0;
}
输出结果为:
今天的日期: 2013-07-31
2013-07-29
2013-07-30