在 Android 应用程序中显示当前时间和日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2271131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display the current time and date in an Android application
提问by BIBEKRBARAL
How do I display the current date and time in an Android application?
如何在 Android 应用程序中显示当前日期和时间?
回答by Zordid
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
.
好的,并不难,因为有几种方法可以做到这一点。我假设您想将当前日期和时间放入TextView
.
String currentDateTimeString = java.text.DateFormat.getDateTimeInstance().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.
可以在此处轻松找到文档中的更多内容 。在那里您将找到有关如何更改用于转换的格式的更多信息。
回答by BIBEKRBARAL
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);
}
}
回答by Prashant
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){
}
}
}
}
回答by Dave Webb
The obvious choices for displaying the time are the AnalogClock
Viewand the DigitalClock
View.
显示时间的明显选择是AnalogClock
View和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:
看起来像这样:
回答by Mahdi Astanei
In case you want a single line of code:
如果你想要一行代码:
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
The result is "2016-09-25 16:50:34"
结果是 "2016-09-25 16:50:34"
回答by fred
My own working solution:
我自己的工作解决方案:
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);
Hope this helps!
希望这可以帮助!
回答by Nam Vu
If you want to get the date and time in a specific pattern you can use
如果您想以特定模式获取日期和时间,您可以使用
Date d = new Date();
CharSequence s = DateFormat.format("yyyy-MM-dd hh:mm:ss", d.getTime());
回答by wonder.mice
From How to get full date with correct format?:
Please, use
请用
android.text.format.DateFormat.getDateFormat(Context context)
android.text.format.DateFormat.getTimeFormat(Context context)
to get valid time and date formats in sense of current user settings (12/24 time format, for example).
获取与当前用户设置相关的有效时间和日期格式(例如,12/24 时间格式)。
import android.text.format.DateFormat;
private void some() {
final Calendar t = Calendar.getInstance();
textView.setText(DateFormat.getTimeFormat(this/*Context*/).format(t.getTime()));
}
回答by vinoth
Here is the code which worked for me. Please try this. It is a simple method which takes time and date from a system call. The method Datetime() whereever you needed.
这是对我有用的代码。请试试这个。这是一种从系统调用中获取时间和日期的简单方法。任何你需要的方法 Datetime() 。
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;
}
回答by Joy Panchal
Use:
用:
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);