我正在尝试在我的 Android 应用程序 (4.2.2)、Eclipse Luna (4.4.0) 上显示当前日期。在下面的代码中,我在使用 Date()
时收到错误,提示我必须添加参数。但是,添加 0
或 long
当然会导致日期为 1969 年 12 月 31 日 7:00。
final DateFormat dateTimeFormatter = DateFormat.getDateTimeInstance();
graph.setCustomLabelFormatter(new CustomLabelFormatter(){
@Override
public String formatLabel(double value, boolean isValueX)
{
if (isValueX)
{
return dateTimeFormatter.format(new Date(0)); //how to append date to value
}
return null; //graphview generates Y-axis label
}
});
Date()
仍然可以用来获取当前日期吗?
最佳答案
您正在使用 epoch date constructor (经过纪元后经过 0 毫秒的特定时间),因此正确的输出实际上是 1969 年 12 月 31 日 7:00。
如果您使用 default constructor new Date()
它将默认为 System.currentTimeMillis() 并为您提供当前时间戳。
此外,请确保您使用的是 java.util.Date
而不是 java.sql.Date
关于java - Date() 的使用;不给出当前日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25651500/