java - 如何显示当前日期?安卓应用程序

标签 java android date

我创建了一个 Android 应用程序,我想在屏幕上显示当前日期。我使用这段代码:

Date date = new Date(0);
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
text.setText("Time: " + dateFormat.format(date));`

但是这段代码显示了日期“1.1.1970”。

如何显示当前日期?

最佳答案

Date(0) 是 1970 年 1 月,参见 the documentation of the Date(long milliseconds) constructor :

public Date(long milliseconds)

Initializes this Date instance using the specified millisecond value. The value is the number of milliseconds since Jan. 1, 1970 GMT.

要获取当前日期,请使用Date()constructor without a parameter :

public Date()

Initializes this Date instance to the current time.

关于java - 如何显示当前日期?安卓应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11839968/

相关文章:

java - 多线程的新手 - 多线程的最具成本效益的方法?

java - 如何测试在 JUnit 中的测试方法下创建的线程的完成情况

android - ListView项目背景颜色

javascript - 使用 angularJS 和输入类型 ="date"更新 mongodb 中的日期

php - 设置日期标题以降低 SpamAssassin 分数

java - 从 Java 管理动态 JavaScript 的最佳实践是什么?

java - 嵌套 Hashmap 覆盖其他键

android - 清除 GridView 并在 android 中重新生成它

java - 膨胀类 fragment GoogleMaps 时出错

javascript - ECMAScript 5.1 规范不正确?日期构造函数日期/时间格式解析为 UTC