📄 calendarutil.java
字号:
public MyDate(int iYear, int iMonth, int iDay){
this.iYear = checkYear(iYear);
this.iMonth = iMonth;
this.iDay = iDay;
}
public MyDate(int iYear, int iMonth){
this.iYear = checkYear(iYear);
this.iMonth = iMonth;
this.iDay = 1;
}
public MyDate(int iYear){
this.iYear = checkYear(iYear);
this.iMonth = 1;
this.iDay = 1;
}
public MyDate(){
this.iYear = 1981;
this.iMonth = 1;
this.iDay = 1;
}
public String toString(){
return ""+ this.iYear +
(this.iMonth > 9 ? "" + this.iMonth : "0" + this.iMonth) +
(this.iDay > 9 ? "" + this.iDay : "0" + this.iDay);
}
public boolean equals(MyDate md){
return ((md.iDay == this.iDay) &&
(md.iMonth == this.iMonth) && (md.iYear == this.iYear));
}
}
// 阳历日期类,继承自定义日期
class SolarDate extends MyDate {
private static int checkMonth(int iMonth){
if(iMonth > 12){
System.out.println("Month out of range, I think you want 12 :)");
return 12;
}else if(iMonth < 1){
System.out.println("Month out of range, I think you want 1 :)");
return 1;
}else
return iMonth;
}
private static int checkDay(int iYear, int iMonth, int iDay){
int iMonthDays = CalendarUtil.iGetSYearMonthDays(iYear, iMonth);
if(iDay > iMonthDays){
System.out.println("Day out of range, I think you want " +
iMonthDays + " :)");
return iMonthDays;
} else if(iDay < 1){
System.out.println("Day out of range, I think you want 1 :)");
return 1;
} else
return iDay;
}
public SolarDate(int iYear, int iMonth, int iDay){
super(iYear);
this.iMonth = checkMonth(iMonth);
this.iDay = checkDay(this.iYear, this.iMonth, iDay);
}
public SolarDate(int iYear, int iMonth){
super(iYear);
this.iMonth = checkMonth(iMonth);
}
public SolarDate(int iYear){
super(iYear);
}
public SolarDate(){
super();
}
public String toString(){
return ""+ this.iYear +
(this.iMonth > 9 ? "-" + this.iMonth : "-0" + this.iMonth) +
(this.iDay > 9 ? "-" + this.iDay : "-0" + this.iDay);
}
public Week toWeek(){
int iOffsetDays = 0;
for(int i = 1901; i < iYear; i++){
if(CalendarUtil.bIsSolarLeapYear(i))
iOffsetDays += 366;
else
iOffsetDays += 365;
}
iOffsetDays += CalendarUtil.iGetSNewYearOffsetDays(iYear, iMonth, iDay);
return new Week((iOffsetDays + 2) % 7);
}
public LunarDate toLunarDate(){
int iYear, iMonth, iDay, iDate;
LunarDate ld;
iDate = Integer.parseInt(CalendarUtil.sCalendarSolarToLundar(this.iYear, this.iMonth, this.iDay));
iYear = iDate / 10000;
iMonth = iDate % 10000 / 100;
iDay = iDate % 100;
ld = new LunarDate(iYear, iMonth, iDay);
return ld;
}
}
// 阴历日期类,继承自定义日期类
class LunarDate extends MyDate {
private String sChineseNum[] = {
"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"
};
private static int checkMonth(int iYear, int iMonth){
if((iMonth > 12) && (iMonth == CalendarUtil.iGetLLeapMonth(iYear) + 12)){
return iMonth;
}else if(iMonth > 12){
System.out.println("Month out of range, I think you want 12 :)");
return 12;
}else if(iMonth < 1){
System.out.println("Month out of range, I think you want 1 :)");
return 1;
}else
return iMonth;
}
private static int checkDay(int iYear, int iMonth, int iDay){
int iMonthDays = CalendarUtil.iGetLMonthDays(iYear, iMonth);
if(iDay > iMonthDays){
System.out.println("Day out of range, I think you want " +
iMonthDays + " :)");
return iMonthDays;
} else if(iDay < 1){
System.out.println("Day out of range, I think you want 1 :)");
return 1;
} else
return iDay;
}
public LunarDate(int iYear, int iMonth, int iDay){
super(iYear);
this.iMonth = checkMonth(this.iYear,iMonth);
this.iDay = checkDay(this.iYear, this.iMonth, iDay);
}
public LunarDate(int iYear, int iMonth){
super(iYear);
this.iMonth = checkMonth(this.iYear, iMonth);
}
public LunarDate(int iYear){
super(iYear);
}
public LunarDate(){
super();
}
public String toString(){
String sCalendar = "农历";
sCalendar += sChineseNum[iYear / 1000] + sChineseNum[iYear % 1000 / 100] +
sChineseNum[iYear % 100 / 10] + sChineseNum[iYear % 10] + "(" + toChineseEra() + ")年";
if(iMonth > 12) {
iMonth -= 12;
sCalendar += "闰";
}
if(iMonth == 12)
sCalendar += "腊月";
else if(iMonth == 11)
sCalendar += "冬月";
else if(iMonth == 1)
sCalendar += "正月";
else
sCalendar += sChineseNum[iMonth] + "月";
if(iDay > 29)
sCalendar += "三十";
else if(iDay > 20)
sCalendar += "二十" + sChineseNum[iDay % 20];
else if(iDay == 20)
sCalendar += "二十";
else if(iDay > 10)
sCalendar += "十" + sChineseNum[iDay % 10];
else
sCalendar += "初" + sChineseNum[iDay];
return sCalendar;
}
public CnWeek toWeek(){
int iOffsetDays = 0;
for(int i = 1901; i < iYear; i++)
iOffsetDays += CalendarUtil.iGetLYearDays(i);
iOffsetDays += CalendarUtil.iGetLNewYearOffsetDays(iYear, iMonth, iDay);
return new CnWeek((iOffsetDays + 2) % 7);
}
public ChineseEra toChineseEra(){
return new ChineseEra(iYear);
}
public SolarDate toSolarDate(){
int iYear, iMonth, iDay, iDate;
SolarDate sd;
iDate = Integer.parseInt(CalendarUtil.sCalendarLundarToSolar(this.iYear, this.iMonth, this.iDay));
iYear = iDate / 10000;
iMonth = iDate % 10000 / 100;
iDay = iDate % 100;
sd = new SolarDate(iYear, iMonth, iDay);
return sd;
}
}
class CnWeek extends Week{
private String sCnWeek[] = {
"日", "一", "二", "三", "四", "五", "六"
};
public CnWeek(){
super();
}
public CnWeek(int iWeek){
super(iWeek);
}
public String toString(){
return "星期" + sCnWeek[this.iWeek];
}
}
class ChineseEra{
int iYear;
String[] sHeavenlyStems = {
"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"
};
String[] sEarthlyBranches = {
"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"
};
public ChineseEra(){
int iYear = 1981;
}
public ChineseEra(int iYear){
if((iYear < 2050) && (iYear > 1901))
this.iYear = iYear;
else
this.iYear = 1981;
}
public String toString(){
int temp;
temp = Math.abs(iYear - 1924);
return sHeavenlyStems[temp % 10] + sEarthlyBranches[temp % 12];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -