• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

To calculate Time diffrence  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get a timestamp as a result of executing a query. EG:2006-01-09 15:36:26.
I need to compute the diffrence between the current time and this timestamp. The function should return false if the difference is more than 30 minutes.
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you need to check if the timestamp is between a certain interval: [now - 30 minutes, now + 30 minutes]. Right? Or is it [now, now + 30 minutes]?

You could do it like this:

- Do you get the timestamp as a String? Then you have to parse it into a Calendar object first. Use java.text.SimpleDateFormat to do that (see the API documentation).

- Next, create two Calendar objects, one for the lower boundary of the interval, one for the upper boundary.

- Finally, use the methods before() and after() of class Calendar to check whether the timestamp is in the interval.

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or just parse the two strings as Dates & see if they differ by more than the number of milliseconds you're interested in.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If by chance you have available the actual time in milliseconds I'd see if the difference is greater than 30*60*1000. If you just have a timestamp available in that format which you need to parse I'd go with what's already mentioned.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!