JAVA 日期加减
程序员文章站
2022-06-26 07:59:55
...
/**
* 计算前一天 、后一天
*
* @param date 当前输入的日期 格式为 2010-01-21
* @param amount
* @return
*/
public static String getYesterday(String date, int amount) {
String newDate = Constant.EMPTY_STRING;
if (date != null) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date dd;
try {
dd = format.parse(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dd);
calendar.add(Calendar.DAY_OF_MONTH, amount);
newDate = format.format(calendar.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
}
return newDate;
}
/**
* 计算两天之间相差的天数 调用此方法必须保证 时间格式与 timeFormatStr指定格式一致 否则异常
*
* @param beginDate 开始时间
* @param endDate 结束时间
* @param timeFormatStr 输入时间格式
* @return int 时间相差天数
*/
public static int getCountDays(String beginDate, String endDate, String timeFormatStr) {
int days = 0;
if (beginDate != null && endDate != null && timeFormatStr != null) {
SimpleDateFormat dateFormat = new SimpleDateFormat(timeFormatStr);
Calendar beginCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
try {
endCalendar.setTime(dateFormat.parse(endDate));
beginCalendar.setTime(dateFormat.parse(beginDate));
while (beginCalendar.before(endCalendar)) {
days++;
beginCalendar.add(Calendar.DAY_OF_YEAR, 1);
}
} catch (ParseException e) {
// 日期格式异常
logger.error("getCountDays failed the" + beginDate + "or" + endDate + "is not format"
+ timeFormatStr, e);
}
}
return days;
}
/** 取近期一年的数据*/
public static List<String> getYears() {
List<String> result = new ArrayList<String>();
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, Integer.parseInt(DateUtil.getSysDate("yyyy")));
c.set(Calendar.MONTH, Integer.parseInt(DateUtil.getSysDate("MM")));
result.add(DateUtil.getSysDate("yyyyMM"));
for (int i = 0; i < 11; i++) {
c.add(Calendar.MONTH, -1);
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH);
if (m == 0) {
result.add((y - 1) + "12");
} else {
if (m < 10)
result.add(y + "0" + m);
else
result.add(y + (m + ""));
}
}
return result;
}
/** 取上个月 */
public static String getPrevMonth(String yearMonth) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar calendar = Calendar.getInstance();
Date curDate = java.sql.Date.valueOf(yearMonth + "-10");
calendar.setTime(curDate);
// 取得现在时间
// System.out.println(sdf.format(curDate));
// 取得上一个时间
calendar.set(Calendar.MONDAY, calendar.get(Calendar.MONDAY) - 1);
// 取得上一个月的下一天
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
return sdf.format(calendar.getTime());
}
/** 取当月下的最后第一天 */
public static void getLastDay(String yearMonth) {
Calendar cal = Calendar.getInstance();
cal.setTime(java.sql.Date.valueOf(yearMonth + "-01"));
cal.roll(Calendar.DAY_OF_MONTH, -1);
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));
}
/** 取当月下的第一天 */
public static void getFirstDay(String yearMonth) {
Calendar cal = Calendar.getInstance();
cal.setTime(java.sql.Date.valueOf(yearMonth + "-01"));
cal.set(Calendar.DAY_OF_MONTH, 1);
System.out.println(new SimpleDateFormat(DEFAULT_DATE_FORMAT).format(cal.getTime()));
}
/**
* 日期按小时加减
*
*/
public static void dateByHour(int hourNumber) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time="2016-08-02 20:00:00";
Date date=sdf.parse(time);
long nh = 1000*60*60*hourNumber;
//long nh = 1000*60*60*8;//8小时的毫秒数
long te=date.getTime()+nh;
sdf.format(te); //时间转换成字符
sdf.parse(sdf.format(te));//字符转换成时间
}
下一篇: Edraw Max亿图图示怎么画冰淇淋?