I'm trying to get the current time including milliseconds, but it only shows SSS.
Here is my code:
import android.text.format.DateFormat
import java.util.Calendar
...
String.format(
"%s IOException: %s",
DateFormat.format("HH:mm:ss.SSS", Calendar.getInstance().time),
ex.message
)
It outputs 20:41:03.SSS some other text
. Also tried just toasting the date, same output.
CodePudding user response:
Well you can't get milliseconds with DateFormat
, you need to use SimpleDateFormat
, so your code should look like this:
String.format(
"%s IOException: %s",
SimpleDateFormat("HH:mm:ss.SSS", Locale.getDefault()).format(Calendar.getInstance().time),
ex.message
)
and also you could use Date()
instead of Calendar.getInstance().time
String.format(
"%s IOException: %s",
SimpleDateFormat("HH:mm:ss.SSS", Locale.getDefault()).format(Date()),
ex.message
)
CodePudding user response:
Use the below code, it should work
import android.text.format.DateFormat
import java.util.Calendar
...
String.format(
"%s IOException: %s",
new SimpleDateFormat("HH:mm:ss.SSS").format(Calendar.getInstance().time),
ex.message
)