2009-01-22

JVD02FLEX 顯示系統時間 - 使用 FLEX-AS3 實作 JAVA 應用程式

題目敘述:

顯示系統時間

依不同的格式顯示目前系統中的日期與時間
1. 2003/5/7 上午 8:6
2. 2003/5/7 上午 08:06:47
3. 2003 年 5 月 7 日 上午08時06分47秒
4. 2003 年 5 月 7 日 星期三 上午08時06分47秒

評分項目

1. 顯示當地時間 TITLE 文字及上下各一分隔線 - 3 分
2. 是否能顯示 『2003/5/7 上午 8:6』 之系統日期時間 - 3 分
3. 是否能顯示 『2003/5/7 上午 08:06:47』 之系統日期時間 - 3 分
4. 是否能顯示 『2003 年 5 月 7 日 上午08時06 分 7秒』 之系統日期時間 - 3 分
5. 是否能顯示 『2003 年 5 月 7 日 星期三 上午08 時 06 分 47 秒』 之系統日期時間 - 3 分

總分 : 15 分

Author: 山羊
URL: http://gjo4rul4.blogspot.com

by Author :
這個範例是介紹如何使用 DateFormatter 來快速格式化日期欄位
當然還有置換字串的方法
再補充的部分是 ADOBE 對於 DateFormatter 所提供的範例
因此我放在一起 , 主要多了一個驗證器的使用方式


-------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" fontSize="14">
<mx:Script>
<![CDATA[
import mx.events.ValidationResultEvent;
/*
題目敘述:

顯示系統時間

依不同的格式顯示目前系統中的日期與時間
1. 2003/5/7 上午 8:6
2. 2003/5/7 上午 08:06:47
3. 2003 年 5 月 7 日 上午08時06分47秒
4. 2003 年 5 月 7 日 星期三 上午08時06分47秒

評分項目

1. 顯示當地時間 TITLE 文字及上下各一分隔線 - 3 分
2. 是否能顯示 『2003/5/7 上午 8:6』 之系統日期時間 - 3 分
3. 是否能顯示 『2003/5/7 上午 08:06:47』 之系統日期時間 - 3 分
4. 是否能顯示 『2003 年 5 月 7 日 上午08時06 分 7秒』 之系統日期時間 - 3 分
5. 是否能顯示 『2003 年 5 月 7 日 星期三 上午08 時 06 分 47 秒』 之系統日期時間 - 3 分

總分 : 15 分

Author: 山羊
URL: http://gjo4rul4.blogspot.com

by Author :
這個範例是介紹如何使用 DateFormatter 來快速格式化日期欄位
當然還有置換字串的方法
再補充的部分是 ADOBE 對於 DateFormatter 所提供的範例
因此我放在一起 , 主要多了一個驗證器的使用方式
*/

private function clickHandler():void{
//清空印出區域
this.printOutArea.text = "";
//印出 TITLE 與分隔線
this.printOutArea.text += "-----------------------------\n";
this.printOutArea.text += " 當地時間 \n";
this.printOutArea.text += "-----------------------------\n";
//套用 第一組 日期格式化物件 --> 因為印出來是 AM 與 PM 所以用置換方法 改成上午與下午
// 利用 .formate() 方法 取回格式化後字串
this.printOutArea.text += this.df1.format(new Date()).replace("AM","上午").replace("PM","下午") + "\n";
//套用 第二組 日期格式化物件 --> 因為印出來是 AM 與 PM 所以用置換方法 改成上午與下午
this.printOutArea.text += this.df2.format(new Date()).replace("AM","上午").replace("PM","下午") + "\n";
//套用 第三組 日期格式化物件 --> 因為印出來是 AM 與 PM 所以用置換方法 改成上午與下午
this.printOutArea.text += this.df3.format(new Date()).replace("AM","上午").replace("PM","下午") + "\n";
//套用 第四組 日期格式化物件 --> 因為印出來是 AM 與 PM 所以用置換方法 改成上午與下午
//第四組因為格式化並不會印出中文 所以 要判斷星期幾 並且套換成中文
if(this.df4.format(new Date()).indexOf("Mon")!=-1){
this.printOutArea.text += this.df4.format(new Date()).replace("AM","上午").replace("PM","下午").replace("Mon","星期一") + "\n";
}
if(this.df4.format(new Date()).indexOf("Tue")!=-1){
this.printOutArea.text += this.df4.format(new Date()).replace("AM","上午").replace("PM","下午").replace("Tue","星期二") + "\n";
}
if(this.df4.format(new Date()).indexOf("Wed")!=-1){
this.printOutArea.text += this.df4.format(new Date()).replace("AM","上午").replace("PM","下午").replace("Wed","星期三") + "\n";
}
if(this.df4.format(new Date()).indexOf("Thu")!=-1){
this.printOutArea.text += this.df4.format(new Date()).replace("AM","上午").replace("PM","下午").replace("Thu","星期四") + "\n";
}
if(this.df4.format(new Date()).indexOf("Fri")!=-1){
this.printOutArea.text += this.df4.format(new Date()).replace("AM","上午").replace("PM","下午").replace("Fri","星期五") + "\n";
}
if(this.df4.format(new Date()).indexOf("Sat")!=-1){
this.printOutArea.text += this.df4.format(new Date()).replace("AM","上午").replace("PM","下午").replace("Sat","星期六") + "\n";
}
if(this.df4.format(new Date()).indexOf("Sun")!=-1){
this.printOutArea.text += this.df4.format(new Date()).replace("AM","上午").replace("PM","下午").replace("Sun","星期日") + "\n";
}
}
//驗證結果事件
private var vResult:ValidationResultEvent;
//格式化日期
private function formatHandler():void{
vResult = dateVal.validate();
//如果驗證結果是合法的結果 才進行 日期格式的轉換
if (vResult.type==ValidationResultEvent.VALID) {
formattedDate.text = df2.format(dob.text);
}
//錯誤的話就清空結果字串欄位
else {
formattedDate.text= "";
}
}
]]>
</mx:Script>
<!--建立 四組 DateFormatter 日期格式化物件 屬性 formatString 輸入遮罩字串-->
<mx:DateFormatter id="df1" formatString="YYYY/M/D A L:N"/>
<mx:DateFormatter id="df2" formatString="YYYY/M/D A LL:NN:SS"/>
<mx:DateFormatter id="df3" formatString="YYYY年M月D日 ALL時NN分SS秒"/>
<mx:DateFormatter id="df4" formatString="YYYY年M月D日 EEE ALL時NN分SS秒"/>
<!--寫標題 輸出區 執行-->
<mx:Label text="JAVA 實力評量 JVD02 使用 FLEX 實作"/>
<mx:TextArea width="350" height="170" id="printOutArea"/>
<mx:Button label="執行" click="clickHandler()"/>
<!--補充的部分為 ADOBE 對於 DateFormatter 的使用範例-->
<mx:Label text="========補充========" color="#FF0A0A"/>
<mx:Label text="請輸入日期格式 (mm/dd/yyyy)"/>
<mx:HBox>
<mx:TextInput id="dob"/>
<mx:Button label="驗證" click="formatHandler()"/>
</mx:HBox>
<mx:HBox>
<mx:Label text="格式化日期"/>
<!--TextInput 屬性 editable 代表是否為一個可修改的欄位 -->
<mx:TextInput editable="false" id="formattedDate"/>
</mx:HBox>
<!--日期驗證器 屬性 source 代表 Binding {dob} 至個物件(這裡是一個文字欄位)
署另 inputFormat 代表可以輸入的日期格式 -->
<mx:DateValidator id="dateVal" source="{dob}" property="text" inputFormat="mm/dd/yyyy"/>

</mx:Application>

沒有留言: