时间操作三

/**
* 对指定的日期指定加法,返回yyyy-mm-dd
*
* @param filed -
*            exp:Calendar.YEAR [2007-4-19]
* @param val -
*            值
* @return - add Date exp:add(Calendar.YEAR,-2) result:2005-4-19
*/
public static String add(java.util.Date date, int filed, int val) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar gc = Calendar.getInstance();
gc.setTime(date);
gc.add(filed, val);
gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc
.get(Calendar.DAY_OF_MONTH));
return df.format(gc.getTime());
}

/**
* 对指日期指定加法,返回yyyy-mm-dd hh:mm:ss
*
* @param date
*            指定日期 java.util.Date
* @param filed -
*            exp:Calendar.HOUR [2007-4-19 9:38:10]
* @param val -
*            值
* @return - add Precision Date exp:add(Calendar.HOUR,-2) result:2007-4-19
*         7:38:10
*/
public static String addPrecision(java.util.Date date, int filed, int val) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar gc = Calendar.getInstance();
gc.setTime(date);
gc.add(filed, val);
gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc
.get(Calendar.DAY_OF_MONTH), gc.get(Calendar.HOUR_OF_DAY), gc
.get(Calendar.MINUTE), gc.get(Calendar.SECOND));
return df.format(gc.getTime());
}

你可能感兴趣的:(时间)