Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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
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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!