使用Android日历获取当前时间

时间:2012-11-08 13:46:59

标签: java android date time android-calendar

我想在Android应用中获取当前时间(HH:MM:SEC:MILLISEC)。我正在使用这段代码:

Calendar c = Calendar.getInstance(); 
int time_start = c.get(Calendar.MILLISECOND);

“获取和设置的字段编号,表示小时内的分钟。例如,在10:04:15.250 PM,MILLI为250。”

我已经查看了其他方法,但我找不到任何指定它会输出所有内容的内容。无论如何我能得到H:M:Sec:MilliSec?或者我只需做一些像

这样的事情
c.get(Calendar.HOUR).get(Calendar.MINUTE).get(Calendar.SECOND).get(Calendar.MILLISECOND).

3 个答案:

答案 0 :(得分:13)

您可以尝试使用SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");

或许这样:

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
String test = sdf.format(cal.getTime());
Log.e("TEST", test);

答案 1 :(得分:6)

这个怎么样?

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:S");
String result = sdf.format(Calendar.getInstance().getTime());
System.out.println(result);

答案 2 :(得分:4)

我会采用System.currentTimeMillis()new Date()方式,并将其放入SimpleDateFormat,以获得您喜欢的输出

相关问题