Solved

Adding a time to current date...

Posted on 2003-10-29
7
470 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

726 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