站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题一个处理日期的类

一个处理日期的类

摘要: 一个处理日期的类

</td> </tr> <tr> <td width="537" height="35" valign="top" class="ArticleTeitle"> import java.util.*;

/**
* 处理日期相关功能的类
*

Copyright: 2004


* @author Filippo Guan
* @version 1.1
*/
public class DateFormat implements Comparable{
//年
protected String year;
//月
protected String month;
//日
protected String day;
//小时
protected String hour;
//分钟
protected String minute;
//秒钟
protected String second;

//------------------------------------------------------------------------------
/**
* 构造一个DateFormat,值为当前系统日期/时间
*/
public DateFormat(){//构造函数
// Date date = new Date();
Calendar date = Calendar.getInstance();//当前系统日期
year = String.valueOf(date.get(Calendar.YEAR));
month = String.valueOf(date.get(Calendar.MONTH) + 1);
day = String.valueOf(date.get(Calendar.DATE));

//工具类StringFunction请从本站下载
year = StringFunction.updateZero(year, 4);//将年字符串长度扩充到4,不足在前面加补0
month = StringFunction.updateZero(month, 2);//将月字符串长度扩充到2,不足前面补0
day = StringFunction.updateZero(day, 2);//将日字符串长度扩充到2,不足前面补0

hour = String.valueOf(date.get(Calendar.HOUR_OF_DAY));
minute = String.valueOf(date.get(Calendar.MINUTE));
second = String.valueOf(date.get(Calendar.SECOND));

hour = StringFunction.updateZero(hour, 2);
minute = StringFunction.updateZero(minute, 2);
second = StringFunction.updateZero(second, 2);
}

/**
* 根据指定日期构造一个DateFormat,如果指定的日期不合法,则抛出一个错误信息
* @param _date 指定的日期
*/
public DateFormat(String _date){//构造函数
this(_date, false);
}

public DateFormat(String _date, boolean _timeFlag){//构造函数

if (_date == null) {
throw new NullPointerException();
}
String inputDate;
if (_timeFlag) {
//转换成YYYYMMDDHHMISS的形式
inputDate = StringFunction.getDateTimeFormat(_date);

if (!checkDateTime(inputDate)) throw new Error("日期不合法");
}
else {
inputDate = StringFunction.getDateFormat(_date);//转换成YYYYMMDD的形式
if (!checkDate(inputDate)) throw new Error("日期不合法");
}


year = inputDate.substring(0, 4);
month = inputDate.substring(4, 6);
day = inputDate.substring(6, 8);
if (_timeFlag) {
hour = inputDate.substring(8, 10);
minute = inputDate.substring(10, 12);
second = inputDate.substring(12, 14);
}
else {
hour = "00";
minute = "00";
second = "00";
}
}
/**
* 根据指定的日期和时间构造一个DateFormat,
* 如果指定的日期和时间不合法,则抛出一个错误信息
* @param _date 指定的日期
* @param _ho 指定的小时
* @param _mi 指定的分钟
* @param _se 指定的秒钟
*/
public DateFormat(String _date, String _ho, String _mi, String _se){//构造函数
this();
if (!checkDate(_date)) throw new NullPointerException("日期不合法");
if (!checkTime(_ho, _mi, _se)) throw new NullPointerException("时间不合法");
year = _date.substring(0, 4);
month = _date.substring(4, 6);
day = _date.substring(6, 8);
hour = _ho;
minute = _mi;
second = _se;
}

public DateFormat(Date d){//函数构造
Calendar date = Calendar.getInstance();
date.setTime(d);
year = String.valueOf(date.get(Calendar.YEAR));
month = String.valueOf(date.get(Calendar.MONTH) + 1);
day = String.valueOf(date.get(Calendar.DATE));

year = StringFunction.updateZero(year, 4);
month = StringFunction.updateZero(month, 2);
day = StringFunction.updateZero(day, 2);

hour = String.valueOf(date.get(Calendar.HOUR_OF_DAY));
minute = String.valueOf(date.get(Calendar.MINUTE));
second = String.valueOf(date.get(Calendar.SECOND));

hour = StringFunction.updateZero(hour, 2);
minute = StringFunction.updateZero(minute, 2);
second = StringFunction.updateZero(second, 2);
}
//------------------------------------------------------------------------------
/**
* 获取当前系统日期/时间
* @return 当前系统日期/时间
*/
public synchronized static DateFormat getCurrentDate(){
return new DateFormat();
}

//------------------------------------------------------------------------------
/**
* 设定日期,
* 如果设定的日期不合法,则抛出一个出错信息
* @param _date 设定的日期值
*/
public void setDate(String _date){
if (!checkDate(_date)) throw new NullPointerException("日期不合法");
year = _date.substring(0, 4);
month = _date.substring(4, 6);
day = _date.substring(6, 8);
}

/**
* 设定日期,
* 如果设定的日期不合法,则抛出一个错误信息
* @param _yyyy 年
* @param _mm 月
* @param _dd 日
*/
public void setDate(int _yyyy, int _mm, int _dd){
String yyyy = StringFunction.updateZero(String.valueOf( _yyyy), 4);
String mm = StringFunction.updateZero(String.valueOf(_mm), 2);
String dd = StringFunction.updateZero(String.valueOf(_dd), 2);
setDate(yyyy + mm + dd);
}

/**
* 设定时间,
* 如果设定的时间不合法,则抛出一个错误信息
* @param _ho 小时
* @param _mi 分钟
* @param _se 秒钟
*/
public void setTime(String _ho, String _mi, String _se){
if (!checkTime(_ho, _mi, _se)) return;
hour = _ho;
minute = _mi;
second = _se;
}

//------------------------------------------------------------------------------
//取出成员变量的值/以规范输出的方式取出成员变量的值
public String getYear(){
return year;
}

public String getMonth(){
return month;
}

public String getDay(){
return day;
}

public String getMonthDate(){
return year + month;
}

public String getDate(){
return year + month + day;
}

public String getHour(){
return hour;
}

public String getMinute(){
return minute;
}

public String getSecond(){
return second;
}

public String getTime(){
return hour + minute + second;
}

public String getPrintMonth(){
return year + "-" + month;
}

public String getPrintDate(){
return year + "-" + month + "-" + day;
}

public String getPrintTime(){
return hour + ":" + minute + ":" + second;
}

public String getPrintDateTime(){
return getPrintDate() + " " + getPrintTime();
}

public String getSQLDate() {
return "TO_DATE('" + getDate() + "','YYYYMMDD')";
}

//------------------------------------------------------------------------------
//
/**
* 检查日期是否合法
* @param _date 需要检查的日期
* @return 如果日期合法则为true,否则为false
*/
public synchronized static boolean checkDate(String _date){
if (_date == null) {
return false;
}
if (_date.length() < 8)
return false;
String yy = _date.substring(0, 4);
String mm = _date.substring(4, 6);
String dd = _date.substring(6, 8);

try {
int iyy = Integer.parseInt(yy);
int imm = Integer.parseInt(mm);
int idd = Integer.parseInt(dd);

if (iyy > 2050 || iyy < 1950){
return false;
}
if (imm < 1 || imm > 12) return false;
int iMaxMonthDay = Integer.parseInt(getMaxMonthDay(yy, mm));
if (idd < 1 || idd > iMaxMonthDay) return false;
return true;
}
catch (Exception e){
System.err.println(e.getMessage());
return false;
}
}

/**
* 检查日期是否合法
* @param _date 需要检查的日期
* @return 如果日期合法则为true,否则为false
*/
public synchronized static boolean checkDateTime(String _date){
if (_date == null) {
return false;
}
if (_date.length() < 14)
return false;
String yy = _date.substring(0, 4);
String mm = _date.substring(4, 6);
String dd = _date.substring(6, 8);
String ho = _date.substring(8, 10);
String mi = _date.substring(10, 12);
String se = _date.substring(12, 14);

try {
int iyy = Integer.parseInt(yy);
int imm = Integer.parseInt(mm);
int idd = Integer.parseInt(dd);

if (iyy > 2050 || iyy < 1950){
return false;
}
if (imm < 1 || imm > 12) return false;
int iMaxMonthDay = Integer.parseInt(getMaxMonthDay(yy, mm));
if (idd < 1 || idd > iMaxMonthDay) return false;
int iho = Integer.parseInt(ho);
int imi = Integer.parseInt(mi);
int ise = Integer.parseInt(se);

if (iho < 0 || iho > 24) return false;
if (imi < 0 || imi > 60) return false;
if (ise < 0 || ise > 60) return false;
return true;
}
catch (Exception e){
System.err.println(e.getMessage());
return false;
}
}

/**
* 检查时间是否合法
* @param _ho 小时
* @param _mi 分钟
* @param _se 秒钟
* @return 如果合法则为true,否则为false
*/
public static synchronized boolean checkTime(String _ho, String _mi, String _se){
try {
int iho = Integer.parseInt(_ho);
int imi = Integer.parseInt(_mi);
int ise = Integer.parseInt(_se);

if (iho < 0 || iho > 24) return false;
if (imi < 0 || imi > 60) return false;
if (ise < 0 || ise > 60) return false;
return true;
}
catch (Exception e){
System.err.println(e.getMessage());
return false;
}
}
//------------------------------------------------------------------------------
//
/**
* 获得某月的最大的天数
* @param yy 年
* @param mm 月
* @return 该月的最大天数
*/
public static synchronized String getMaxMonthDay(String yy,String mm){

Calendar cal = Calendar.getInstance();
try{
int y = Integer.parseInt(yy);
int m = Integer.parseInt(mm);
cal.set(y, m-1, 1);
}
catch(Exception e){
System.err.println( " DataFunction Exception " + e.getMessage());
}
String maxDay = String.valueOf(cal.getActualMaximum(Calendar.DAY_OF_MONTH));
return maxDay;
}

/**
* 获得某月的最大天数
* @param _yyyymm 年月
* @return 该月的最大天数
*/
public static synchronized String getMaxMonthDay(String _yyyymm){
String yy = _yyyymm.substring(0, 4);
String mm = _yyyymm.substring(4, 6);
return getMaxMonthDay(yy, mm);
}
//------------------------------------------------------------------------------
//
/**
* 得到过去的某个月
* @param _mm 向前推算的月数
* @return 该月的日期
*/
public DateFormat getPreviousMonth(int _mm){
if (_mm < 0)
return getNextMonth(-_mm);
int iyy = Integer.parseInt(year);
int imm = Integer.parseInt(month);
while (imm - _mm <= 0) {
_mm -= 12;
iyy--;
}
imm -= _mm;

String yy = String.valueOf(iyy);
String mm = String.valueOf(imm);

yy = StringFunction.updateZero(yy, 4);
mm = StringFunction.updateZero(mm, 2);

DateFormat df = new DateFormat(yy + mm + "01");
return df;
}

/**
* 得到将来的某个月
* @param _mm 向后推算的月数
* @return 该月的日期
*/
public DateFormat getNextMonth(int _mm){
if (_mm < 0)
return getPreviousMonth(-_mm);
int iyy = Integer.parseInt(year);
int imm = Integer.parseInt(month);
while (_mm + imm > 12) {
_mm -= 12;
iyy++;
}
imm += _mm;

String yy = String.valueOf(iyy);
String mm = String.valueOf(imm);

yy = StringFunction.updateZero(yy, 4);
mm = StringFunction.updateZero(mm, 2);

DateFormat df = new DateFormat(yy + mm + "01");
return df;
}

/**
* 得到上个月
* @return 该月的日期
*/
public DateFormat getPreviousMonth(){
return getPreviousMonth(1);
}

/**
* 得到下个月
* @return 该月的日期
*/
public DateFormat getNextMonth(){
return getNextMonth(1);
}
//------------------------------------------------------------------------------
//
/**
* 得到过去的某日
* @param _day 向前推算的天数
* @return 该日的日期
*/
public DateFormat getPreviousDay(int _day){
if (_day < 0)
return getNextDay(-_day);
int iyy = Integer.parseInt(year);
int imm = Integer.parseInt(month);
int idd = Integer.parseInt(day);
DateFormat df = new DateFormat();
df.setDate(iyy, imm, idd);

if (idd - _day > 0){
idd -= _day;
df.setDate(iyy, imm, idd);
}
else {
_day -= idd;
df = df.getPreviousMonth();
df.setDate(df.getMonthDate() + getMaxMonthDay(df.getMonthDate()));
df = df.getPreviousDay(_day);
}
return df;
}

/**
* 得到将来的某日
* @param _day 向后推算的天数
* @return 该日的日期
*/
public DateFormat getNextDay(int _day){
if (_day < 0)
return getPreviousDay(-_day);
int iyy = Integer.parseInt(year);
int imm = Integer.parseInt(month);
int idd = Integer.parseInt(day);
DateFormat df = new DateFormat();
df.setDate(iyy, imm, idd);

if (idd + _day <= Integer.parseInt(df.getMaxMonthDay(df.getMonthDate()))){
idd += _day;
df.setDate(iyy, imm, idd);
}
else {
_day -= Integer.parseInt(df.getMaxMonthDay(df.getMonthDate())) - idd + 1;
df = df.getNextMonth();
df.setDate(df.getMonthDate() + "01");
df = df.getNextDay(_day);
}
return df;
}
//------------------------------------------------------------------------------
//
/**
* 获得某日的过去几个月的日期
* @param _date 指定的日期
* @param _month 向前推算的月数
* @return 结果的日期
*/
public synchronized static DateFormat getPreviousMonth(String _date, int _month){
if (!DateFormat.checkDate(_date)) {
throw new NullPointerException("日期不正确");
}
return new DateFormat(_date).getPreviousMonth(_month);
}

/**
* 获得某日的将来几个月的日期
* @param _date 指定的日期
* @param _month 向后推算的日期
* @return 结果的日期
*/
public synchronized static DateFormat getNextMonth(String _date, int _month){
if (!DateFormat.checkDate(_date)) {
throw new NullPointerException("日期不正确");
}
return new DateFormat(_date).getNextMonth(_month);
}

/**
* 获得某日的过去若干天的日期
* @param _date 指定的日期
* @param _day 向前推算的日期
* @return 结果的日期
*/
public synchronized static DateFormat getPreviousDay(String _date, int _day){
if (!DateFormat.checkDate(_date)) {
throw new NullPointerException("日期不正确");
}
return new DateFormat(_date).getPreviousDay(_day);
}

/**
* 获得某日的将来若干天的日期
* @param _date 指定的日期
* @param _day 向后推算的日期
* @return 结果的日期
*/
public synchronized static DateFormat getNextDay(String _date, int _day){
return new DateFormat(_date).getNextDay(_day);
}
//------------------------------------------------------------------------------
//
/**
* 比较两个日期的前后
* @param _date1 日期1
* @param _date2 日期2
* @return 比较的结果,
* 如果>0,则_date1>_date2;=0,两个日期相等;<0,则_date1<_date2
*/
public synchronized static int compare(String _date1, String _date2){
if (!checkDate(_date1)) {
throw new NullPointerException("日期不正确");
}
if (!checkDate(_date2)) {
throw new NullPointerException("日期不正确");
}
int nDate1 = Integer.parseInt(_date1);
int nDate2 = Integer.parseInt(_date2);
return nDate1 - nDate2;
}
//------------------------------------------------------------------------------
public String toString(){
if (getHour() == null || getMinute() == null || getSecond() == null) {
return getDate();
}
return getDate() + getTime();
}
//------------------------------------------------------------------------------
public boolean equals(Object o){
if (o instanceof DateFormat) {
return toString().equals(o.toString());
}
else {
DateFormat obj = new DateFormat(o.toString(), true);
return equals(obj);
}
}
//------------------------------------------------------------------------------
/**
* 比较大小
* @param o
* @return
*/
public int compareTo(Object o){
if (o instanceof DateFormat) {
return toString().compareTo(o.toString());
}
else {
DateFormat obj = new DateFormat(o.toString(), true);
return compareTo(obj);
}
}

//------------------------------------------------------------------------------
/**
* 返回两个日期的距离天数
* @param startDate 开始日期
* @param endDate 结束日期
* @return
*/
public static long getPeriodDayCount(String startDate, String endDate){
Date d1 = new DateFormat(startDate).toDate();
Date d2 = new DateFormat(endDate).toDate();
long periodTime = d2.getTime() - d1.getTime();
return periodTime/24/60/60/1000;
}
//------------------------------------------------------------------------------
public Date toDate(){
Calendar c = Calendar.getInstance();
int iy = Integer.parseInt(getYear());
c.set(Calendar.YEAR, Integer.parseInt(getYear()));
c.set(Calendar.MONTH, Integer.parseInt(getMonth()) -1);
c.set(Calendar.DATE, Integer.parseInt(getDay()));
c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(getHour()));
c.set(Calendar.MINUTE, Integer.parseInt(getMinute()));
c.set(Calendar.SECOND, Integer.parseInt(getSecond()));

return c.getTime();
}
//------------------------------------------------------------------------------
public static void main(String[] args) {
try {
System.out.println(DateFormat.getPeriodDayCount("2003-06-01","2003-06-07"));
}
catch (Exception e){
e.printStackTrace();
}
}
}


</td> <td width="165" valign="top" class="ArticleTeitle">
</td> </tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">


↑返回目录
前一篇: Java的HTTP文件队列下载
后一篇: 在Java中使用反射分析类结构