日期:2014-05-16  浏览次数:20422 次

用于从数据库取得系统时间
import java.util.*;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
*
* <p>
* <h2>SysDate工具类用于从数据库取得系统时间。</h2>
* </p>
*
* <p>
* SysDate工具类可以将取得的系统时间缓存起来,不用每次都去数据库取得系统时间, 大大减少了数据库访问次数(实际上只访问了一次),提高效率。
* </p>
*
* <p>
* SysDate工具类还支持日期到字符串,或者字符串到日期的格式转换。如果需要支持其他的
* 日期运算(如:截取日期部分,使时间为00:00:00),请将相关方法抽象成通用工具方法, 加入到该工具类中。
* </p>
*
*/
public final class SysDate {

private static SysDate sysDate = new SysDate();

private static String sMaxDate = "2038-12-31";

private static Date maxDate;

// 数据库时间与WEB服务器时间差
private static long diff = 0;

private static int dateLength = 12;

// 日期格式: yyyyMMdd
private static DateFormat dateFormatSimple;

// 日期格式: yyyyMM
private static DateFormat dateFormatYearMonth;

// 日期格式
private static DateFormat dateFormatEn;

// 日期时间格式
private static DateFormat dateTimeFormatEn;

// 日期字符串的格式
private static String formatDate = "yyyy-MM-dd";

// 日期时间字符串的格式
private static String formatDateTime = "yyyy-MM-dd HH:mm:ss";

// 不允许实例化SysDate对象
private SysDate() {
}

/** 取得日期格式字符串,缺省为:"yyyy-MM-dd" */
public static String getFormatDate() {
return formatDate;
}

/** 取得日期时间格式字符串,缺省为:"yyyy-MM-dd HH:mm:ss" */
public static String getFormatDateTime() {
return formatDateTime;
}

// 从WEB服务器取得时间
private static Date getWebDate() {
return Calendar.getInstance().getTime();
}

/** 获取最大日期时间 */
public static Date getMaxDate() {
if (maxDate == null) {
maxDate = SysDate.getDate(sMaxDate);
}
return maxDate;
}

/** 获取系统日期时间 */
public static Date getSysDate() {
Date date = Calendar.getInstance().getTime();
return new Timestamp(date.getTime() - diff);
}

// 将指定日期对象格式化成指定格式的日期字符串
private static String getDate(Date date, DateFormat formator) {
return formator.format(date);
}

// 将指定的日期字符串按照指定的格式解析成日期对象
private static Date getDate(String date, DateFormat formator) {
if (date == null || date.length() <= 0) {
return null;
}
Date d = null;
try {
d = formator.parse(date);
} catch (Exception e) {
try {
if (date.length() <= dateLength) {
d = SysDate.getDateFormat().parse(date);
} else {
d = SysDate.getDateTimeFormat().parse(date);
}
} catch (ParseException e1) {
String format1 = getFormatDate();
String format2 = getFormatDateTime();
}
}
if (d != null) {
return getTimestamp(d);
}
return null;
}

private static DateFormat getFormatYearMonth() {
if (dateFormatYearMonth == null) {
String format = "yyyyMM";
dateFormatYearMonth = new SimpleDateFormat(format);
}
return dateFormatYearMonth;
}

/** 获取当前系统日期,并转换成字符串,格式:yyyyMM */
public static String getDateYearMonth() {
return getDate(getSysDate(), getFormatYearMonth());
}

/** 获取指定日期的字符串,格式:yyyyMM */
public static String getDateYearMonth(Date date) {
return getDate(date, getFormatYearMonth());
}

private static DateFormat getFormatSimple() {
if (dateFormatSimple == null) {
String format = "yyyyMMdd";
dateFormatSimple = new SimpleDateFormat(format);
}
return dateFormatSimple;
}

/** 获取当前系统日期,并转换成字符串,格式:yyyyMMdd */
public static String getDateSimple() {
return g