如何在Android应用程序中显示当前日期和时间?在Android应用程序中显示当前时间和日期
回答
好吧,并不难,因为有几种方法可以做到这一点。我假设你想把当前日期&时间放入TextView
。
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
// textView is the TextView view that should display it
textView.setText(currentDateTimeString);
还有更多可以很容易地找到here 文档中读取。您可以在这里找到更多关于如何更改用于转换的格式的信息。
显示时间的明显选择是AnalogClock
View和DigitalClock
View。
例如,以下布局:
<?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>
是这样的:
亲爱的先生,我想使用setText显示当前时间。 – BIBEKRBARAL 2010-02-16 09:19:51
看完这个明显的答案后,我觉得自己像个笨蛋!我实现了自己的runnable,让它睡了一段时间,等等,当明显的答案是XML-one-liner!非常感谢(在您发布后超过一年):-) – dbm 2011-02-22 08:08:29
2015年已弃用,建议您使用TextClock代替。 :) – Evilripper 2015-02-27 09:45:27
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){
}
}
}
}
请提供runOnUiThread函数的定义。 – 2011-10-24 11:30:13
@Harshit只要你的类扩展了这个函数就可以使用Android SDK活动 – 2012-01-08 16:24:16
我知道这是个老问题,但是如果有人会像我一样在谷歌中找到它,他应该知道Date.getX方法已被弃用。 – tobi 2012-07-20 09:28:05
我自己工作的解决方案:
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);
希望日是有帮助的!
这将使当前日期和时间:
public String getCurrDate()
{
String dt;
Date cal = Calendar.getInstance().getTime();
dt = cal.toLocaleString();
return dt;
}
从How to get full date with correct format?:
请使用
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()));
}
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);
}
}
如果你想在一个特定的模式的日期和时间,您可以使用
Date d = new Date();
CharSequence s = DateFormat.format("yyyy-MM-dd hh:mm:ss", d.getTime());
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
试试下面的代码:
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);
String formattedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
使用formattedDate
作为填充日期之前,您String
。
对我而言:mDateButton.setText(formattedDate);
实际上,您最好使用TextClock小部件。它为您处理所有复杂问题,并将尊重用户的12/24小时偏好。 http://developer.android.com/reference/android/widget/TextClock.html
确实是处理它的最好方法。 +1 – gor 2014-12-30 13:02:47
这是我工作的代码。请试试这个。这是一个简单的方法,它需要系统调用的时间和日期。 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;
}
用途:
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);
只需复制此代码,并希望这个作品对你罚款。
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
String strDate = sdf.format(c.getTime());
如果你想要的一行代码:
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
结果是"2016-09-25 16:50:34"
(y)很好的回答:) – 2017-03-24 20:19:19
显示当前日期函数:
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String date = df.format(c.getTime());
Date.setText(date);
你必须要进口
import java.text.SimpleDateFormat; import java.util.Calendar;
你必须要使用
TextView Date;
Date = (TextView) findViewById(R.id.Date);
String currentDateandTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
Toast.makeText(getApplicationContext(), currentDateandTime, Toast.LENGTH_SHORT).show();
为了得到当前时间/日期只需使用下面的代码片段:
要使用时间:
SimpleDateFormat simpleDateFormatTime = new SimpleDateFormat("HH:mm", Locale.getDefault());
String strTime = simpleDateFormatTime.format(now.getTime());
要使用日期:
SimpleDateFormat simpleDateFormatDate = new SimpleDateFormat("E, MMM dd, yyyy", Locale.getDefault());
String strDate = simpleDateFormatDate.format(now.getTime());
,你是好去。
对于显示当前日期和时间上的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 – Display the current date and time in an Android Studio Example with source code
- 1. 显示调度程序当前日期和固定时间extjs
- 2. 如何显示当前日期? Android应用程序
- 3. 显示实况当前日期时间
- 4. 如何在Toad for Oracle中显示当前日期和时间?
- 5. 在android中比较输入日期和时间与当前日期和时间
- 6. Xcode在创建地图应用程序时在Datepicker上显示当前日期
- 7. 在C中的MessageBox中显示当前日期时间#
- 8. 如何在wp7应用程序上显示当前时间?
- 9. 显示当前工作日和日期
- 10. 使用日期/时间选取器显示日期/时间Android
- 11. 是否有显示当前日期和时间的功能?
- 12. 显示当前系统日期和时间
- 13. ET中的当前日期和时间
- 14. 当选择日期时间比当前日期晚时Javascript显示错误
- 15. Web应用程序中的时区敏感日期和时间显示?
- 16. 在GWT中添加日期和时间到当前日期和时间
- 17. 显示当前日期
- 18. Sharepoint:显示当前日期
- 19. MySQL日期时间和当前日期时间函数
- 20. 如何在Android应用程序中显示日期选择器
- 21. 在Excel项目时间表上显示当前的周/日期
- 22. 无法显示当前日期和日期前一周之间的值
- 23. Android中当前日期和输入日期之间的差异
- 24. 如何检测星期几和时间,然后根据当前日期和时间在tableview中显示数据?
- 25. XSLT:以当前日期和时间
- 26. Mysql/Php - 当前日期和时间
- 27. 显示时间和日期在树枝
- 28. 如何比较UTC日期和Android中的当前UTC时间?
- 29. 比较日期时间到MySQL中的当前日期时间?
- 30. sencha touch - datepickerfield在当前日期之前不显示日期
得到错误在getDateInstance()和新的Date() – BIBEKRBARAL 2010-02-16 07:53:50
请 - 更加明确!什么是错误?你输入了错误的DateFormat类吗?它是'java.text.DateFormat'而不是'android.text.format.DateFormat'!它是'java.util.Date'而不是'java.sql.Date'! 只是提出一些问题提示:尽量准确,例如:在你的问题中声明你的意思是“显示”。当你输入我的行时 - 当然,Date和DateFormat必须被导入 - 如果每个选项都是2,你可以尝试的最少是任意组合:它只有4! – Zordid 2010-02-16 11:08:09
非常感谢你sir.i得到了时间 – BIBEKRBARAL 2010-02-17 04:54:47