1 得到相应的 日 月 年
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1); //得到前一天
calendar.add(Calendar.MONTH, -1); //得到前一个月
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1;
int day = aCalendar.get(Calendar.DAY_OF_MONTH )
2 创建指定日期
Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse("2014-2-1 12:12:12");
Calendar aCalendar = Calendar.getInstance();
aCalendar.setTime(date1);
3 得到相应的日期差几天
//得到两个日期间有几天,有多余的时分秒算一天
public static int daysOfDiffer(Date fDate, Date oDate) {
int day = 0 ;
try {
Long date3= oDate.getTime()- fDate.getTime();
day = (int)Math.ceil(date3/(24*3600*1000.0));
} catch (ParseException e) {
e.printStackTrace();
}
return day;
}
4 日期添加指定的天数
public static String getAddd( Integer day ){
Date d = new Date();
long time = d.getTime()+day*24*60*60*1000;
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( new Date( time ));
}
5 比较两个时间大小,
思路:将时间转换成指定时间类型,计算出毫秒数,然后比对大小即可