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
2010-02-16 07:44:44
Getting error at getDateInstance() and new Date()
RBADS
2010-02-16 07:53:50
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
2010-02-16 11:08:09
Thank you very much sir.i got time
RBADS
2010-02-17 04:54:47
sorry sir, i got date not time.similarly can we get time ?
RBADS
2010-02-17 06:30:37
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
2010-02-17 08:14:16
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:
Dave Webb
2010-02-16 09:01:28
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
2010-07-21 10:39:38