java - Convert Hours and Minutes into Milliseconds and return into HH:mm:ss Format -
using timeunit wanted convert hours , minutes milliseconds below code:
int hours = 01, minutes = 0; long milliseconds = timeunit.seconds.tomillis(timeunit.hours.toseconds(hours) + timeunit.minutes.toseconds(minutes)); works fine 3600000 milliseconds
and using simpledateformat return hh:mm:ss format:
simpledateformat formatter = new simpledateformat("hh:mm:ss"); date date = new date(milliseconds); string returnformat = formatter.format(date); //final result. and give me result of 09:59:59 not expected output.
i confused, what's wrong code above? expecting 01:00:00 output.
update:
actually using above code create simple countdown timer using handler post.delayed function.
... @override public void run() { milliseconds -= 1000; //remove 1 seconds handler.postdelayed(this,1000); //delay in 1 seconds simpledateformat formatter = new simpledateformat("hh:mm:ss"); //create time formatting date date = new date(milliseconds); //put milliseconds format string returnformat = formatter.format(date)); log.w("countdown", returnformat); }
this happens because of timezone different. making timezone gmt able gain expected result. use following code snippet.
simpledateformat formatter = new simpledateformat("hh:mm:ss"); date date = new date(milliseconds); formatter.settimezone(timezone.gettimezone("gmt")); string returnformat = formatter.format(date); //final result. system.out.println(returnformat);
Comments
Post a Comment