• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Subtracting Time (Date) in JSP

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to calculate the difference in two times. I've created two calendars, one with the current time and one set to a specific time. I need to subtract them and calculate the difference in Months, days, hours, minutes, and seconds.
I've been searching the web for a week now and I can't seem to find anything.
Someone help please!!!
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could you simple convert to millisecs the date and make a simple operation like
long dif = date1.getTime() - date2.getTime()
dif is the difference on milliseconds
 
Philip Cheshire
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the error I get when I try that:
/tomcat/work/Standalone/#########_jsp.java:433: operator - cannot be applied to java.util.Date,java.util.Date
long dif = AuctionCalendar.getTime() - calendar.getTime();
 
Sheriff
Posts: 67694
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because the getTime() method of Calendar returns a Date, whose getTime() method returns the long value in msecs.
So in order to convert a calendar to its msec equivalent you need to use something along the lines of:

hth,
bear
P.S. For future posts: this is a general Java question and should have been posted in the Java (Beginner) forum; just because you are trying to do this in a JSP doesn't make it a JSP question.
 
Philip Cheshire
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Hernando, after a little bit of tweaking to my application, it works great!!
Mucho Gracias
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic