import java.text.parseexception;
import java.text.simpledateformat;
import java.util.calendar;
import java.util.date;
public class datecomparedemo {
//得到以後的日期
public date datecompare(string str, int month,int day) throws parseexception {
date date = null;
simpledateformat df = new simpledateformat("yyyy-mm-dd" );
date = df.parse(str);
calendar c = calendar. getinstance();
c.settime(date);
c.add(calendar. month, month);
c.add(calendar. day_of_month, day);
return c.gettime();
}
public static void main(string[] args) throws parseexception {
datecomparedemo dcd = new datecomparedemo();
string a = "2010-01-01";
date b = dcd.datecompare( "2010-05-03", 0, 0);
date a1 = dcd.datecompare(a, 3, 0); //得到a三個月以後的日期
date a2 = dcd.datecompare(a, 0, 5); //得到a五天後的日期
int result1 = a1.compareto(b);
int result2 = a2.compareto(b);
if(result1 < 0) {
system. out.println("a三個月以後的日期比b小" );
} else {
system. out.println("a三個月以後的日期比b大" );
}
if(result2 <0) {
system. out.println("a五天後的日期比b小" );
system. out.println("a五天後的日期比b大" );
}
特别說明:尊重作者的勞動成果,轉載請注明出處哦~~~
http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt222