tags:

views:

2333

answers:

3

How to display current date and time in android application?

A: 

Okay, not that hard as there are several methods to do this. I assume you want to put the current date & time into a TextView.

String currentDateTimeString = DateFormat.getDateInstance().format(new Date());

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

There is more to read in the documentation that can easily be found here. There you'll find more information on how to change the format used for conversion.

Zordid
Getting error at getDateInstance() and new Date()
RBADS
Please - be more explicit! What's the error? Did you import the wrong DateFormat class? It's `java.text.DateFormat` and NOT `android.text.format.DateFormat`! And it's `java.util.Date` and NOT `java.sql.Date`!Just a little hint on asking questions: try to be precise, e.g.: declare what you mean by "display" in your question. And when you type in my lines - both Date and DateFormat must, of course, be imported - if there's a choice of 2 for each, the least you could try is any combination: it's just 4!
Zordid
Thank you very much sir.i got time
RBADS
sorry sir, i got date not time.similarly can we get time ?
RBADS
Have a look at http://developer.android.com/reference/java/text/SimpleDateFormat.html - there you can see how to define **exactly** what you want to be in your output string. E.g. for time use `"HH:mm:ss"`! Completely: `currentTimeString = new SimpleDateFormat("HH:mm:ss").format(new Date());`
Zordid
A: 

The obvious choices for displaying the time are the AnalogClock View and the DigitalClock View.

For example, the following layout:

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

Looks like this:

screenshot

Dave Webb
dear sir, i want to display current time using setText.
RBADS
A: 

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 + ":"+minute + ":"+ 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){
                }
            }
    }
}
Prashant