• Post Reply Bookmark Topic Watch Topic
  • New Topic

calculation between two different time  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone

in my program i have two type of times. one is the system time which i took form the system using System.currentTimeMillis()
this time is in milliseconds

and the other one is the flight time stored by myself in the following fotmat as a string 14:30pm

the problem i got is that now i need to do a calculation between this two time to find out the time left for the flight

does anyone know how can i do this, if so please help me out. i'm struggling with this problem for more than two weeks

thank you
 
Sheriff
Posts: 21137
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a SimpleDateFormat to convert the string into a Date object. You may have to use Calendar as well to set the date itself.
 
shanofer ibra lebbe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please explain me how to do that
 
Rob Spoor
Sheriff
Posts: 21137
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
shanofer ibra lebbe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you

do you know how can i get the time difference between two values

for example i have two times one is the system time its stored as an integer if the time is 15:32:21 its stored as 153221
and the flight time stored as an integer as well
(but i have both as a string as well)

now i need to get the difference between this two times
in my program i'm using an if statement if the current system is greater than the flight time
then it should subtract the flight time from the system time and display the difference in the time format

otherwise it should say you missed the flight

can you please help me out with this
cheers
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for example i have two times one is the system time its stored as an integer if the time is 15:32:21 its stored as 153221
and the flight time stored as an integer as well


This time that you have, which is stored as an integer, is not compatible with the current (system) time from the System class. So when you say system time, what are you referring to?

now i need to get the difference between this two times
in my program i'm using an if statement if the current system is greater than the flight time
then it should subtract the flight time from the system time and display the difference in the time format

otherwise it should say you missed the flight


If all you want is to check if you missed the flight, then what you described should work. If you actually need to calculate the difference in time, then you will have to develop an algorithm for your format. Or you can take the string version, parse it with the simple data format class to a Date object to find the difference, in milliseconds.

Henry
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am creating a chatterbot and when the user asks the bot how long have we been chatting for the bot must tell the time difference. The chatterbot is working all I need to do is calculate the time. I need to get the the time when the conversation starts and I need to get the time when the user asks the question of how long have we been talking for and subtract these two times. Any ideas?

Thanks
 
Rob Spoor
Sheriff
Posts: 21137
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Call System.currentTimeMillis() (and store its result) when the chat starts, call System.currentTimeMillis() when the user wants to know the chat time, subtract the start from it and convert to seconds, minutes etc. The latter is basic algebra:
- hours = millis / (1000 * 60 * 60)
- minutes = (millis / (1000 * 60)) % 60
- seconds = (millis / 1000) % 60
- millis = millis % 1000
 
Moe Adams
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Rob. Got it to work
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!