您的位置:首页 > 其它

CalendarUtils 日期格式化工具类

2016-11-11 00:00 387 查看
摘要: 对字符串日期格式化,Date格式化为字符串,日期增加减少 年/月/日等功能点,均能满足,可自定义格式字符串形式

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @ClassName: CalendarUtils
* @Description: 日历工具类:开销量较大,但是一般情况下并不会对系统造成瓶颈限制
* @date Oct 28, 2016 5:16:24 PM
*/
public class CalendarUtils {

public static final String YYYY_MM = "yyyy-MM";

public static final String YYYY_MM_DD = "yyyy-MM-dd";

public static final String YYYY_MM_DD_Hms = "yyyy-MM-dd HH:mm:ss";

public static final String YYYY_MM_DD_Hmss = "yyyy-MM-dd HH:mm:sss";

public static final String YYYYMM = "yyyyMM";

public static final String YYYYMMDD = "yyyyMMdd";

public static final String YYYYMMDDHHmmss = "yyyyMMddHHmmss";

public static final String YYYYMMDDHHmmsss = "yyyyMMddHHmmsss";

private static final Logger logger = LoggerFactory.getLogger(CalendarUtils.class);

static Calendar calendar = Calendar.getInstance();

/**
* @Title: doFormatString
* @Description: 将时间格式化为指定格式的字符串
* @date Oct 28, 2016 4:52:46 PM
* @param date
* @param type
* @return
* @throws ParseException
*/
public static String doFormatString(Date date, String type) {
String result = null;
try {
if (StringUtils.isNotBlank(type)) {
SimpleDateFormat format = new SimpleDateFormat(type);
result = format.format(date);
} else {
// 默认格式化为yyyy-MM-dd HH:mm:ss
SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_Hms);
result = format.format(date);
}
} catch (Exception e) {
logger.error("日期格式化异常.");
e.printStackTrace();
}
return result;
}

/**
* @Title: doFormatDate
* @Description: 将指定格式的字符串格式化为时间
* @date Oct 28, 2016 5:08:01 PM
* @param date
* @param type
* @return
* @throws ParseException
*/
public static Date doFormatDate(String date, String type) {
Date result = null;
if (StringUtils.isEmpty(type)) {
// 默认格式化为yyyy-MM-dd HH:mm:ss
type = YYYY_MM_DD_Hms;
}
try {
SimpleDateFormat format = new SimpleDateFormat(type);
result = format.parse(date);
} catch (ParseException e) {
logger.error("日期格式化异常.");
e.printStackTrace();
}
return result;
}

/**
* @Title: doAddDay
* @Description: 操作日期的增减
* @date Oct 28, 2016 5:16:56 PM
* @param date
* @param n
* @return
*/
public static Date doAddDay(Date date, Integer n) {
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, n);
return calendar.getTime();
}

/**
* @Title: doAddMonth
* @Description: 操作月份的增减
* @date Oct 28, 2016 5:17:29 PM
* @param date
* @param n
* @return
*/
public static Date doAddMonth(Date date, Integer n) {
calendar.setTime(date);
calendar.add(Calendar.MONTH, n);
return calendar.getTime();
}

/**
* @Title: doAddYear
* @Description: 操作年份的增减
* @date Oct 28, 2016 5:17:32 PM
* @param date
* @param n
* @return
*/
public static Date doAddYear(Date date, Integer n) {
calendar.setTime(date);
calendar.add(Calendar.YEAR, n);
return calendar.getTime();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Calendar Date Utils