2008年3月4日 星期二

C# 輸入西洋日期,取出今年,中國年

///
/// 輸入西洋日期,取出今年,中國年
///

/// 西洋日期
/// 取出今年,中國年;例如:2004→093
public String getChinaYear(string WestDate)
{
try
{
string dateStr="";
string yyy="00";

int year=int.Parse(WestDate.Substring(0,4))-1911;
yyy= yyy + year.ToString();

dateStr = yyy.Substring(2);

return dateStr;
}
catch(Exception E)
{
return null;
}
}

C# 取得今天中國日期

///
/// 取得今天中國日期
///

/// 今天中國日期
public String getChinaToday()
{
try
{
DateTime dt = DateTime.Now;
int year = dt.Year - 1911;

return year.ToString() + dt.ToString("/MM/dd", DateTimeFormatInfo.InvariantInfo);
}
catch(Exception E)
{
return null;
}
}

C# 民國年轉西元年 (971202 > 1981-12-02)

public static string getParseDate(string dateStr)
{
try
{
if (dateStr == null dateStr.Equals(""))
return "";

return (int.Parse(dateStr.Substring(0, dateStr.Length -4)) + 1911)+"-"+dateStr.Substring(dateStr.Length - 4, 2)+"-"+dateStr.Substring(dateStr.Length - 2, 2);
}
catch
{
return dateStr;
}
}

日期轉換

GetTheHoursOfDay(): 某日期的 24 小時時刻列表
GetTheFirstDayOfWeek(): 某日期在該星期的第一天 (星期日)
GetTheLastDayOfWeek(): 某日期在該星期的最後一天 (星期六)
GetTheFirstDayOfMonth(): 某日期在該月份的第一天
GetTheLastDayOfMonth(): 取得某日期在該月份的最後一天
GetTheFirstDaysOfWeekInMoth(): 某日期在該月份每周的第一天列表
GetTheFirstDayOfQuarter(): 某日期在該季的第一天
GetTheLastDayOfQuarter(): 某日期在該季的最後一天
GetTheFirstDaysOfMonthInQuarter(): 取得某日期在該季每個月的第一天列表
GetTheFirstDayOfYear(): 某日期在當年的第一天
GetTheLastDayOfYear(): 某日期在當年的最後一天
GetTheFirstDaysOfQuarterInYear(): 某日期於當年每一季的第一天列表


///
/// 取得某日期的 24 小時時刻列表
///

/// 某日期
/// 某日期的 24 小時時刻列表
public static DateTime[] GetTheHoursOfDay(DateTime dt)
{
List dtList = new List();

for (int i = 0; i < 24; i++)
{
dtList.Add(new DateTime(dt.Year, dt.Month, dt.Day, i, 0, 0));
}

return dtList.ToArray();
}

///
/// 取得某日期在該星期的第一天 (星期日)
///

/// 某日期
/// 某日期在該星期的第一天 (星期日)
public static DateTime GetTheFirstDayOfWeek(DateTime dt)
{
return dt.AddDays((int) dt.DayOfWeek*-1).Date;
}

///
/// 取得某日期在該星期的最後一天 (星期六)
///

/// 某日期
/// 某日期在該星期的最後一天 (星期六)
public static DateTime GetTheLastDayOfWeek(DateTime dt)
{
return dt.AddDays(7 + (int) dt.DayOfWeek*-1 - 1).Date;
}

///
/// 取得某日期在該月份的第一天 (1 號)
///

/// 某日期
/// 某日期在該月份的第一天
public static DateTime GetTheFirstDayOfMonth(DateTime dt)
{
return new DateTime(dt.Year, dt.Month, 1);
}

///
/// 取得某日期在該月份的最後一天
///

/// 某日期
/// 某日期在該月份的最後一天
public static DateTime GetTheLastDayOfMonth(DateTime dt)
{
return new DateTime(dt.Year, dt.Month + 1, 1).AddDays(-1);
}

///
/// 取得某日期在該月份每周的第一天列表
///

/// 某日期
/// 某日期在該月份每周的第一天列表
public static DateTime[] GetTheFirstDaysOfWeekInMoth(DateTime dt)
{
List dtList = new List();

DateTime dtTemp = GetTheFirstDayOfWeek(GetTheFirstDayOfMonth(dt)).Date;
DateTime dtEnd = GetTheLastDayOfMonth(dt).Date;

for (int i = 0; i < 6; i++)
{
if (dtTemp.AddDays(i*7) <= dtEnd)
{
dtList.Add(dtTemp.AddDays(i*7));
}
}

return dtList.ToArray();
}

///
/// 取得某日期在該季的第一天
///

/// 某日期
/// 某日期在該季的第一天
public static DateTime GetTheFirstDayOfQuarter(DateTime dt)
{
if (dt >= new DateTime(dt.Year, 1, 1) && dt <= new DateTime(dt.Year, 3, DateTime.DaysInMonth(dt.Year, dt.Month), 23, 59, 59))
{
return new DateTime(dt.Year, 1, 1);
}
else if (dt >= new DateTime(dt.Year, 4, 1) && dt <= new DateTime(dt.Year, 6, DateTime.DaysInMonth(dt.Year, dt.Month), 23, 59, 59))
{
return new DateTime(dt.Year, 4, 1);
}
else if (dt >= new DateTime(dt.Year, 7, 1) && dt <= new DateTime(dt.Year, 9, DateTime.DaysInMonth(dt.Year, dt.Month), 23, 59, 59))
{
return new DateTime(dt.Year, 7, 1);
}
else
{
return new DateTime(dt.Year, 10, 1);
}
}

///
/// 取得某日期在該季的最後一天
///

/// 某日期
/// 某日期在該季的最後一天
public static DateTime GetTheLastDayOfQuarter(DateTime dt)
{
if (dt >= new DateTime(dt.Year, 1, 1) && dt <= new DateTime(dt.Year, 3, DateTime.DaysInMonth(dt.Year, dt.Month), 23, 59, 59))
{
return new DateTime(dt.Year, 3, DateTime.DaysInMonth(dt.Year, dt.Month));
}
else if (dt >= new DateTime(dt.Year, 4, 1) && dt <= new DateTime(dt.Year, 6, DateTime.DaysInMonth(dt.Year, dt.Month), 23, 59, 59))
{
return new DateTime(dt.Year, 6, DateTime.DaysInMonth(dt.Year, dt.Month));
}
else if (dt >= new DateTime(dt.Year, 7, 1) && dt <= new DateTime(dt.Year, 9, DateTime.DaysInMonth(dt.Year, dt.Month), 23, 59, 59))
{
return new DateTime(dt.Year, 9, DateTime.DaysInMonth(dt.Year, dt.Month));
}
else
{
return new DateTime(dt.Year, 12, DateTime.DaysInMonth(dt.Year, dt.Month));
}
}

///
/// 取得某日期在該季每個月的第一天列表
///

/// 某日期
/// 取得某日期在該季每個月的第一天列表
public static DateTime[] GetTheFirstDaysOfMonthInQuarter(DateTime dt)
{
List dtList = new List();

DateTime dtTemp = GetTheFirstDayOfQuarter(dt);
DateTime dtEnd = GetTheLastDayOfQuarter(dt);

for (int i = 0; i < 3; i++)
{
if (new DateTime(dt.Year, dtTemp.AddMonths(i).Month, 1) <= dtEnd)
{
dtList.Add(new DateTime(dt.Year, dtTemp.AddMonths(i).Month, 1));
}
}

return dtList.ToArray();
}

///
/// 取得某日期在當年的第一天
///

/// 某日期
/// 某日期在當年的第一天
public static DateTime GetTheFirstDayOfYear(DateTime dt)
{
return new DateTime(dt.Year, 1, 1);
}

///
/// 取得某日期在當年的最後一天
///

/// 某日期
/// 某日期在當年的最後一天
public static DateTime GetTheLastDayOfYear(DateTime dt)
{
return new DateTime(dt.Year, 12, DateTime.DaysInMonth(dt.Year, 12));
}

///
/// 取得某日期於當年每一季的第一天列表
///

/// 某日期
/// 某日期於當年每一季的第一天列表
public static DateTime[] GetTheFirstDaysOfQuarterInYear(DateTime dt)
{
List dtList = new List();
dtList.Add(new DateTime(dt.Year, 1, 1));
dtList.Add(new DateTime(dt.Year, 4, 1));
dtList.Add(new DateTime(dt.Year, 7, 1));
dtList.Add(new DateTime(dt.Year, 10, 1));

return dtList.ToArray();
}

C# 判斷是否為數字

public static long checkint(string str)
{
try
{
int li_str=Convert.ToInt16(str);
}
catch
{
return -1;
}
return 1;
}

Regular expression

如果您查詢J2SE 1.4之後String的線上API手冊說明,您會發現有matches()、replaceAll()等方法,您所傳入的參數是「正則表示式」(Regular expression)的字串,正則表示式的功能是J2SE 1.4之後加入的新功能。

正則表示式最早是由數學家Stephen Kleene于1956年提出,主要使用在字元字串的格式比對,後來在資訊領域廣為應用,現在已經成為ISO(國際標準組織)的標準之一。

Java在J2SE 1.4之後開始支援正則表示式,您可以在API文件的 java.util.regex.Pattern 類別中找到支援的正則表示式相關資訊。

如果您使用String類別來配置字串物件,您可以使用簡易的方法來使用正則表示式,並應用於字串的比對或取代等動作上,以下先介紹幾個簡單的正則表示式。

例如一些常用的範圍,我們可以使用預先定義的字元類別:
. 符合任一字元
\d 等於 [0-9] 數字
\D 等於 [^0-9] 非數字
\s 等於 [ \t\n\x0B\f\r] 空白字元
\S 等於 [^ \t\n\x0B\f\r] 非空白字元
\w 等於 [a-zA-Z_0-9] 數字或是英文字
\W 等於 [^a-zA-Z_0-9] 非數字與英文字


. 符合任一字元。例如有一字串abcdebcadxbc,使用.bc來比對的話,符合的子字串有abc、ebc、xbc三個;如果使用..cd,則符合的子字串只有bcd。

以上的例子來根據字元比對,您也可以使用「字元類」(Character class)來比較一組字元範圍,例如:
[abc] a、b或c
[^abc] 非a、b、c的其它字元
[a-zA-Z] a到z或A到Z(範圍)
[a-d[m-p]] a到d或m到p(聯集)
[a-z&&[def]] d、e或f(交集)
[a-z&&[^bc]] a到z,除了b與c之外(減集)
[a-z&&[^m-p]] a到z且沒有m到p(a-lq-z)(減集)










一次只指定一個字元不過癮,也可以用Greedy quantifiers來指定字元可能出現的次數:
X? X出現一次或完全沒有
X* X出現零次或多次
X+ X出現一次或多次
X{n} X出現n次
X{n,} X出現至少n次
X{n,m} X出現至少n次,但不超過m次


另外還有Reluctant quantifiers、Possessive quantifiers等的指定,您可以自行參考 java.util.regex.Pattern 類別中的說明。

在String類別中,matches()方法可以讓您驗證字串是否符合指定的正規表示式,這通常用於驗證使用者輸入的字串資料是否正確,例如電話號碼格式;replaceAll()方法可以將符合正規表示式的子字串置換為指定的字串;split()方法可以讓您依指定的正規表示式,將符合的子字串分離出來,並以字串陣列傳回。


下面這個程式示範幾個正則表示式的應用:
• UseRegularExpression.java
import java.util.Scanner;

public class UseRegularExpression {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);

String str = "abcdefgabcabc";
System.out.println(str.replaceAll(".bc", "###"));

System.out.print("輸入手機號碼: ");
str = scanner.next();

// 簡單格式驗證
if(str.matches("[0-9]{4}-[0-9]{6}"))
System.out.println("格式正確");
else
System.out.println("格式錯誤");

System.out.print("輸入href標籤: ");
// Scanner的next()方法是以空白為區隔
// 我們的輸入有空白,所以要執行兩次
str = scanner.next() + " " + scanner.next();

// 驗證href標籤
if(str.matches(""))
System.out.println("格式正確");
else
System.out.println("格式錯誤");

System.out.print("輸入電子郵件: ");
str = scanner.next();

// 驗證電子郵件格式
if(str.matches(
"^[_a-z0-9-]+([.][_a-z0-9-]+)*@[a-z0-9-]+([.][a-z0-9-]+)*$"))
System.out.println("格式正確");
else
System.out.println("格式錯誤");
}
}

公司統一編號驗證

/*
* 項 目 計 算 方 法 說 明
統一編號 0 4 5 9 5 2 5 7  
邏輯乘數 1 2 1 2 1 2 4 1 兩數上下對應相乘
  乘 積 0 8 5 1 5 4 2 7
    0 0 0 8 0 0 0 0 乘積直寫並上下相加
--------------------------------------------
乘積之和 0 8 5 9 5 4 2 7 將相加之和再相加
  0+8+5+9+5+4+2+7=40   正確!
最後結果, 40 能被 10 整除, 故 04595257 符合邏輯。

*若第七位數字為 7 時
統一編號 1 0 4 5 8 5 7 5 倒數號二位為 7
邏輯乘數 1 2 1 2 1 2 4 1 兩數上下對應相乘
  乘 積 1 0 4 1 8 1 2 5
    0 0 0 0 0 0 8 0 乘積直寫並上下相加
---------------------------------------------
乘積之和 1 0 4 1 8 1 1 5
0 再相加時最後第二位數取 0 或 1 均可。
  1+0+4+1+8+1+1+5=21  
  1+0+4+1+8+1+0+5=20  正確!
最後結果中, 20 能被 10 整除, 故 10458575 符合邏輯。




public static bool checkCompanyID(string sCompanyID)
{
try
{
//傳入公司統編長度不等於8就return
if(sCompanyID.Length != 8)
return false;

int aSum = 0;

//公司統編邏輯乘數( 1, 2, 1, 2, 1, 2, 4, 1 )
int[] LogicCompanyID = {1, 2, 1, 2, 1, 2, 4, 1};

for(int i = 0 ; i < LogicCompanyID.Length ; i++)
{
//公司統編與邏輯乘數相乘
int aMultiply = Convert.ToInt32(sCompanyID.Substring(i,1)) * LogicCompanyID[i];

//將相乘的結果, 取十位數及個位數相加
int aAddition = ((aMultiply / 10) + (aMultiply % 10));

//如果公司統編的第 7 位是 7 時, 會造成相加結果為 10 的特殊情況, 所以直接以 0 代替進行加總
aSum += (aAddition == 10)?0:aAddition;

}
//判斷總和的餘數, 假使為 0 公司統編正確回傳 true, 其它值則反之.
return (aSum % 10 == 0);
}
catch
{
return false;
}


例外狀況: 70664079