1、某个时间往推前几天,往后几天
/**
* @param time 时间
*@param day 往前推传入负整数 后退传入正整数
*@param format 格式
* @return
*/
public static String afterDay(Date time, Integer day,String format) {
if (time == null) {
time = new Date();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
//当前往后一天
calendar.add(Calendar.DATE, day);
Date date = calendar.getTime();
DateFormat df = new SimpleDateFormat(format);
return df.format(date);
}
2、比较某个时间和当前时间
//比较两个时间
public static boolean compareDate(String beginDate){
boolean flag = false;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date firstDate = sdf.parse(beginDate);
Date currentDate = new Date();
String endDate = sdf.format(currentDate);
Date secondDate = sdf.parse(endDate);
if(secondDate.getTime()>=firstDate.getTime()){
flag = true;
}
} catch (ParseException e) {
e.printStackTrace();
}
return flag;
}
3、验证日期格式是否正确
/**
* 根据时间 和时间格式 校验是否正确
*
* @param length 校验的长度
* @param sDate 校验的日期
* @param format 校验的格式
* @return
*/
public static boolean isLegalDate(int length, String sDate, String format) {
int legalLen = length;
if ((sDate == null) || (sDate.length() != legalLen)) {
return false;
}
DateFormat formatter = new SimpleDateFormat(format);
try {
Date date = formatter.parse(sDate);
return sDate.equals(formatter.format(date));
} catch (Exception e) {
return false;
}
}
4、获取当前年月日字符串形式
public static String getCurrentStringDate() {
Calendar instance = Calendar.getInstance();
int year = instance.get(Calendar.YEAR);
int month = instance.get(Calendar.MONTH) + 1;
String _month = String.valueOf(month);
if (month < 10) {
_month = "0" + month;
}
int currentDay = DateUtils.getCurrentDayOfMonth();
String _currentDay = String.valueOf(currentDay);
if (currentDay < 10) {
_currentDay = "0" + currentDay;
}
return year + "年" + _month + "月" + _currentDay+ "日";
}
5、计算两个日期之间的天数
/**
* 在一个时间段计算出多少天
*
* @param beginDate 开始时间
* @param endDate 结束时间
* @return 天数
*/
public static Integer daysBetween(String beginDate, String endDate) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(beginDate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(endDate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
} catch (Exception e) {
return 0;
}
}
6、日期转化为指定字符串格式
public static String date2Str(Date date, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String format = sdf.format(date);
return format;
}
7、计算两个时间相差几个小时,以30分钟为计算基数,即0.5,1,1、、、
public static Double calculationTimeDiff(Date startTime,Date endTime){
double finalHour = 0;
long end = endTime.getTime();
long start = startTime.getTime();
long diff = end - start;
long min = diff /60 / 1000;
long hour = diff / 60 /60 / 1000;
if(min%60>=30){
finalHour = hour+0.5;
}else{
finalHour = hour;
}
return finalHour;
}
8、某年某月的天数
public static int getDaysByMonth(String dateStr) {
if (dateStr.isEmpty()) {
return 0;
}
int day = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Date date = sdf.parse(dateStr);
Calendar instance = Calendar.getInstance();
instance.setTime(date);
day = instance.getActualMaximum(Calendar.DAY_OF_MONTH);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
return day;
}