import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test7 {
public static void main(String[] args) {
try {
//字符串转日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse("2017-05-26");
System.out.println(date.toString());//Fri May 26 00:00:00 CST 2017
//日期转字符串
String s = sdf.format(date);
System.out.println(s);//2017-05-26
//比较两个日期的大小
Date d1 = sdf.parse("2017-05-26");
Date d2 = sdf.parse("2017-05-20");
System.out.println(d1.getTime() > d2.getTime());//true
//两个日期相差的月份
Date d3 = sdf.parse("2017-05-26");
Calendar c = Calendar.getInstance();
c.setTime(d3);
c.add(Calendar.MONTH, 10);//往后推10个月
Date d4 = c.getTime();
System.out.println(sdf.format(d4));//2018-03-26
} catch (Exception e) {
e.printStackTrace();
}
}
}