import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public static void sampleJDK() {
// 1 创建一个任意时间对象 , 如: 2012-12-14 15:23:55
Calendar calendar = Calendar.getInstance();
calendar.set(2012, Calendar.NOVEMBER, 15, 18, 23, 55);
// 2 计算任意两个时间之间相差的天数
Calendar start = Calendar.getInstance();
start.set(2012, Calendar.NOVEMBER, 20);
Calendar end = Calendar.getInstance();
end.set(2012, Calendar.NOVEMBER, 12);
long startTim = start.getTimeInMillis(); // 毫秒
long endTim = end.getTimeInMillis();// 毫秒
long diff = endTim - startTim;
int diff_days = (int) (diff / 1000 / 60 / 60 / 24);
System.out.println("相差天数:" + diff_days);
// 获取 18 天之后的某天在下个月的当前周的第一 天日期
Calendar current = Calendar.getInstance();
DateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(dataFormat.format(current.getTime()));
current.add(Calendar.DAY_OF_MONTH, 18);
current.add(Calendar.MONTH, 1);
System.out.println(dataFormat.format(current.getTime()));
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
//过去七天
c.setTime(new Date());
c.add(Calendar.DATE, - 7);
Date d = c.getTime();
String day = format.format(d);
System.out.println("过去七天:"+day);
//过去一月
c.setTime(new Date());
c.add(Calendar.MONTH, -1);
Date m = c.getTime();
String mon = format.format(m);
System.out.println("过去一个月:"+mon);
//过去三个月
c.setTime(new Date());
c.add(Calendar.MONTH, -3);
Date m3 = c.getTime();
String mon3 = format.format(m3);
System.out.println("过去三个月:"+mon3);
//过去一年
c.setTime(new Date());
c.add(Calendar.YEAR, -1);
Date y = c.getTime();
String year = format.format(y);
System.out.println("过去一年:"+year);
}