Solved

Adding a time to current date...

Posted on 2003-10-29
7
465 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now