我创建了一个 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/