📄 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>
* 这个类型是一个工具类型,它仅仅用在时间计算差值天数的场合,通过它你也可以获得年、月、日
* 的数值也可以获得由它们组成的字符串,你仅仅只需简单设定要计算
* 的开始和结束时间点,就可以计算出这两个时间点之间的天数(永远是正的整数),当然你也
* 可以指设定其中一个时间,而另一个则由该类型自动提供当前系统的时间作为基准来计算。
* </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 类型引用指向一个 Rad_Date 对象,这个对象是在程序
* 生命周期中出现的唯一一个对象,它在构造方法当中初始化一次。
*/
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
* @
* 作用:
* 用来获得一个Rad_Date的实例,这是一个单例,通过该实例我们可以计算两个日期之间的天数,
* 以及获得年、月、日,以及由年、月、日组成的字符串
*/
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
* @
* 作用:
* 用来设置Rad_Date单例对象的起始时间,它将作为对象计算时间被减的起始点,
* 不设置则以当前日期为准.
* */
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
* @
* 作用:
* 用来设置Rad_Date单例对象的终止时间,它将作为对象计算时间被减的终结点,
* 不设置则以当前日期为准.
* */
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 当前时间的 年
*/
public int getYear()
{ //return Current Year
return now.get(Calendar.YEAR);
}
/**
* @param NULL
* @author Joe Radlorama
* @return 当前时间的 月
*/
public int getMonth()
{
//return Current Month
return now.get(Calendar.MONTH)+1;
}
/**
* @param NULL
* @author Joe Radlorama
* @return 当前时间的 日
*/
public int getDate()
{
//return Current Date
return now.get(Calendar.DATE);
}
/**
* @param NULL
* @author Joe Radlorama
* @return 当前时间的 "年-月-日" 字符串
*/
public String toString()
{
//return Time String Contain Current "Year-Month-Date"
return ""+getYear()+"-"+getMonth()+"-"+getDate();
}
/**
* @param NULL
* @author Joe Radlorama
* @return 你指定的起始时间(年-月-日)到你指定的结束时间(年-月-日,默认是当前时间)的间
* 隔天数。
* 注意:每次都需要你初始化这两个时间,否则默认值计算当前时间和当前时间的差值
*/
public int getDaysBetweenTimePoint()
{
int Days = 0;
////////////////////////////////////////
//设定起始时间的年数和终止时间的年数
///////////////////////////////////////
int yeartoDate = toDate.get(Calendar.YEAR);
int yearTag = start.get(Calendar.YEAR);
//////////////////////////////////////////////////////////////
//判断起始时间的年数和终止时间的年数之间的年是否是闰年,并作相应天数的累加
//////////////////////////////////////////////////////////////
for(int y = yeartoDate+1;y<yearTag;y++)
{
if(y%400==0||(y%4!=0&&y%100==0))
{
Days+=366; //加上闰年的天数
}
else
{
Days+=365; //加上平年的天数
}
}
int TmpYear;//用来存放结束时间所在的年的天数
if(yearTag%400==0||(yearTag%4!=0&&yearTag%100==0))
{
TmpYear=366; //结束时间如果是闰年则 为 366 天
}
else
{
TmpYear=365;//结束时间如果是平年则 为 365 天
}
int total = 0;//存放总共的间隔天数
if(yeartoDate == yearTag)//起始和终止都在同一年。
{
total = Math.abs(toDate.get(Calendar.DAY_OF_YEAR)-start.get(Calendar.DAY_OF_YEAR));
}
else//起始和终止不在同一年。
{
//计算结束日期在当前年的第几天。
int toDateDate = toDate.get(Calendar.DAY_OF_YEAR);
//计算起始日期到结束日期所在年的年尾有几天。
int tagDate =Math.abs(TmpYear-start.get(Calendar.DAY_OF_YEAR));
total = (Days+toDateDate+tagDate);//计算总天数
}
toDate = Calendar.getInstance();//归位
start = Calendar.getInstance();//归位
return total;//返回计算结果。
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -