208

Android アプリケーションで現在の日付と時刻を表示するにはどうすればよいですか?

4

23 に答える 23

308

わかりました、これを行うにはいくつかの方法があるので、それほど難しくありません。現在の日付と時刻をTextView.

String currentDateTimeString = java.text.DateFormat.getDateTimeInstance().format(new Date());

// textView is the TextView view that should display it
textView.setText(currentDateTimeString);

ここ で簡単に見つけることができるドキュメントには、さらに読むべき情報があります 。そこには、変換に使用される形式を変更する方法に関する詳細が記載されています。

于 2010-02-16T07:44:44.293 に答える
126
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);
    }

}

ここに画像の説明を入力してください

于 2011-11-04T10:58:45.877 に答える
52
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){
                }
            }
    }
}
于 2010-07-21T10:39:38.960 に答える
41

時間を表示するための明白な選択肢は、AnalogClockビューDigitalClockビューです。

たとえば、次のレイアウト:

<?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>

このように見えます:

スクリーンショット

于 2010-02-16T09:01:28.937 に答える
22

私自身の作業ソリューション:

Calendar c = Calendar.getInstance();

String sDate = c.get(Calendar.YEAR) + "-" 
+ c.get(Calendar.MONTH)
+ "-" + c.get(Calendar.DAY_OF_MONTH) 
+ " at " + c.get(Calendar.HOUR_OF_DAY) 
+ ":" + c.get(Calendar.MINUTE);

お役に立てれば!

于 2011-06-08T11:43:09.230 に答える
20

特定のパターンで日付と時刻を取得したい場合は、使用できます

Date d = new Date();
CharSequence s = DateFormat.format("yyyy-MM-dd hh:mm:ss", d.getTime());
于 2012-01-29T06:15:30.963 に答える
14

正しい形式で完全な日付を取得する方法から? :

使ってください

android.text.format.DateFormat.getDateFormat(Context context)
android.text.format.DateFormat.getTimeFormat(Context context)

現在のユーザー設定の意味で有効な時刻と日付の形式を取得します (たとえば、12/24 時間形式)。

import android.text.format.DateFormat;

private void some() {
    final Calendar t = Calendar.getInstance();
    textView.setText(DateFormat.getTimeFormat(this/*Context*/).format(t.getTime()));
}
于 2011-10-27T21:06:33.347 に答える
11

これが私のために働いたコードです。これを試してください。システムコールから日時を取得する簡単な方法です。

public static String getDatetime() {
    Calendar c = Calendar .getInstance();
    System.out.println("Current time => "+c.getTime());
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mms");
    String formattedDate = df.format(c.getTime());
    return formattedDate;
}
于 2014-05-12T07:30:00.113 に答える
8

使用する:

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);
于 2014-07-22T05:10:41.937 に答える
7

実際には、TextClock ウィジェットを使用するのが最善です。すべての複雑さを処理し、ユーザーの 12/24 時間の設定を尊重します。 http://developer.android.com/reference/android/widget/TextClock.html

于 2014-05-02T15:25:04.250 に答える
7
String formattedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()); 

日付を入れてformattedDateご利用ください。 私の場合:String
mDateButton.setText(formattedDate);

于 2013-09-24T11:53:14.690 に答える
6
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);

これにより、2010-05-24T18:13:00 のような日時形式が得られます

于 2012-03-14T04:59:03.113 に答える
5

このコードをコピーするだけで、問題なく動作することを願っています。

Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
String strDate = sdf.format(c.getTime());
于 2015-07-20T08:10:22.800 に答える
5

これにより、現在の日付と時刻が得られます。

public String getCurrDate()
{
    String dt;
    Date cal = Calendar.getInstance().getTime();
    dt = cal.toLocaleString();
    return dt;
}
于 2011-07-27T11:04:34.957 に答える
1

Textview に現在の日付と時刻を表示する場合

    /// 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);

完全なコードAndroid を確認する – ソース コードを含む Android Studio の例で現在の日付と時刻を表示する

于 2018-03-08T06:17:41.650 に答える