我有一个约会对象,我需要从它开始getTime()
。问题是它始终显示00:00:00
。
SimpleDateFormat localDateFormat = new SimpleDateFormat("HH:mm:ss");
long date = Utils.getDateObject(DateObject).getTime();
String time = localDateFormat.format(date);
为什么时间总是
'00:00:00'
。我应该追加Time to my Date Object
吗 最佳答案
您应该将实际的Date
对象传递给format
,而不是long
:
SimpleDateFormat localDateFormat = new SimpleDateFormat("HH:mm:ss");
String time = localDateFormat.format(Utils.getDateObject(DateObject));
假设实际上是什么
Utils.getDateObject(DateObject)
都返回一个Date
(这是您的问题所隐含的,但并未实际说明),那么它应该可以正常工作。例如,这很完美:
import java.util.Date;
import java.text.SimpleDateFormat;
public class SDF {
public static final void main(String[] args) {
SimpleDateFormat localDateFormat = new SimpleDateFormat("HH:mm:ss");
String time = localDateFormat.format(new Date());
System.out.println(time);
}
}
在下面重新发表您的评论:
谢谢TJ,但实际上我还是得到00:00:00作为时间。
这意味着您的
Date
对象的小时,分钟和秒为零,如下所示:import java.util.Date;
import java.text.SimpleDateFormat;
public class SDF {
public static final void main(String[] args) {
SimpleDateFormat localDateFormat = new SimpleDateFormat("HH:mm:ss");
String time = localDateFormat.format(new Date(2013, 4, 17)); // <== Only changed line (and using a deprecated API)
System.out.println(time);
}
}