2010年11月30日 星期二

[ Java 代碼範本 ] java.text.SimpleDateFormat : 日期物件的格式化/文字化/標準化


前言 :
首先讓我們來看看 JDK 如何形容 java.text.SimpleDateFormat 這個物件 :
SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization.
SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. However, you are encouraged to create a date-time formatter with either getTimeInstance, getDateInstance, or getDateTimeInstance in DateFormat. Each of these class methods can return a date/time formatter initialized with a default format pattern. You may modify the format pattern using the applyPattern methods as desired. For more information on using these methods, see DateFormat.

下面是格式化字元對應說明表 :


範例說明 :
* 類別 SimpleDateFormatTest 代碼 :
  1. package john.text;  
  2.   
  3. import java.text.DateFormat;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.Date;  
  6.   
  7. import john.text.*;  
  8.   
  9. public class SimpleDateFormatTest {  
  10.     public static void main(String args[]) {  
  11.         //----------------------------------------------------------------------------//  
  12.         // 模式文字格式化  
  13.         // SimpleDateFormat("模式文字");  
  14.         //----------------------------------------------------------------------------//  
  15.         Date date = new Date();  
  16.           
  17.         System.out.println("-----------------------------------------------------------------");  
  18.         System.out.println("模式文字格式化");  
  19.         System.out.println("-----------------------------------------------------------------");  
  20.           
  21.         DateFormat dateformat = new SimpleDateFormat("yyyyMMddHHmmss");  
  22.         System.out.println("yyyyMMddHHmmss                |" + dateformat.format(date));  
  23.           
  24.         dateformat = new SimpleDateFormat("yyyy/MM/dd (EE) HH:mm:ss.SSSSS");  
  25.         System.out.println("yyyy/MM/dd (EE) HH:mm:ss.SSSSS|" + dateformat.format(date));  
  26.           
  27.         System.out.println("-----------------------------------------------------------------");  
  28.           
  29.         // 分秒12:34固定了時  
  30.         dateformat = new SimpleDateFormat("yyyyMMddHH1234");  
  31.         System.out.println("yyyyMMddHH1234                |" + dateformat.format(date));  
  32.           
  33.         dateformat = new SimpleDateFormat("yyyy/MM/dd (EE) HH:12:34.SSSSS");  
  34.         System.out.println("yyyy/MM/dd (EE) HH:12:34.SSSSS|" + dateformat.format(date));  
  35.           
  36.         System.out.println("-----------------------------------------------------------------");  
  37.           
  38.         // 順便說一句、這樣的風雅也能夠。  
  39.         dateformat = new SimpleDateFormat("yyyy年MM月dd日 EE星期 (^_-)-☆");  
  40.         System.out.println("yyyy年MM月dd日 EE星期 (^_-)-☆|" + dateformat.format(date));     
  41.         
  42.     }  
  43. }  

執行結果 :
-----------------------------------------------------------------
模式文字格式化
-----------------------------------------------------------------
yyyyMMddHHmmss |20100419223045
yyyy/MM/dd (EE) HH:mm:ss.SSSSS|2010/04/19 (星期一) 22:30:45.00460
-----------------------------------------------------------------
yyyyMMddHH1234 |20100419221234
yyyy/MM/dd (EE) HH:12:34.SSSSS|2010/04/19 (星期一) 22:12:34.00460
-----------------------------------------------------------------
yyyy年MM月dd日 EE星期 (^_-)-☆|2010年04月19日 星期一星期 (^_-)-☆


補充說明 :
* WisdomFish - IT3.0 中庸學習之道 : 類別 DateFormat
DateFormat 是日期/時間格式化子類別的抽象類別,它以與語言無關的方式格式化並解析日期或時間。日期/時間格式化子類別(如 SimpleDateFormat)允許進行格式化(也就是日期 -> 文本)、解析(文本-> 日期)和標準化。將日期表示為 Date 物件,或者表示為從 GMT(格林尼治標準時間)1970 年 1 月 1 日 00:00:00 這一刻開始的毫秒數。

* JDK 6.0 : java.text.SimpleDateFormat
* JavaWorld : 日期格式輸出的介紹
This message was edited 3 times. Last update was at 12/11/2010 12:15:06

2010年11月29日 星期一

[ 資料結構 小學堂 ] 搜尋 : 二元搜尋法

前言 : 
如果要搜尋的資料已經事先排序好, 則可以使用二元搜尋法 (Binary Search) 來進行搜尋. 二元搜尋法是將資料分割成兩等份, 再比較鍵值與中間值的大小, 如果鍵值小於中間值, 可以確定要找的資料再前半段的元素, 否則在後半部. 如此分割數次直到找到或確定不存在為止. 

二元搜尋法範例說明 : 
考慮有已排序序數列 2, 3, 5, 8, 9, 11, 12, 16, 18 而要搜尋值為 11 時 : 
Step1 : 首先更第五個數值9 比較, 因為11 > 9, 所以往後半部移動. 

2, 3, 5, 8, 9, 11, 12, 16, 18 #11>9

Step2 : 接著選擇後半段中間值 12 比較, 因為11 < 12, 所以往前半部移動. 

2, 3, 5, 8, 9, 11, 12, 16, 18 # 11<12

Step3 : 因為前半部只剩下11=11, 表示搜尋完成. 

二元法分析 : 

1. 時間複雜度 : 因為每次的搜尋都會比上一次少一半的範圍, 最多只需要比較 log2n+1 或 log2(n+1), 時間複雜度為 O(logn).
2. 二分法必須事先經過排序, 且資料量必須能直接在記憶體中執行.
3. 此法適合於不需增刪的靜態資料.

範例代碼 : 

  1. #include   
  2.   
  3. using namespace std;  
  4. int binarySearch(int data[50], int val){  
  5.     int low=0, high=49, mid=0;  
  6.     cout << "搜尋處理中..." << endl;  
  7.     while(low<=high && val!=-1) {  
  8.         mid = (low+high)/2;  
  9.         if(val
  10.             high = mid-1;  
  11.             cout << val << " 介於 data[" << low << "]=" << data[low] << " 與 data[" << mid << "]" << data[mid] << endl;  
  12.         } else if(val>data[mid]) {  
  13.             low = mid+1;  
  14.             cout << val << " 介於 data[" << mid << "]=" << data[mid] << " 與 data[" << high << "]" << data[high] << endl;  
  15.         } else{  
  16.             return mid;  
  17.         }  
  18.     }  
  19.     return -1;  
  20. }  
  21. void main() {  
  22.     int data[50] = {0}, val=1;  
  23.     for(int i=0; i<50; i++) {  
  24.         data[i] = val;  
  25.         val+=(rand()%5+1);  
  26.     }  
  27.     int key=0, num;  
  28.     while(1) {  
  29.         cout << "請輸入搜尋值(1-150), 輸入-1結速: ";  
  30.         cin >> key;  
  31.         if(key<0)  
  32.             break;  
  33.         num = binarySearch(data, key);  
  34.         if(num>=0) {  
  35.             cout << "在第 " << num << "個位置找到 ( data[" << num << "]=" << data[num] << "." << endl;   
  36.         } else {  
  37.             cout << "##### 沒有找到 " << key << "#####" << endl;  
  38.         }  
  39.     }  
  40. }  
執行結果 : 

請輸入搜尋值(1-150), 輸入-1結束: 23 <輸入值進行搜尋>
搜尋處理中...
23 介於 data[0]=1 與 data[24]72
23 介於 data[0]=1 與 data[11]39
23 介於 data[5]=17 與 data[10]38
23 介於 data[6]=22 與 data[8]30
23 介於 data[6]=22 與 data[7]26
23 介於 data[7]=26 與 data[7]26
##### 沒有找到 23#####
請輸入搜尋值(1-150), 輸入-1結束: 17
搜尋處理中...
17 介於 data[0]=1 與 data[24]72
17 介於 data[0]=1 與 data[11]39
在第 5個位置找到 ( data[5]=17.)

補充說明 : 
* Wiki : Binary Search 

In computer science, a binary search is an algorithm for locating the position of an element in a sorted list. It inspects the middle element of the sorted list: if equal to the sought value, then the position has been found; otherwise, the upper half or lower half is chosen for further searching based on whether the sought value is greater than or less than the middle element...

* [C 範例代碼] 尋找演算法 : 二分查找

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...