using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace HotelPms.Share.Windows.Util
|
{
|
public class JapaneseDateTime
|
{
|
private int year, month, dayOfMonth;
|
private bool isLeap;
|
private DateTime time;
|
//System.Globalization.ChineseLunisolarCalendar cc;
|
System.Globalization.JapaneseLunisolarCalendar cc;
|
private static string[] RokuyouList = new string[] { "大安", "赤口", "先勝", "友引", "先負", "仏滅" };
|
|
/// <summary>
|
/// 旧暦年
|
/// </summary>
|
public int Year
|
{
|
get { return year; }
|
}
|
|
/// <summary>
|
/// 旧暦月
|
/// </summary>
|
public int Month
|
{
|
get { return month; }
|
}
|
|
/// <summary>
|
/// 新暦時間
|
/// </summary>
|
public DateTime NewTime
|
{
|
get { return time; }
|
}
|
|
/// <summary>
|
/// 旧暦時間
|
/// </summary>
|
public DateTime OldTime
|
{
|
get { return new DateTime(year, month, dayOfMonth); }
|
}
|
|
/// <summary>
|
/// 旧暦日
|
/// </summary>
|
public int DayOfMonth
|
{
|
get { return dayOfMonth; }
|
}
|
|
/// <summary>
|
/// 閏月かどうか
|
/// </summary>
|
public bool IsLeap
|
{
|
get { return isLeap; }
|
}
|
|
/// <summary>
|
/// 指定日付で旧暦を変換する
|
/// </summary>
|
/// <param name="time"></param>
|
public JapaneseDateTime(DateTime time)
|
{
|
//cc = new System.Globalization.ChineseLunisolarCalendar();
|
cc = new System.Globalization.JapaneseLunisolarCalendar();
|
|
if (time > cc.MaxSupportedDateTime || time < cc.MinSupportedDateTime)
|
throw new Exception("Out Of Range :" + cc.MinSupportedDateTime.ToShortDateString() + "~" + cc.MaxSupportedDateTime.ToShortDateString());
|
year = cc.GetYear(time);
|
month = cc.GetMonth(time);
|
dayOfMonth = cc.GetDayOfMonth(time);
|
isLeap = cc.IsLeapMonth(year, month);
|
if (cc.IsLeapYear(year) && month >= cc.GetLeapMonth(year)) { month -= 1; }
|
this.time = time;
|
}
|
|
public static JapaneseDateTime Now
|
{
|
get { return new JapaneseDateTime(DateTime.Now); }
|
}
|
|
public JapaneseDateTime(int Year, int Month, int DayOfMonth, bool IsLeap)
|
{
|
if (Year >= cc.MaxSupportedDateTime.Year || Year <= cc.MinSupportedDateTime.Year)
|
throw new Exception("Out Of Range ::" + cc.MinSupportedDateTime.ToShortDateString() + "~" + cc.MaxSupportedDateTime.ToShortDateString());
|
|
if (Month < 1 || Month > 12)
|
throw new Exception("日付が不正です。");
|
//cc = new System.Globalization.ChineseLunisolarCalendar();
|
cc = new System.Globalization.JapaneseLunisolarCalendar();
|
|
if (cc.GetLeapMonth(Year) != Month && IsLeap)
|
throw new Exception("GetLeapMonth Error");
|
if (cc.GetDaysInMonth(Year, IsLeap ? Month + 1 : Month) < DayOfMonth || DayOfMonth < 1)
|
throw new Exception("Day Error");
|
year = Year;
|
month = Month;
|
dayOfMonth = DayOfMonth;
|
isLeap = IsLeap;
|
time = DateTime.Now;
|
}
|
|
/// <summary>
|
/// 旧暦⇒新暦
|
/// </summary>
|
/// <returns></returns>
|
public DateTime ToDateTime()
|
{
|
return cc.ToDateTime(year, isLeap ? month + 1 : month, dayOfMonth, time.Hour, time.Minute, time.Second, time.Millisecond);
|
}
|
|
/// <summary>
|
/// 旧暦⇒新暦
|
/// </summary>
|
/// <returns></returns>
|
public static DateTime ToDateTime(JapaneseDateTime CnTime)
|
{
|
return CnTime.ToDateTime();
|
}
|
|
/// <summary>
|
/// 新暦⇒旧暦
|
/// </summary>
|
/// <returns></returns>
|
public static JapaneseDateTime ToJapaneseDateTime(DateTime Time)
|
{
|
return new JapaneseDateTime(Time);
|
}
|
|
/// <summary>
|
/// 新暦⇒旧暦
|
/// </summary>
|
/// <returns></returns>
|
public static string ToJapaneseDateString(DateTime Time)
|
{
|
return ToJapaneseDateString(Time, "yyyy/MM/dd");
|
}
|
|
/// <summary>
|
/// 新暦⇒旧暦
|
/// </summary>
|
/// <returns></returns>
|
public static string ToJapaneseDateString(DateTime Time, string format)
|
{
|
try
|
{
|
JapaneseDateTime jpDate = new JapaneseDateTime(Time);
|
return jpDate.OldTime.ToString(format);
|
}
|
catch
|
{
|
return string.Empty;
|
}
|
}
|
|
/// <summary>
|
/// 新暦⇒六曜
|
/// </summary>
|
/// <returns></returns>
|
public static string ToRokuyou(DateTime Time)
|
{
|
try
|
{
|
JapaneseDateTime jpDate = new JapaneseDateTime(Time);
|
int index = (jpDate.Month + jpDate.DayOfMonth) % 6;
|
return RokuyouList[index];
|
}
|
catch
|
{
|
return string.Empty;
|
}
|
}
|
}
|
}
|