由於在仁耀的系統中需要做到陽曆轉陰曆的功能,所以嘗試用.Net所開發的System.Globalizatoin (namespace) -- http://msdn2.microsoft.com/zh-tw/library/system.globalization(VS.80).aspx 來達到此目的.
因為需要用到台灣陽曆轉陰曆的功能,因此我使用了TaiwanLunisolarCalendar 類別 -- http://msdn2.microsoft.com/zh-tw/library/system.globalization.taiwanlunisolarcalendar(VS.80).aspx ,此類別是專門針對台灣的曆法所發展出來的類別,透過其已開發好的method便可將所輸入的陰曆轉換成農曆!!以下為我所寫的C#範例:
using System;using System.Collections.Generic;
using System.Text;
using System.Globalization;
namespace TimeTran
{
class Program
{
static int Main(string[] args)
{
string s = "58 / 12 / 23";
int y,ny,sToken1,sToken2;
string ly,strny,tT,S;
string[] dateFormats = { "yyyy/MM/dd", "yyy/MM/dd", "yy/MM/dd", "y/MM/dd", "yyyy-MM-dd", "yyy-MM-dd", "yy-MM-dd", "y-MM-dd", "yyyy/M/dd","yyy/M/dd","yy/M/dd","y/M/dd", "yyyy-M-dd","yyy-M-dd","yy-M-dd","y-M-dd", "yyyy/MM/d","yyy/MM/d","yy/MM/d","y/MM/d", "yyyy-MM-d","yyy-MM-d","yy-MM-d","y-MM-d", "yyyy/M/d","yyy/M/d","yy/M/d","y/M/d", "yyyy-M-d","yyy-M-d","yy-M-d","y-M-d"};
TaiwanLunisolarCalendar tl = new TaiwanLunisolarCalendar();
S = s.Replace(" ", ""); //將字串內的空白消去
sToken1 = S.IndexOf("/", 0); //抓出字串內第一個/的位置
sToken2 = S.IndexOf("/", iToken1 + 1);
string nYear = S.Substring(0, iToken1).Trim(); //抓出字串中的新曆年
string nMonth = S.Substring(iToken1+1, iToken2-iToken1-1).Trim();
string nDay = S.Substring(iToken2+1).Trim();
ny = int.Parse(iYear) + 1911; //將民國轉成西元
strny = ny.ToString();
tT = strny +"/"+ iMonth +"/"+ iDay;
DateTime dy = DateTime.ParseExact(tT, dateFormats, null, DateTimeStyles.AllowWhiteSpaces); //須先將字串轉成DataTime的型態,以供TaiwanLunisolarCalendar的物件tl使用
Console.WriteLine(DateTime.Now);
Console.WriteLine("國曆:" + tT);
Console.WriteLine("陰曆:" + tl.GetYear(dy) + "/" + tl.GetMonth(dy) + "/" + tl.GetDayOfMonth(dy)); //將欲轉換的日期傳進.Net以寫好的方法內,接著再傳回轉換好的農曆
return 0;
}
}
}
End~
沒有留言:
張貼留言