/**
* 计算指定日期的上一天
*
* @param dateTime
* 日期格式为yyyyMMdd
* @return
*/
public static String getBeforeDay(String dateTime) {
Calendar now = CalendargetInstance()
SimpleDateFormat simpledate = new SimpleDateFormat(yyyyMMdd)
Date date = null;
try {
date = simpledateparse(dateTime)
} catch (ParseException ex) {
Systemoutprintln(日期格式不符合要求 + exgetMessage())
return null;
}
nowsetTime(date)
int year = nowget(CalendarYEAR)
int month = nowget(CalendarMONTH)
int day = nowget(CalendarDAY_OF_MONTH) ;
nowset(year month day)
String time = simpledateformat(nowgetTime())
return time;
}
/**
* 计算指定日期的下一天
*
* @param dateTime
* 日期格式为yyyyMMdd
* @return
*/
public static String getNextDay(String dateTime) {
Calendar now = CalendargetInstance()
SimpleDateFormat simpledate = new SimpleDateFormat(yyyyMMdd)
Date date = null;
try {
date = simpledateparse(dateTime)
} catch (ParseException ex) {
Systemoutprintln(日期格式不符合要求 + exgetMessage())
return null;
}
nowsetTime(date)
int year = nowget(CalendarYEAR)
int month = nowget(CalendarMONTH)
int day = nowget(CalendarDAY_OF_MONTH) + ;
nowset(year month day)
String time = simpledateformat(nowgetTime())
return time;
}
/**
* 得到指定月的天数
* @param _year
* @param _month
* @return
*/
public static int getMaxDayOfMonth(int _year int _month){
Calendar now = CalendargetInstance()
int year = ;
int month = ;
if(_month==){
year = _year ;
month = ;
}else{
year = _year;
month = _month ;
}
nowset(CalendarYEAR year)
nowset(CalendarMONTH month)
return nowgetActualMaximum(CalendarDATE)
}
/**
* 计算时间差
*
* @param beginTime
* 开始时间格式yyyyMMdd HH:mm:ss
* @param endTime
* 结束时间格式yyyyMMdd HH:mm:ss
* @return 从开始时间到结束时间之间的时间差(秒)
*/
public static long getTimeDifference(String beginTime String endTime) {
long between = ;
SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMdd HH:mm:ss)
Date end = null;
Date begin = null;
try {// 将截取到的时间字符串转化为时间格式的字符串
end = sdfparse(endTime)
begin = sdfparse(beginTime)
} catch (ParseException e) {
eprintStackTrace()
}
between = (endgetTime() begingetTime()) / ;// 除以是为了转换成秒
return between;
}
/**
* 计算时间差
*
* @param time
* 指定的时间格式为yyyyMMdd HH:mm:ss
* @return 当前时间和指定时间的时间差(秒)
*/
public static long getTimeDifference(String time) {
long between = ;
SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMdd HH:mm:ss)
String systemTime = sdfformat(new Date())toString()
Date end = null;
Date begin = null;
try {// 将截取到的时间字符串转化为时间格式的字符串
end = sdfparse(time)
begin = sdfparse(systemTime)
} catch (ParseException e) {
eprintStackTrace()
}
between = Mathabs(endgetTime() begingetTime()) / ;// 除以是为了转换成秒
return between;
}