Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Subtracting Time (Date) in JSP

 
Philip Cheshire
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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!!!
 
Hernando
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
  • 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();
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Hernando, after a little bit of tweaking to my application, it works great!!
Mucho Gracias
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic