Solved

Calendar increment by certain milliseconds

Posted on 2009-03-30
4
369 Views
Last Modified: 2013-11-24
Not sure what I am doing wrong in following snippet.

I am trying to do to set the time info on the Calendar object from endTime variable. Only time info (hour, minutes and seconds).  And the data will stay as is. (date, month, year).

Until this point it is fine, I get the desired result. but then I need to increment the calendar for certain milliseconds also. So I do it using last line. but that makes the calendar with some year 1939.

Calendar endTime // equivalent to 8:30am Jan 1, 1900
int StCl = 10;   // equal to 10 minutes
 
Calendar maxTime = Calendar.getInstance();
Calendar maxTimeFullInfo = (Calendar)maxTime.clone();
maxTimeFullInfo.set(Calendar.HOUR, endTime.get(Calendar.HOUR));
maxTimeFullInfo.set(Calendar.MINUTE, endTime.get(Calendar.MINUTE));
maxTimeFullInfo.set(Calendar.SECOND, endTime.get(Calendar.SECOND));
maxTimeFullInfo.set(Calendar.MILLISECOND, 0);
maxTimeFullInfo.set(Calendar.AM_PM, endTime.get(Calendar.AM_PM));
maxTimeFullInfo.setTimeInMillis(maxTimeFullInfo.getTimeInMillis() +stCl*60000);

Open in new window

0
Comment
Question by:fix2009
  • 2
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24025719
use the add() method to add time to it

0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 24025724
0
 
LVL 2

Accepted Solution

by:
mhaq_java earned 300 total points
ID: 24025943
Well I doesn't see any problem in your code it works fine
but still you can use add method like this


     Calendar cal=(Calendar)endTime.clone();
     cal.add(Calendar.MILLISECOND,(10*60000));
0
 

Author Comment

by:fix2009
ID: 24065770
Yeah u r right mhaq! The code worked as is. I am not sure how it was coming up as 1939 that day. I think I didnt make any changes.

BTW -  add method is (int, int)  I cannot add a long in milliseconds.  
0

Featured Post

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.

Question has a verified solution.

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

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

773 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