Solved

Calendar increment by certain milliseconds

Posted on 2009-03-30
4
366 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

Suggested Solutions

Title # Comments Views Activity
Java / Linux and Regular Expressions 11 71
JavaScript/Java - Changing an image background color 4 56
Domino Website - Redirection 12 54
Eclipse with gitlab 1 15
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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:

863 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

18 Experts available now in Live!

Get 1:1 Help Now