EGHDK
EGHDK

Reputation: 18120

Using Android Calendar to get the current time

I'm trying to get the current time (HH:MM:SEC:MILLISEC) in an android app. I'm using this piece of code:

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

"Field number for get and set indicating the minute within the hour. E.g., at 10:04:15.250 PM the MILLI is 250."

I've looked through the other methods, but I couldn't find anything specifying it would output everything. Is there anyway I can get H:M:Sec:MilliSec? or do I just have to do something like

c.get(Calendar.HOUR).get(Calendar.MINUTE).get(Calendar.SECOND).get(Calendar.MILLISECOND).

Upvotes: 8

Views: 28825

Answers (3)

Siddharth Lele
Siddharth Lele

Reputation: 27748

You could try with SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");

Like this perhaps:

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

Upvotes: 19

user1162766
user1162766

Reputation:

What about this?

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

Upvotes: 9

Rafael T
Rafael T

Reputation: 15679

I would go the System.currentTimeMillis() or the new Date() way, and put these in a SimpleDateFormat, to get exactly the output you like

Upvotes: 3

Related Questions