package com.qxueyou.scc.base.util;
|
|
import java.sql.Time;
|
import java.sql.Timestamp;
|
import java.text.SimpleDateFormat;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
/**
|
* 提供日期或者是时间格式化处理工具类。
|
*
|
* @author 邓志永
|
* @since JDK1.6
|
* @history 2014-11-28 邓志永 新建
|
*/
|
public final class DateTimeFormatUtils {
|
/** ISO_DATETIME_FORMAT*/
|
public final static String ISO_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
/** ISO_DATE_FORMAT*/
|
public final static String ISO_DATE_FORMAT = "yyyy-MM-dd";
|
/** ISO_SHORT_DATE_FORMAT*/
|
public final static String ISO_SHORT_DATE_FORMAT = "yyyy-MM";
|
/** ISO_TIME_FORMAT*/
|
public final static String ISO_TIME_FORMAT = "HH:mm:ss";
|
|
private DateTimeFormatUtils() {
|
}
|
|
/**
|
* 根据配置的默认日期时间格式来获取指定的对象的字符串信息。
|
*
|
* @param value
|
* 需要被转换的日期时间对象引用。
|
* @return 返回的是格式化后的字符串。
|
*/
|
public static String formatDateTime(Timestamp value) {
|
return formatDateTime(value, ISO_DATETIME_FORMAT);
|
}
|
|
/**
|
* 根据配置的默认日期时间格式来获取指定的对象的字符串信息。
|
*
|
* @param value
|
* 需要被转换的日期时间对象引用。
|
* @param defaultFormat
|
* 按照指定的格式来处理当前的日期时间对象。
|
* @return 返回的是格式化后的字符串。
|
*/
|
public static String formatDateTime(Timestamp value, String defaultFormat) {
|
if (value == null) {
|
return "";
|
}
|
String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_DATETIME_FORMAT
|
: defaultFormat;
|
SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle);
|
return objSimpleDateFormat.format(value);
|
}
|
|
public static String formatDate(Timestamp value) {
|
return formatDateTime(value, ISO_DATE_FORMAT);
|
}
|
|
public static String formatDate(Timestamp value, String defaultFormat) {
|
if (value == null) {
|
return "";
|
}
|
String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_DATE_FORMAT
|
: defaultFormat;
|
SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle);
|
return objSimpleDateFormat.format(value);
|
}
|
|
public static String formatDate(java.sql.Date value) {
|
return formatDate(value, ISO_DATE_FORMAT);
|
}
|
|
public static String formatDate(java.util.Date value) {
|
return formatDate(value, ISO_DATE_FORMAT);
|
}
|
|
public static String formatDate(java.util.Date value, String defaultFormat) {
|
if (value == null) {
|
return "";
|
}
|
String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_DATE_FORMAT
|
: defaultFormat;
|
SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle);
|
return objSimpleDateFormat.format(value);
|
}
|
|
public static String formatDate(java.sql.Date value, String defaultFormat) {
|
if (value == null) {
|
return "";
|
}
|
String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_DATE_FORMAT
|
: defaultFormat;
|
SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle);
|
return objSimpleDateFormat.format(value);
|
}
|
|
public static String formatTime(Time value, String defaultFormat) {
|
if (value == null) {
|
return "";
|
}
|
String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_TIME_FORMAT
|
: defaultFormat;
|
SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle);
|
return objSimpleDateFormat.format(value);
|
}
|
|
public static String formatTime(Time value) {
|
return formatTime(value, ISO_TIME_FORMAT);
|
}
|
|
public static String formatShortDate(Timestamp value) {
|
return formatDateTime(value, ISO_SHORT_DATE_FORMAT);
|
}
|
|
public static String formatShortDate(Timestamp value, String defaultFormat) {
|
if (value == null) {
|
return "";
|
}
|
String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_SHORT_DATE_FORMAT
|
: defaultFormat;
|
SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle);
|
return objSimpleDateFormat.format(value);
|
}
|
|
}
|