I am having milliseconds value and want to display the time subtracting 5 minutes from current milliseconds value in hh:mm:ss
format.
Code
String str = String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours((cal.getTimeInMillis()-300000)),
TimeUnit.MILLISECONDS.toMinutes(cal.getTimeInMillis()-300000) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(cal.getTimeInMillis()-300000)),
TimeUnit.MILLISECONDS.toSeconds(cal.getTimeInMillis()-300000) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(cal.getTimeInMillis()-300000)));
Toast.makeText(getApplicationContext(), "Alarm Set."+str, Toast.LENGTH_LONG).show()
Output now
Alarm Set. 386467:25:00
Output Required
Alarm Set. 07:25:00
As you see minutes
and seconds
are getting retrieved quiet right but there's some problem with hours
.
P.S
1.I referred this post.They say it works fine.But don't know why not in my case.
2.I am sure about what i want to get as hours value i.e 07
as i have set the value using Calendar.HOUR
and its getting displayed too if i use cal.get(Calendar.HOUR)
.cal is of course object of Calendar
class.
Alternative Solution
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
String str1 = sdf.format(new Date(cal.getTimeInMillis()-300000));
Toast.makeText(getApplicationContext(), "Alarm Set."+str1, Toast.LENGTH_LONG).show();