?
Solved

Adding a time to current date...

Posted on 2003-10-29
7
Medium Priority
?
473 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 320 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Independent Software Vendors: 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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

650 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