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();
}
}
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();
}
}
相关文章推荐
- android 字符串工具类 格式化日期字符串 拼接数组 判断字符串是否为空 转换时间显示 转换文件大小 获取中英文字符个数
- 日期格式化工具类
- 【工具类】关于日期相关类(当前日期加减一、格式化日期)
- 日期格式化工具类
- 基于python的-写入工具类(保存文件和日期格式化)
- js 日期格式化工具类
- Java中日期格式化成字符串和字符串解析成日期对象工具类
- java高仿微博日期显示格式化,日期辅助工具类
- 分享自己写的JS版日期格式化和解析工具类,绝对好用!
- JS版日期格式化和解析工具类,毫秒级
- java日期格式化工具类
- Java 日期格式化工具类
- java日期格式化工具类【包含常用的日期格式化方法】
- Android工具类之时间日期格式化代码大全
- java日期格式化工具类
- java日期格式化工具类
- 自己整理的常用字符串校验和日期格式化工具类
- 日期格式化工具类
- JS版日期格式化和解析工具类
- Java日期工具类,Java时间工具类,Java时间格式化