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);
|
}
|
|
}
|