현재 날짜와 시간을 표시하십시오 작업공간에서 안드로이드 응용 프로그램 어떻게 합니까?
그땅은 아닌 하드 등 여러 가지 방법을. 내가 solaris. 옮길 현재 날짜 &. '시간' 에 테스트비에프.
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
// textView is the TextView view that should display it
textView.setText(currentDateTimeString);
더 쉽게 할 수 있는 붽뎄 읽어들입니다 찾을 수 있다 [here] [1] . # 39, ll 방법에 대한 자세한 내용은 좁히어 you& 사용되는 형식을 변경하십시오 변환.
[1]: http://developer.android.com/reference/java/text/DateFormat.html # getDateTimeInstance% 28%29
public class XYZ extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
Calendar c = Calendar.getInstance();
System.out.println("Current time => "+c.getTime());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
// formattedDate have current date/time
Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();
// Now we display formattedDate value in TextView
TextView txtView = new TextView(this);
txtView.setText("Current Date and Time : "+formattedDate);
txtView.setGravity(Gravity.CENTER);
txtView.setTextSize(20);
setContentView(txtView);
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Thread myThread = null;
Runnable runnable = new CountDownRunner();
myThread= new Thread(runnable);
myThread.start();
}
public void doWork() {
runOnUiThread(new Runnable() {
public void run() {
try{
TextView txtCurrentTime= (TextView)findViewById(R.id.lbltime);
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String curTime = hours + ":" + minutes + ":" + seconds;
txtCurrentTime.setText(curTime);
}catch (Exception e) {}
}
});
}
class CountDownRunner implements Runnable{
// @Override
public void run() {
while(!Thread.currentThread().isInterrupted()){
try {
doWork();
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}catch(Exception e){
}
}
}
}
명백하네 구입할 수 있는 시간을 표시하는 오른길로 '아나로그크로크' 보기 및 '디지털크로크' 보기.
예를 들어, 다음 레이아웃뿐만:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<AnalogClock
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<DigitalClock
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"/>
</LinearLayout>
다음과 같습니다.
[스크린샷] [3]!
[3]: png https://content.screencast.com/users/davweb/folders/snagit/media/5d8976bd-4833-4a3f-a3a5-74cbed922891/02.16. 2010-09.09.17
단 한 줄도 경우에 대비하여 코드:
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
그 결과, 16:50:34" " ',' 25-09-2016
Https://stackoverflow.com/questions/7054945/how-to-get-full-date-with-correct-format 에서:
사용하십시오
android.text.format.DateFormat.getDateFormat(Context context)
android.text.format.DateFormat.getTimeFormat(Context context)
현재 시간, 날짜 형식의 afaq 유효함 감지에서 사용자 설정 (12/24 시간 형식, 예를 들어).
import android.text.format.DateFormat;
private void some() {
final Calendar t = Calendar.getInstance();
textView.setText(DateFormat.getTimeFormat(this/*Context*/).format(t.getTime()));
}
다음은 코드에는 협력했습니다 for me. 이 시도하시오. 시스템 호출 () 에서 날짜 시간이 오래 걸리고 있는 것은 간단한 방법. 당신이 필요로 하는 방법을 어딜가 날짜 시간 ().
public static String Datetime()
{
Calendar c = Calendar .getInstance();
System.out.println("Current time => "+c.getTime());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mms");
formattedDate = df.format(c.getTime());
return formattedDate;
}
사용:
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
int minutes = c.get(Calendar.MINUTE);
int hour = c.get(Calendar.HOUR);
String time = hour + ":" + minutes + ":" + seconds;
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
String date = day + "/" + month + "/" + year;
// Assuming that you need date and time in a separate
// textview named txt_date and txt_time.
txt_date.setText(date);
txt_time.setText(time);
사실, re 베스트중에 오프하도록 테스트클로크 you& # 39 를 위젯. 이 모든 # 39 에 대한 핸들을 복잡성입니다 너회들의 기본설정에는 12/24hr user& 존중할 것이라고 말했다. http://developer.android.com/reference/android/widget/TextClock.html
Calendar c = Calendar.getInstance();
int month=c.get(Calendar.MONTH)+1;
String sDate = c.get(Calendar.YEAR) + "-" + month+ "-" + c.get(Calendar.DAY_OF_MONTH) +
"T" + c.get(Calendar.HOUR_OF_DAY)+":"+c.get(Calendar.MINUTE)+":"+c.get(Calendar.SECOND);
이 같은 형식을 부여하느뇨 날짜 및 시간 05 24t18 2010 13)
현재 날짜 표시 기능:
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String date = df.format(c.getTime());
Date.setText(date);
가져올 합니다
>. 가져오기의 제바스티스트롬심레다테포르마. >. 가져오기의 제바스티우스칼렌다.
사용하려고 합니다.
TextView Date;
Date = (TextView) findViewById(R.id.Date);
종료기 아래 코드:
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println("time => " + dateFormat.format(cal.getTime()));
String time_str = dateFormat.format(cal.getTime());
String[] s = time_str.split(" ");
for (int i = 0; i < s.length; i++) {
System.out.println("date => " + s[i]);
}
int year_sys = Integer.parseInt(s[0].split("/")[0]);
int month_sys = Integer.parseInt(s[0].split("/")[1]);
int day_sys = Integer.parseInt(s[0].split("/")[2]);
int hour_sys = Integer.parseInt(s[1].split(":")[0]);
int min_sys = Integer.parseInt(s[1].split(":")[1]);
System.out.println("year_sys => " + year_sys);
System.out.println("month_sys => " + month_sys);
System.out.println("day_sys => " + day_sys);
System.out.println("hour_sys => " + hour_sys);
System.out.println("min_sys => " + min_sys);
/// For Show Date
String currentDateString = DateFormat.getDateInstance().format(new Date());
// textView is the TextView view that should display it
textViewdate.setText(currentDateString);
/// For Show Time
String currentTimeString = DateFormat.getTimeInstance().format(new Date());
// textView is the TextView view that should display it
textViewtime.setText(currentTimeString);
확인란 전체 코드 안드로이드 - 현재 날짜와 시간을 표시하십시오 작업공간에서 안드로이드 스튜디오 예와 함께 소스 코드