Solved

Adding a time to current date...

Posted on 2003-10-29
7
467 Views
Last Modified: 2010-08-05
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
Comment
Question by:aris_datuin
  • 3
  • 2
  • 2
7 Comments
 
LVL 24

Accepted Solution

by:
sciuriware earned 80 total points
ID: 9640561
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
 
LVL 35

Expert Comment

by:TimYates
ID: 9640575
// 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
 

Author Comment

by:aris_datuin
ID: 9640634
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:aris_datuin
ID: 9640684
anyway it's ok now...  getTime() is working fine...

thanks a lot experts...! =)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9640688
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
 
LVL 35

Expert Comment

by:TimYates
ID: 9640689
:-)
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 9640776
aris_datuin, next time at least split the points: I gave you no details .....

;JOOP!
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question