开发者

Continous decreasing a time in java in format HH:MM:SS

开发者 https://www.devze.com 2023-02-01 16:34 出处:网络
I have a to implement a bidding system in a project and on the server, in the auction table, i have a field for start time and end time.

I have a to implement a bidding system in a project and on the server, in the auction table, i have a field for start time and end time.

What I am doing is that when a user searches, of course he sees only item whose auction is still open

When the user click on the item, he is bought to a page where he can bid. There I get the interval between the start time and the end time and then i start decreasing the time开发者_StackOverflow中文版.

is there any existing code that can be used to decrease a time whose format is in

hh:mm:ss in java

??

Edit:there was an error in the questiom, the time to decrease in the time between the end time and the current time


You don't need to decrease the time. You need to show how much time is remaining. i.e. the interval between now and an end time. Are you using a Java client? Or do you need the code to be in Java script?

EDIT: Say the bid ends at 12:00:00 and the current time is 11:59:20 (40 second to go) You would calculate the following.

long endBidTime = .... // today at 12:00:00
long currentTime = System.currentTimeMillis();
long remaining = currentTime - endBidTime;
long hours = remaining / 3600000;
long mins = remaining / 60000 % 60;
long seconds = remaining / 1000 % 60;
String remainingText = "%02d:%02d:%02d".format(hours,mins,seconds);

You are always calculating the time against a reference time so there is no need to decrement any thing as time progresses in currentTimeMillis() naturally.


I think i will proceed using the following way because i don't want to rely on client time:

-> get the required auction from the server -> using server time, calculate the remaining time (i.e. Current Server Time- auction end time)

-> Send remaining time to client -> at client decrease until 0 Second

0

精彩评论

暂无评论...
验证码 换一张
取 消