• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

calculation between two different time

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you please explain me how to do that
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
shanofer ibra lebbe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Rob. Got it to work
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic