?? rad_date.java
字號:
import java.util.Calendar;
/*********************
* C All Right Reserved By RS U.S
*
* @author Joe Radlorama
* @since 2007.9.27
* @serial TVer-0.0.1 (TestVersion:0.0.1)
*
*
* @version TVer-0.0.1
* @category Date Caculator
*
* <h1>
* 這個(gè)類型是一個(gè)工具類型,它僅僅用在時(shí)間計(jì)算差值天數(shù)的場合,通過它你也可以獲得年、月、日
* 的數(shù)值也可以獲得由它們組成的字符串,你僅僅只需簡單設(shè)定要計(jì)算
* 的開始和結(jié)束時(shí)間點(diǎn),就可以計(jì)算出這兩個(gè)時(shí)間點(diǎn)之間的天數(shù)(永遠(yuǎn)是正的整數(shù)),當(dāng)然你也
* 可以指設(shè)定其中一個(gè)時(shí)間,而另一個(gè)則由該類型自動提供當(dāng)前系統(tǒng)的時(shí)間作為基準(zhǔn)來計(jì)算。
* </h1>
* */
class Rad_Date
{
/*
* Singleton pattern(Lazy Bone): "Rad_Date" reference can only
* show in the application,It would be initaled in the construct.
*
* 懶漢單例模式,Rad_Date 類型引用指向一個(gè) Rad_Date 對象,這個(gè)對象是在程序
* 生命周期中出現(xiàn)的唯一一個(gè)對象,它在構(gòu)造方法當(dāng)中初始化一次。
*/
private static Rad_Date r_date = null;
/*
* toDate is an instanse of type "Calendar",it be used to record the finish date you want;
* start is an instanse of type "Calendar",it be used to record the start date you want;
*/
private static Calendar toDate = null;
private static Calendar start = null;
/*
* now is an instanse of type "Calendar",it be used to record the date now,It make you can
* access the date field you want,but now it can only access Year\Month\Date;
*/
private static Calendar now = Calendar.getInstance();
//priuvate Construct
private Rad_Date()
{
toDate = Calendar.getInstance();
start = Calendar.getInstance();
}
/**
* @param NULL
* @author Joe Radlorama
* @
* 作用:
* 用來獲得一個(gè)Rad_Date的實(shí)例,這是一個(gè)單例,通過該實(shí)例我們可以計(jì)算兩個(gè)日期之間的天數(shù),
* 以及獲得年、月、日,以及由年、月、日組成的字符串
*/
public static Rad_Date newInstance()
{
//if r_date is null new an instance then return
//otherwise return this object
if(r_date == null) r_date = new Rad_Date();
return r_date;
}
/**
* @param Calendar
* @author Joe Radlorama
* @
* 作用:
* 用來設(shè)置Rad_Date單例對象的起始時(shí)間,它將作為對象計(jì)算時(shí)間被減的起始點(diǎn),
* 不設(shè)置則以當(dāng)前日期為準(zhǔn).
* */
public void setFromDate(Calendar calendar)
{
//set start time;
start = calendar;
start.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);//Calender Month Field Less Than Actual Month One Month
}
/**
* @param Calendar
* @author Joe Radlorama
* @
* 作用:
* 用來設(shè)置Rad_Date單例對象的終止時(shí)間,它將作為對象計(jì)算時(shí)間被減的終結(jié)點(diǎn),
* 不設(shè)置則以當(dāng)前日期為準(zhǔn).
* */
public void setToDate(Calendar calendar)
{
//set finish time
toDate = calendar;
toDate.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);
}
/**
* @param NULL
* @author Joe Radlorama
* @return 當(dāng)前時(shí)間的 年
*/
public int getYear()
{ //return Current Year
return now.get(Calendar.YEAR);
}
/**
* @param NULL
* @author Joe Radlorama
* @return 當(dāng)前時(shí)間的 月
*/
public int getMonth()
{
//return Current Month
return now.get(Calendar.MONTH)+1;
}
/**
* @param NULL
* @author Joe Radlorama
* @return 當(dāng)前時(shí)間的 日
*/
public int getDate()
{
//return Current Date
return now.get(Calendar.DATE);
}
/**
* @param NULL
* @author Joe Radlorama
* @return 當(dāng)前時(shí)間的 "年-月-日" 字符串
*/
public String toString()
{
//return Time String Contain Current "Year-Month-Date"
return ""+getYear()+"-"+getMonth()+"-"+getDate();
}
/**
* @param NULL
* @author Joe Radlorama
* @return 你指定的起始時(shí)間(年-月-日)到你指定的結(jié)束時(shí)間(年-月-日,默認(rèn)是當(dāng)前時(shí)間)的間
* 隔天數(shù)。
* 注意:每次都需要你初始化這兩個(gè)時(shí)間,否則默認(rèn)值計(jì)算當(dāng)前時(shí)間和當(dāng)前時(shí)間的差值
*/
public int getDaysBetweenTimePoint()
{
int Days = 0;
////////////////////////////////////////
//設(shè)定起始時(shí)間的年數(shù)和終止時(shí)間的年數(shù)
///////////////////////////////////////
int yeartoDate = toDate.get(Calendar.YEAR);
int yearTag = start.get(Calendar.YEAR);
//////////////////////////////////////////////////////////////
//判斷起始時(shí)間的年數(shù)和終止時(shí)間的年數(shù)之間的年是否是閏年,并作相應(yīng)天數(shù)的累加
//////////////////////////////////////////////////////////////
for(int y = yeartoDate+1;y<yearTag;y++)
{
if(y%400==0||(y%4!=0&&y%100==0))
{
Days+=366; //加上閏年的天數(shù)
}
else
{
Days+=365; //加上平年的天數(shù)
}
}
int TmpYear;//用來存放結(jié)束時(shí)間所在的年的天數(shù)
if(yearTag%400==0||(yearTag%4!=0&&yearTag%100==0))
{
TmpYear=366; //結(jié)束時(shí)間如果是閏年則 為 366 天
}
else
{
TmpYear=365;//結(jié)束時(shí)間如果是平年則 為 365 天
}
int total = 0;//存放總共的間隔天數(shù)
if(yeartoDate == yearTag)//起始和終止都在同一年。
{
total = Math.abs(toDate.get(Calendar.DAY_OF_YEAR)-start.get(Calendar.DAY_OF_YEAR));
}
else//起始和終止不在同一年。
{
//計(jì)算結(jié)束日期在當(dāng)前年的第幾天。
int toDateDate = toDate.get(Calendar.DAY_OF_YEAR);
//計(jì)算起始日期到結(jié)束日期所在年的年尾有幾天。
int tagDate =Math.abs(TmpYear-start.get(Calendar.DAY_OF_YEAR));
total = (Days+toDateDate+tagDate);//計(jì)算總天數(shù)
}
toDate = Calendar.getInstance();//歸位
start = Calendar.getInstance();//歸位
return total;//返回計(jì)算結(jié)果。
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -