• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 477
  • Last Modified:

Adding a time to current date...

May I ask how to add or subtract a time to a current date...?  May I also ask if the result could still be a date...?

Thanks in advance experts...! =)
0
aris_datuin
Asked:
aris_datuin
  • 3
  • 2
  • 2
1 Solution
 
sciuriwareCommented:
You are welcome,

if you convedrt a Date into a long (mSec) you can add as you like.
If you convert back to a Date it will probably not be the same Date (datum).
So, you can!
Examples?
;JOOP!
0
 
TimYatesCommented:
// Get a calendar
Calendar c = Calendar.getInstance() ;
// Set the time to now
c.setTimeInMillis( System.currentTimeMillis() ) ;
// Add four hours
c.add( Calendar.HOUR, 4 ) ;
// add 30 mins
c.add( Calendar.MINUTE, 30 ) ;
// get the date back
Date newDate = new Date( c.getTimeInMillis() ) ;
0
 
aris_datuinAuthor Commented:
That was fast...  
I've tried the codes Tim but I am encountering an error in setTimeInMillis...

setTimeInMillis has protected access in java.util.Calendar

May I ask how to resolve this...?

Thanks again... =)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
aris_datuinAuthor Commented:
anyway it's ok now...  getTime() is working fine...

thanks a lot experts...! =)
0
 
TimYatesCommented:
It does?

What JDK you using?  It's public in 1.4

Hmmm...

Try:

c.setTime( new Date() ) ;

instead...  that will do the same thing :-)
0
 
TimYatesCommented:
:-)
0
 
sciuriwareCommented:
aris_datuin, next time at least split the points: I gave you no details .....

;JOOP!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now