JAVA 日期加减



/**
* 计算前一天 、后一天
*
* @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));//字符转换成时间

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值