i wandted to conver a long value to formatted time.
The code i used:
long startTimeInSeconds = 60*60*1000*sH + 60*1000*sM + sS*1000 + sL;
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss,SSS");
Date start= new Date(SU.startTime);
StartTime = dateFormat.format(start);
Here sH sM .. are the hours minutes .. from a string, the format was this:"hh:mm:ss,SSS".
SU.startTime is the startTimeInSeconds.
StartTime is a String.
When i display the StartTime it not gives me the correct time.
For example when the startTimeInSeconds is 2510823, it has to be 0:41:50,823 but i get 1:41:50,823.
I dont know what i did wrong, if i display the hours minutes… like this:
int hours = (int) (SU.startTime / (60 * 60 * 1000));
Then i get the correct values.
Does anyone know whats the solution for this problem??
The immediate problem would be fixed by specifying a time zone, as others have suggested. However, a more subtle problem is that you’re using an inappropriate type. You only have a duration, by the sounds of it – or possibly a time of day; it’s unclear. Time zones shouldn’t get involved here, if you can use the right type – after all, a “start time in seconds” doesn’t logically have a time zone related to it.
Unfortunately Java doesn’t really have the right types to cover this. You’ve got
DateandCalendar, neither of which is appropriate. I’d suggest that you use Joda Time instead, where you could use theDurationorLocalTimetype based on your requirements – then use an appropriate formatter for that type, and you won’t run into this issue.Joda Time is a much better API for date/time handling in general – I would strongly advise using it in preference to the Java API wherever you can.
EDIT: As noted in comments, you also want
HHrather thanhhin your format string, as you want the 24 hour clock in order to show midnight as 00 rather than 12.