关于CalendarUtil项目中工具类的使用
这是我在自己项目中所编写的时间工具类。如有雷同,纯属巧合
功能 获取当前日期的上三个月日期
这其中有很多关于时间的长短使用的工具类,我会展示出最具代表性的代码,可以举一反三获取当前时间前后任意时间。仅供参考
功能:获取当前日期的上三个月日期
public static String getMonth(Date date) throws ParseException{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date); // 设置为当前时间
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 3); // 设置为上三个月
date = calendar.getTime();
return dateFormat.format(date);
}
功能:获取当前日期的前一天日期
public static String getDay(Date date) throws ParseException{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date); // 设置为当前时间
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 1); // 获取当前日期的前一天日期
date = calendar.getTime();
return dateFormat.format(date);
}
功能:获取当前日期的一年后日期
public static String getYear(Date date) throws ParseException{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date); // 设置为当前时间
calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 1); // 获取当前日期的前一天日期
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 1); // 获取当前日期的前一天日期
date = calendar.getTime();
return dateFormat.format(date);
}
备注
如果有哪位使用请标明出处!