?
Solved

Calendar increment by certain milliseconds

Posted on 2009-03-30
4
Medium Priority
?
388 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 200 total points
ID: 24025724
0
 
LVL 2

Accepted Solution

by:
mhaq_java earned 600 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

839 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