Android-如何擷取系統目前日期時間的方法(2): Calendar dt = Calendar.getInstance();
Android-如何擷取系統目前日期時間的方法(2):
Calendar dt = Calendar.getInstance();
注意:整個事件程式碼都必須放在
onCreate()
{
}
下麵
……………………..
範例:
import android.view.*;
import android.widget.*;
import java.util.Calendar;
import java.util.Date;
// button2
事件處理程式
public void button2_Click(View view) {
//## date ##############
Calendar dt = Calendar.getInstance();
int thisYear = dt.get(Calendar.YEAR);
int thisMonth = dt.get(Calendar.MONTH)+1;
int thisDate = dt.get(Calendar.DAY_OF_MONTH);
int thisHour = dt.get(Calendar.HOUR_OF_DAY);
int thisMin = dt.get(Calendar.MINUTE);
int thisSec = dt.get(Calendar.SECOND);
///
輸出在editText
EditText ed2 = (EditText) findViewById(R.id.editText2);
ed2.setText(Integer.toString(thisYear)+”/” + Integer.toString(thisMonth)+”/”
+ Integer.toString(thisDate)+” ” + Integer.toString(thisHour)+”:”
+ Integer.toString(thisMin)+”:” + Integer.toString(thisSec));
} //end of button2_Click()