using System;
using System.Globalization;
using System.Threading;
namespace JapaneseCalendar
{
/// <summary>
/// Class1 の概要の説明です。
/// </summary>
class Class1
{
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
TestFormat();
//
TestCulture();
//
ForceCulture();
//
ToDateTime();
//
Console.ReadLine();
}
static private void TestFormat()
{
Console.WriteLine("------------------------------------------------");
Console.WriteLine("DateFormatを調べる");
// Date Format
// System.DateTime time1 = System.DateTime.Now;
System.DateTime time1 = System.DateTime.Parse("2002/04/01 13:23:45"); // 西暦のテスト用
// System.DateTime time1 = System.DateTime.Parse("14/04/01 13:23:45"); // 和暦のテスト用
Console.WriteLine("\nShort date pattern.");
Console.WriteLine("d\t = {0:d}", time1);
Console.WriteLine("dd\t = {0:dd}", time1);
Console.WriteLine("ddd\t = {0:ddd}", time1);
Console.WriteLine("dddd\t = {0:dddd}", time1);
Console.WriteLine("\nLong date pattern.");
Console.WriteLine("D\t{0:D}", time1);
Console.WriteLine("\nFull (long date + short time).");
Console.WriteLine("f\t{0:f}", time1);
Console.WriteLine("ff\t{0:ff}", time1);
Console.WriteLine("fff\t{0:fff}", time1);
Console.WriteLine("ffff\t{0:ffff}", time1);
Console.WriteLine("fffff\t{0:fffff}", time1);
Console.WriteLine("ffffff\t{0:ffffff}", time1);
Console.WriteLine("ffffffff\t{0:fffffff}", time1);
Console.WriteLine("\nhour in a 12-hour clock");
Console.WriteLine("hh\t{0:hh}", time1);
Console.WriteLine("\nhour in a 24-hour clock");
Console.WriteLine("HH\t{0:HH}", time1);
Console.WriteLine("mm\t{0:mm}", time1);
Console.WriteLine("hhmm\t{0:hhmm}", time1);
Console.WriteLine("hhmmss\t{0:hhmmss}", time1);
Console.WriteLine("\nFull date/time pattern (long date + long time).");
Console.WriteLine("F\t{0:F}", time1);
Console.WriteLine("\nGeneral (short date + short time).");
Console.WriteLine("g\t{0:g}", time1);
Console.WriteLine("gg\t{0:gg}", time1);
Console.WriteLine("ggggg\t{0:ggggg}", time1);
Console.WriteLine("\nFull date/time pattern (long date + long time).");
Console.WriteLine("\nGeneral (short date + long time).");
Console.WriteLine("G\t{0:G}", time1);
Console.WriteLine("\nFull date/time pattern (long date + long time).");
Console.WriteLine("m\t{0:m}", time1);
Console.WriteLine("\nMonth day pattern.");
Console.WriteLine("M\t{0:M}", time1);
Console.WriteLine("MM\t{0:MM}", time1);
Console.WriteLine("MMM\t{0:MMM}", time1);
Console.WriteLine("MMMM\t{0:MMMM}", time1);
Console.WriteLine("\nRFC1123 pattern.");
Console.WriteLine("r/R\t{0:r}", time1);
Console.WriteLine("\nSortable date/time pattern; conforms to ISO 8601.");
Console.WriteLine("s\t{0:s}", time1);
Console.WriteLine("ss\t{0:ss}", time1);
Console.WriteLine("sss\t{0:sss}", time1);
Console.WriteLine("\nShort time pattern.");
Console.WriteLine("t\t{0:t}", time1);
Console.WriteLine("\nLong time pattern.");
Console.WriteLine("T\t{0:T}", time1);
Console.WriteLine("\nUniversal sortable date pattern");
Console.WriteLine("u\t{0:u}", time1);
Console.WriteLine("\nUniversal sortable date/time pattern");
Console.WriteLine("U\t{0:U}", time1);
Console.WriteLine("\ntimezone offset (hour only). ");
Console.WriteLine("y\t{0:y}", time1);
Console.WriteLine("yy\t{0:yy}", time1);
Console.WriteLine("yyy\t{0:yyy}", time1);
Console.WriteLine("yyyy\t{0:yyyy}", time1);
Console.WriteLine("yyyyy\t{0:yyyyy}", time1);
Console.WriteLine("yyyyyy\t{0:yyyyyy}", time1);
Console.WriteLine("\nYear month pattern.");
Console.WriteLine("z\t{0:zy}", time1);
Console.WriteLine("zz\t{0:zz}", time1);
Console.WriteLine("zzz\t{0:zzz}", time1);
Console.WriteLine("\nYear month pattern.");
Console.WriteLine("Y\t{0:Y}", time1);
Console.WriteLine("\nSeparators.");
Console.WriteLine("/ and :\t{0:yy/MM/dd hh:mm:ss}", time1);
Console.WriteLine("\nQuoted string.");
string str = time1.ToString("yyyy/MM'月の'dd'日よーん' hh:mm:ss ");
Console.WriteLine( "\'\t" + str );
str = time1.ToString("yyyy/MM\"月の\"dd\"日よーん\" hh:mm:ss ");
Console.WriteLine( "\"\t" + str );
}
static private void TestCulture()
{
Console.WriteLine("------------------------------------------------");
Console.WriteLine("カレントスレッドのカレントカルチャー情報を調べる");
CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
Console.WriteLine("CurrentCulture.Name = {0}", currentCulture.Name);
Console.WriteLine("CurrentCulture.DisplayName = {0}", currentCulture.DisplayName);
Console.WriteLine("DateTimeFormat.FullDateTimePattern = {0}",
currentCulture.DateTimeFormat.FullDateTimePattern.ToString());
Console.WriteLine("Calendar = {0}", currentCulture.DateTimeFormat.Calendar.ToString());
// グレゴリー暦の場合のカレンダータイプ
if (currentCulture.DateTimeFormat.Calendar.GetType() == typeof(GregorianCalendar))
Console.WriteLine("CalendarType = {0}",
((GregorianCalendar)(currentCulture.DateTimeFormat.Calendar)).CalendarType.ToString());
}
static private void ForceCulture()
{
Console.WriteLine("------------------------------------------------");
Console.WriteLine("カレンダーの種類を指定して日時を表示する。");
CultureInfo culture = null;
DateTime now = System.DateTime.Now;
// そのまま、DateTime.Now を表示すると、コントロールパネルで設定している
// カレンダーの種類(和暦、西暦(英語)、西暦(日本語))で表示される。
Console.WriteLine("無指定 : " + System.DateTime.Now.ToString("F"));
// カルチャーのクローンを作る。new より早い。
culture = (CultureInfo)(Thread.CurrentThread.CurrentCulture.Clone());
// 西暦(日本語)表示にする。
GregorianCalendar gregLocalCal = new System.Globalization.GregorianCalendar();
gregLocalCal.CalendarType = GregorianCalendarTypes.Localized;
culture.DateTimeFormat.Calendar = gregLocalCal;
Console.WriteLine("西暦(日本語)表示: " + now.ToString("F", culture));
// 西暦(英語)表示にする。
GregorianCalendar gregUSCal = new System.Globalization.GregorianCalendar();
gregUSCal.CalendarType = GregorianCalendarTypes.USEnglish;
culture.DateTimeFormat.Calendar = gregUSCal;
Console.WriteLine("西暦(英語)表示 : " + now.ToString("F", culture));
// 和暦表示にする。
Calendar jpCal = new System.Globalization.JapaneseCalendar();
culture.DateTimeFormat.Calendar = jpCal;
Console.WriteLine("和暦表示 : " + now.ToString("F", culture));
// 台湾暦
TaiwanCalendar twCal = new System.Globalization.TaiwanCalendar();
culture = new System.Globalization.CultureInfo("zh-TW");
culture.DateTimeFormat.Calendar = twCal;
Console.WriteLine(culture.NativeName + " : " + now.ToString("F", culture));
}
static private void ToDateTime()
{
Console.WriteLine("------------------------------------------------");
Console.WriteLine("文字列から DateTime へ");
// 西暦文字列→DateTime
DateTime time1 = System.DateTime.Parse("2002/4/1 13:23:45");
Console.WriteLine("『2002/4/1 13:23:45』→DateTime : " + time1.ToString("F"));
// 和暦文字列→DateTime
Calendar jpCal = new System.Globalization.JapaneseCalendar();
CultureInfo culture = new CultureInfo("ja-JP");
culture.DateTimeFormat.Calendar = jpCal;
DateTime time2 = System.DateTime.Parse("平成 14年4月1日 月曜日 13:23:45");
Console.WriteLine("『平成 14年4月1日 月曜日 13:23:45』→DateTime : " + time2.ToString("F"));
}
}
}
|