The Chinese(Lunar) calendar Converted to the Gregorian calendar
Code such as below:
Auhtor : Jimmy Jun 16th 2010
1)中国农历转公历(阳历)

static TransDate ChineseDateToSolarDate(TransDate ChineseDate)
{
/**农历转换成公历*/
System.Globalization.ChineseLunisolarCalendar clc = new System.Globalization.ChineseLunisolarCalendar();
int years,month,day,hour,minutes,second;
TransDate transDate;
;
years = year(ChineseDate);
Month = mthofyr(ChineseDate);
day = dayofmth(ChineseDate);
// hour = datetimeutil::hour(ChineseDate);
// minutes = datetimeutil::minute(ChineseDate);
// second = datetimeutil::second(ChineseDate);
transDate = clc.ToDateTime(years,Month,day,hour,minutes,second,0);
return transDate;//strfmt("农历 : %1;公历:%2",ChineseDate,CalendarStr);
}
2)西方公历(阳历) 转中国农历
The Gregorian calendar Converted to the Chinese(Lunar) calendar.

static TransDate solarDateToChineseDate(TransDate _TransDate)
{
/**公历转换成农历*/
System.Globalization.ChineseLunisolarCalendar cCalendar = new System.Globalization.ChineseLunisolarCalendar();
TransDate GetChineseDateTime(Date datetime)
{
int lyear = cCalendar.GetYear(datetime);
int lmonth = cCalendar.GetMonth(datetime);
int lday = cCalendar.GetDayOfMonth(datetime);
//获取闰月, 0 则表示没有闰月
int leapMonth = cCalendar.GetLeapMonth(lyear);
boolean isleap = false;
if (leapMonth > 0)
{
if (leapMonth == lmonth)
{
//闰月
isleap = true;
lmonth--;
}
else if (lmonth > leapMonth)
{
lmonth--;
}
}
//return strfmt("%1年%2月%3日",lyear,lmonth,lday);
return mkdate(lday,lmonth,lyear);
}
;
return GetChineseDateTime(_TransDate);
}
the Calendar Transfer in Dyamics AX 4.0

static void Jimmy_ChineseLunisolarDateAX4(Args _args)//AX4.0
{
Dialog dlg = new Dialog("Calendar conver to Chinese Lunisolar Date");
DialogField dlgDate = dlg.addField(typeid(TransDate),"Calendar");
DialogField dlgType = dlg.addField(typeid(Noyes),"公历转农历");
TransDate Calendar;
str 100 ChineseLunisolarDate(TransDate CalendarDateTime)
{
int years,month,day,leapMonth,Weekday;
Container J = ['甲',"乙",'丙','丁','戊','己','庚','辛','壬','癸'];
Container z = ["子","丑","寅","卯","巳","午","未","申","酉","戌","亥"];
Container s = ["鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"];
Container w = ["无","正","二","三","四","五","六","七","八","九","十","冬","腊"];
Container c = ["初","十","廿","三"];
Container r = ["日","一","二","三","四","五","六","七","八","九"];
Container wd = ['日',"一","二","三","四","五","六"];
Str 20 jj,zz,ss,ww,cc,rr,rn,ry,wkday;
Str 100 ChineseStr;
System.Globalization.ChineseLunisolarCalendar clc;
boolean isleapMonth,IsLeapYear;
;
clc = new System.Globalization.ChineseLunisolarCalendar();
years = clc.GetYear(CalendarDateTime);
month = clc.GetMonth(CalendarDateTime);
day = clc.GetDayOfMonth(CalendarDateTime);
leapMonth = clc.GetLeapMonth(years);
Weekday = clc.GetDayOfWeek(CalendarDateTime);
IsLeapYear = clc.IsLeapYear(years);
jj = Conpeek(J,(years - 4) MOD 10 + 1);
zz = Conpeek(Z,(years - 4) MOD 12 +1);
SS = Conpeek(S,(years - 4) MOD 12 +1);
//wkday = '星期' + Conpeek(wd,Weekday + 1);
wkday = dayname(Weekday);
if (leapMonth > 0)
{
if (leapMonth == month) //闰月
{
isleapMonth = true;
ry = '闰月';
month = month - 1;
}
else if (month > leapMonth)
{
month = month - 1;
}
}
WW = Conpeek(W,month + 1);
cc = ConPeek(C,day / 10 + 1);
rr = ConPeek(R,day mod 10 + 1);
if(leapMonth)
ry = '润';
else
ry = '平';
if(IsLeapYear)
rn = '润';
else
rn = '平';
ChineseStr = strfmt("%1%2%3(%8)年(%4)%5月 %6%7日" ,JJ,ZZ,ss,ry,WW,cc,rr,rn)+ wkday;
return ChineseStr + strfmt(" %1年 %2月 %3日",years,month,day);
}
str 100 ChineseDateToSolarDate(TransDate ChineseDate)
{
int years,month,day,hour,minutes,second;
TransDate CalendarStr;
System.Globalization.ChineseLunisolarCalendar clc = new System.Globalization.ChineseLunisolarCalendar();
;
years = year(ChineseDate);
Month = mthofyr(ChineseDate);
day = dayofmth(ChineseDate);
// hour = datetimeutil::hour(ChineseDate);
// minutes = datetimeutil::minute(ChineseDate);
// second = datetimeutil::second(ChineseDate);
CalendarStr = clc.ToDateTime(years,Month,day,hour,minutes,second,0);
return strfmt("农历 : %1;公历:%2",ChineseDate,CalendarStr);
}
;
dlgDate.value(today());
dlg.doInit();
if(!dlg.run())
return ;
if(dlgType.value())
info(ChineseLunisolarDate(dlgDate.value()));
else
info(ChineseDateToSolarDate(dlgDate.value()));
}
the Calendar Transfer in Dyamics AX 2009

static void Jimmy_ChineseLunisolarDateAX2009(Args _args)
{
CreatedDateTime Cdt;
str 50 ChineseLunisolarDate(Createddatetime ADataTime)
{
int years,month,day,leapMonth,Weekday;
Container J = ['甲',"乙",'丙','丁','戊','己','庚','辛','壬','癸'];
Container z = ["子","丑","寅","卯","巳","午","未","申","酉","戌","亥"];
Container s = ["鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"];
Container w = ["无","正","二","三","四","五","六","七","八","九","十","冬","腊"];
Container c = ["初","十","廿","三"];
Container r = ["日","一","二","三","四","五","六","七","八","九"];
Container wd = ['日',"一","二","三","四","五","六"];
Str 20 jj,zz,ss,ww,cc,rr,rn,ry,wkday;
boolean isleapMonth,IsLeapYear;
Str 50 ChineseStr;
System.Globalization.ChineseLunisolarCalendar clc = new System.Globalization.ChineseLunisolarCalendar();
;
years = clc.GetYear(ADataTime);
month = clc.GetMonth(ADataTime);
day = clc.GetDayOfMonth(ADataTime);
leapMonth = clc.GetLeapMonth(years);
Weekday = clc.GetDayOfWeek(ADataTime);
IsLeapYear = clc.IsLeapYear(years);
print System.DateTime::IsLeapYear(years),Weekday;
jj = Conpeek(J,(years - 4) MOD 10 + 1);
zz = Conpeek(Z,(years - 4) MOD 12 +1);
SS = Conpeek(S,(years - 4) MOD 12 +1);
wkday = '星期' + Conpeek(wd,Weekday + 1);
if (leapMonth > 0)
{
if (leapMonth == month)
{
//闰月
isleapMonth = true;
ry = '闰月';
month = month - 1;
}
else if (month > leapMonth)
{
month = month - 1;
}
}
WW = Conpeek(W,month + 1);
cc = ConPeek(C,day / 10 + 1);
rr = ConPeek(R,day mod 10 + 1);
if(leapMonth)
ry = '润';
else
ry = '平';
if(IsLeapYear)
rn = '润';
else
rn = '平';
ChineseStr = strfmt("%1%2%3(%8)年(%4)%5月 %6%7日" ,JJ,ZZ,ss,ry,WW,cc,rr,rn)+ wkday;
print strfmt("%1年 %2月 %3日",years,month,day);
return ChineseStr;
}
;
Cdt = dateTimeUtil::newDateTime(mkdate(19,12,1985),0);//My birthday 11初八
Cdt = dateTimeUtil::newDateTime(today(),0);
print Cdt;//dateTimeUtil::utcNow()
Cdt = datetimeUtil::applyTimeZoneOffset(Cdt,datetimeutil::getCompanyTimeZone());
print ChineseLunisolarDate(Cdt);
print dateTimeUtil::getSystemDateTime();
pause;
}
/*
return System.String::Format("{0}{1} {2}年{3}{4}月{5}{6}"
, "甲乙丙丁戊己庚辛壬癸"[(years - 4) % 10]
, "子丑寅卯辰巳午未申酉戌亥"[(years - 4) % 12]
, "鼠牛虎兔龙蛇马羊猴鸡狗猪"[(years - 4) % 12]
, month == leapMonth ? "润" : ""
, "无正二三四五六七八九十冬腊"[leapMonth > 0 && leapMonth <= month ? month - 1 : month]
, "初十廿三"[day / 10]
, "日一二三四五六七八九"[day % 10]
*/