/**
* 根据起期和期限计算止期
* @param startDate 起期(格式为yyyy-MM-dd)
* @param date 天数或者月数或者年数
* @param dayOrMonth (传"day"或者"month"或者"year")
* @return
* @throws ParseException
*/
public static String calculateDate(String startDate, int date, String dayOrMonth) throws ParseException {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(sf.parse(startDate));
if ("year".equals(dayOrMonth))
calendar.add(Calendar.YEAR, date);
else if ("month".equals(dayOrMonth))
calendar.add(Calendar.MONTH, date);
else
calendar.add(Calendar.DAY_OF_YEAR, date);
return sf.format(calendar.getTime());
}
执行:calculateDate("2017-08-20", 2, "month")
结果:2017-10-20
/**
* 日期比较
* @param time1 日期1(格式为yyyy-MM-dd)
* @param time2 日期2(格式为yyyy-MM-dd)
* @return 日期1在日期2之前 true; 日期1在日期2之前 false
* @throws ParseException
*/
public static boolean compareDate(String time1, String time2) throws ParseException {
boolean flag = false;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = df.parse(time1);
Date date2 = df.parse(time2);
if (date1.before(date2))
flag = true;
return flag;
}
执行:compareDate("2017-01-15", "2017-02-20")
结果:true
方法1:
/**
* 判断当前系统时间是否在当天的6点之后
* @return true-是 false-否
*/
private static boolean compareDate(){
boolean flag = false;
//当前系统时间
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDateStr = df.format(new Date());
String yyyyMMdd = currentDateStr.split(" ")[0];
// String hour = "";//配置成配置项
// if (StringUtils.isEmpty(hour)) {
// hour = "06";
// } else {
// hour = hour.replace(" ", "");
// }
// int hours;
// try {
// hours = Integer.parseInt(hour);
// } catch (Exception e) {
// hours = 6;
// }
// if(hours < 0 || hours >= 24) hour = "06";
// String appointDateStr = yyyyMMdd + " " + hour + ":00:00";
// String HHmmss = "06:00:00"; //配置成配置项
// SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss");
// try {
// df2.parse(HHmmss);
// } catch (ParseException e1) {
// HHmmss = "06:00:00";
// }
String HHmmss = "";
try {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
HHmmss = sdf.format(calendar.getTime());
} catch (Exception e) {
HHmmss = "06:00:00";
}
String appointDateStr = yyyyMMdd + " " + HHmmss;
try {
Date currentDate = df.parse(currentDateStr);
Date appointDate = df.parse(appointDateStr);
if (currentDate.after(appointDate)) {
flag = true;
}
} catch (ParseException e) {
//打印日志:转换日期异常
}
return flag;
}
方法2:(推荐这种方式)
/**
* 判断当前系统时间是否在当天的6点之后
* @return true-是 false-否
*/
private static boolean compareDate(){
boolean flag = false;
//当前系统时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDateStr = sdf.format(new Date());//当前日期
try {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 6);//配置成配置项
calendar.set(Calendar.SECOND, 0);//配置成配置项
calendar.set(Calendar.MINUTE, 0);//配置成配置项
String appointDateStr = sdf.format(calendar.getTime());//指定日期
Date currentDate = sdf.parse(currentDateStr);
Date appointDate = sdf.parse(appointDateStr);
if (currentDate.after(appointDate)) {
flag = true;
}
} catch (ParseException e) {
//打印日志:转换日期异常
} catch (Exception e) {
//打印日志:转换日期异常
}
return flag;
}
/**
* 当前日期与指定日期相差天数
*/
private static int days(Date currentDate, Date appointDate) {
int days = (int) ((currentDate.getTime() - appointDate.getTime()) / (1000*3600*24));
return days;
}
//main方法验证
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String appointDateStr = "2012-06-17 00:00:00";
String currentDateStr = sdf.format(new Date());
currentDateStr = currentDateStr.split(" ")[0] + " 00:00:00";
try {
Date appointDate = sdf.parse(appointDateStr);
Date currentDate = sdf.parse(currentDateStr);
int days = days(currentDate, appointDate);
System.out.println(days);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}