用于从数据库取得系统时间

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 getDate(getSysDate(), getFormatSimple());
}

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

/**
* 取得日期格式,由SysConfig.getFormatDate()配置, 如:yyyy-MM-dd
*/
public static DateFormat getDateFormat() {
if (dateFormatEn == null) {
String format = getFormatDate();
dateFormatEn = new SimpleDateFormat(format);
}
return dateFormatEn;
}

/**
* 取得日期时间格式,由SysConfig.getFormatDateTime()配置, 如:yyyy-MM-dd HH:mm:ss
*/
public static DateFormat getDateTimeFormat() {
if (dateTimeFormatEn == null) {
String format = getFormatDateTime();
dateTimeFormatEn = new SimpleDateFormat(format);
}
return dateTimeFormatEn;
}

/**
* 获取当前系统日期,并转换成字符串,格式由SysConfig.getFormatDate()配置, 如:yyyy-MM-dd
*/
public static String getDate() {
return getDate(getSysDate(), getDateFormat());
}

/**
* 获取当前系统日期时间,并转换成字符串,格式由SysConfig.getFormatDateTime()配置, 如:yyyy-MM-dd
* HH:mm:ss
*/
public static String getDateTime() {
return getDate(getSysDate(), getDateTimeFormat());
}

/**
* 将指定日期对象转换成字符串,格式由SysConfig.getFormatDate()配置, 如:yyyy-MM-dd
*/
public static String getDate(Date date) {
return getDate(date, getDateFormat());
}

/**
* 将指定日期对象转换成字符串,格式由SysConfig.getFormatDateTime()配置, 如:yyyy-MM-dd HH:mm:ss
*/
public static String getDateTime(Date date) {
return getDate(date, getDateTimeFormat());
}

/**
* 将字符串转换成日期对象,格式由SysConfig.getFormatDate()配置, 如:yyyy-MM-dd
*/
public static Date getDate(String date) {
return getDate(date, getDateFormat());
}

/**
* 将字符串转换成日期对象,格式由SysConfig.getFormatDateTime()配置, 如:yyyy-MM-dd HH:mm:ss
*/
public static Date getDateTime(String date) {
return getDate(date, getDateTimeFormat());
}

/** 取得指定日期所在月份的第一天,如果未指定日期,则默认为当前日期 */
public static Date getMonthDateFirst(Date date) {
Date nowDate = date;
if (nowDate == null) {
nowDate = SysDate.getSysDate();
if (nowDate == null) {
return null;
}
}

Calendar calendar = Calendar.getInstance();
calendar.setTime(nowDate);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.DATE, 1);

return calendar.getTime();
}

/** 取得指定日期所在月份的最后一天,如果未指定日期,则默认为当前日期 */
public static Date getMonthDateLast(Date date) {
Date nowDate = date;
if (nowDate == null) {
nowDate = SysDate.getSysDate();
if (nowDate == null) {
return null;
}
}

Calendar calendar = Calendar.getInstance();
calendar.setTime(nowDate);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.HOUR_OF_DAY, 23);

int lastDay = calendar.getActualMaximum(Calendar.DATE);
calendar.set(Calendar.DATE, lastDay);

return calendar.getTime();
}

private static Date getTimestamp(Date date) {
if (date == null) {
return null;
}
if (date instanceof Timestamp) {
return date;
}
return new Timestamp(date.getTime());
}

public static SysDate getInstance() {
return sysDate;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值