Calendar object being buggy

Hi, I'm not new to Java, but this seems like the best place for this question: I have some code dealing with parsing and reading schedules:
       Calendar now = Calendar.getInstance();
          Calendar startDateTime = (Calendar) startTime.clone();
          Calendar stopDateTime = (Calendar) stopTime.clone();
          startDateTime.set(Calendar.YEAR, now.get(Calendar.YEAR));
          startDateTime.set(Calendar.MONTH, now.get(Calendar.MONTH));
          startDateTime.set(Calendar.DATE, now.get(Calendar.DATE));
          stopDateTime.set(Calendar.YEAR, now.get(Calendar.YEAR));
          stopDateTime.set(Calendar.MONTH, now.get(Calendar.MONTH));
          stopDateTime.set(Calendar.DATE, now.get(Calendar.DATE));
          if (scheduleType == 0) //weekly
               for (int i = 0; i < schedule.length(); i = i + 3)
                    //System.out.println (TIMEFORMAT.format(startDateTime.getTime()));
                    startDateTime.set(Calendar.DAY_OF_WEEK, Integer.parseInt(schedule.substring(i, i + 1)));
                    stopDateTime.setTime(startDateTime.getTime());
                    stopDateTime.add(Calendar.DATE, Integer.parseInt(schedule.substring(i + 1, i + 3)));
                    System.out.println (TIMEFORMAT.format(startDateTime.getTime()) + " " + TIMEFORMAT.format(new Date()) + " " + TIMEFORMAT.format(stopDateTime.getTime()));
...My out put is:
2008-11-24 09:47:00.000 2008-11-10 10:26:14.981 2008-11-27 15:00:00.000
Which is not what I though I'd get (startDateTime and stopDateTime should be 2008-11-10, 2008-11-13).
However, if I uncomment the "System.out.println(TIMEFORMAT.format(startDateTime.getTime()));" right after the if statement, my out put becomes:
2008-11-10 09:47:00.000 2008-11-10 10:27:33.924 2008-11-13 15:00:00.000
Which is what I want.
This makes no sense to me, that System.out.println is apparently "changing" something. The closet I can figure is that System.out.println gives more time for something to do something. Shrugs.
I figure I can bandaid it by setting the startDateTime/stopDateTime to (Calendar) now.clone(), and then setting the specific hour, minute, second to the stopTime/startTime instead, but this is really bugging me.
Thanks for any help.

I've been working with java for 4-5 years, and I'm studying CS for my major, so I thought myself at least not new, but I suppose if the experienced say so, then I guess my self-estimation was wrong. Well, this is a good reality check.
Before anything else though, about what you said about .getTime() ("It is also setting startDateTime to the current time"). I'm under the impression that .getTime() get's the Calendar object's current time as a Date object (which as far as I've used, seems to be true) and also calculates the millisecond value. So if my Calendar is set to "January 2000", when I call .getTime() on it, do I get back a Date object set to "January 2000" (what I set it to) or "November 2008" (today's date)?
As for the program, I read in a schedule, a start_time, a stop_time (as well as the schedule_id, but that's not important) from a database, and create a schedule object. Then in one method of my Schedule object I check if the current time is in the schedule. I create startDateTime and stopDateTime objects on the fly since if the program runs overnight to the next week, a startDateTime, stopDateTime created at initialization will be wrong.
Here's a very heavily stripped down recreated version with a main and the test case I was using so you can run this.
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Schedule2
     //date formatter
     private static final SimpleDateFormat TIMEFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
     String schedule;
     Calendar startTime = Calendar.getInstance();
     public Schedule2(String schedule, Date startTime)
          this.schedule = schedule;
          this.startTime.setTime(startTime);
     public boolean nowInSchedule()
        Calendar now = Calendar.getInstance();
          Calendar startDateTime = (Calendar) startTime.clone(); //set the time values to correct values
          startDateTime.set(Calendar.YEAR, now.get(Calendar.YEAR)); //set the year/month/date values to correct ones
          startDateTime.set(Calendar.MONTH, now.get(Calendar.MONTH));
          startDateTime.set(Calendar.DATE, now.get(Calendar.DATE));
          for (int i = 0; i < schedule.length(); i = i + 3)
               //System.out.println(TIMEFORMAT.format(startDateTime.getTime()));
               startDateTime.set(Calendar.DAY_OF_WEEK, Integer.parseInt(schedule.substring(i, i + 1))); //sets current day_of_week to the one in the schedule
               System.out.println(TIMEFORMAT.format(startDateTime.getTime()));
               //when I check the startDateTime.getTime(), here is when I get the wrong date (2008-11-24 instead of the 10th)
          return false;
     public static void main(String [] args)
          Calendar a = Calendar.getInstance();
          a.set(1899, 12, 30, 10, 50, 0);
          Schedule2 sked = new Schedule2 ("203", a.getTime());
          System.out.println(sked.nowInSchedule());     
}So in my test case, I had "203" as the schedule, meaning every Monday until Thursday (first digit is Monday, second two is the number of days the schedule lasts) and the start_time as "30/12/1899 10:50:00 AM".
I expected to get back "2008-11-10" since this week's Monday falls on the second, but instead I got "2008-11-24".
I did read the Calendar API, but I suppose I didn't understand it fully. .set() updates the fields, but not the millisecond value. So, maybe .set(Calendar.DAY_OF_WEEK, ...) is not doing what I think it is doing because the millisecond value isn't computed yet, and .set(Calendar.DAY_OF_WEEK, ..) seems more complicated than just setting straight YEAR/MONTH/DATE.
I'm using Java 1.4.2 BTW.
Edited by: Yi.Liu on Nov 10, 2008 12:57 PM

Similar Messages

  • Calendar object without time

    Hi,
    I need to get a calendar object without displaying the time (hh:mm) field. Can smone help me with this??
    Thanks in advance

    Thanks for the replies..but my requirement is a
    calendar object which i need to set in one of the
    existing bean class set methods which takes in only a
    calendar object (I cant modify the method signature).
    I am pasting my code below..
    The situation is : I get data from database as a
    string read from a file which is then converted into
    a format yyyy-MM-dd using a simple date format. This
    returns a date object which i need to convert to
    calendar without showing the time fields. The prb is
    it is showing the GMT diff as default.
    public static Calendar dateString2Calendar(String s)
    throws ParseException {
    static SimpleDateFormat df = new
    SimpleDateFormat("dd-MMM-yy");
    static SimpleDateFormat df1 = new
    SimpleDateFormat("yyyy-MM-dd");
    Calendar cal = Calendar.getInstance();          
    1 = null;
         Date d2 = null;
         String S1 = null;
         d1 = df.parse(s);
         S1 = df1.format(d1);
         d2 = df1.parse(S1);     
         cal.setTime(d2);     
         return cal;          
    Req output : 2004-09-07
    Current output : 2004-09-07+05:30
    00:00 also not allowed in the output :(The Calendar object represents a particular moment in time - it does not have a format and it always has a time portion. Do not use the toString() method to display it - if you use the System.out.println() method to display it, the toString() method is being still being called. Format the display using SimpleDateFormat as suggested.

  • Help! problem with Calendar objects.

    I'm trying to figure out somebody's age based on their birthday inputed in MM/DD/YYYY format. I've created two Calendar objects; now, dob. I initialize both of them using the getInstance() method than I set the year, month, and day fields for dob to the user inputs. Then I subject the number of years between them(eg. 2005 - 1984 = 21). Then I add 21 to the year field for dob and compare that to now to see if the day has passed yet this year, and if it hasn't I would subtract 1 from 21 to get the age.
    I use the before() method to see if now is before dob, which returns the answer I want except for when the dob's month is the same as now's month.
    When is use 10/10/1984 I get the age as being 20 but it should be 21, however if I use any month before Oct. I get the correct age.
    Here's the code I'm using, any help would be greatly appreciated.Calendar calendar1;
    Calendar calendar2;
    int age;
    calendar1 = Calendar.getInstance();
    calendar2 = Calendar.getInstance();
    // the following three lines would be user input
    calendar2.set(Calendar.YEAR,1984);
    calendar2.set(Calendar.DAY_OF_MONTH,1);
    calendar2.set(Calendar.MONTH,10);
    int currentYear = calendar1.get(Calendar.YEAR);
    int birthYear = calendar2.get(Calendar.YEAR);
    age = currentYear - birthYear;
    calendar2.add(Calendar.YEAR, age);
    if(calendar1.before(calendar2))
        age -= 1;
    System.out.println(age);

    When is use 10/10/1984 I get the age as being 20 but
    it should be 21, however if I use any month before
    Oct. I get the correct age.Probably because January is month 0 according to Calendar. So when you do
    calendar2.set(Calendar.MONTH,10);you are actually setting the month to November.

  • Is there a way to add event details using a calendar object vs. those dumb field entry boxes?

    I have switched from PC to Mac over a year and a half ago, and have been plagued with the "dumb" date entry boxes in iCal and now Reminders on the mac. For one, they don't show you the DAY OF WEEK for an event, it assumes you are parked on that day, but of course reality sets in and causes constant date changes for events. Picking a date in the future would be much more helpful if Apple followed some usability norms in iCal. For instance, if you are booking a flight online, those helpful little calendar objects pop up, and further, they are smart about "end dates and times" for events, knowing it can't be earlier than the start date. Apple iCal unfortunately always thinks you are creating an ALL DAY event, and when you change the start time, it creates the end time for the next day. Not very smart. Or is it me that is not very smart for constantly hawking every little data entry field for correctness. Why, oh why dear lord has Apple not given some usability attention to iCal on the mac??? At least match the interface of the iPhone with a nice little slot machine rolling control.
    Is there an add in?
    Is there hope that Apple will ever fix its lagging Mail and iCal apps on the Mac?
    Please advise. Or just offer a kind word of consolation if there is no hope for a better interface.

    In the future, Swing related questions should be posted in the Swing forum.
    The code you posted is of little use since it uses custom classes so we can't see the behaviour of your code.
    In general if you add a component to a Container at run time then you just use
    container.revalidate()
    This will invoke the LayoutManager and repaint the components in the container based on the new layout.

  • Is there a way to prevent all objects being responsive in a responsive project? (Max W doesn't do anything?)

    Hi
    I'm making a responsive Edge animate scene,  which has a Vimeo video embedded inside, and will have images animated in front of and behind the video player for added wow actors. So far so good...
    The problem is I would like the player to remain at a fixed scale (500 x 281) ... 
    Once that's working I'd like to also control over a few images in the scene as to whether they're responsive or not.
    I've tried setting a Min and Max width to my container in which the Vimeo Embed code is being run but they don't stop the rectangle scaling at all.
    Here's my example test project:
    Dropbox - Edge02.zip
    I'm adding the final code to Wordpress using the Wordpress plugin called 'Edge Suite' (Developer version)  which is mentioned in an Adobe tutorial.
    Also for reference.
    Here's a site who have achieved exactly what I looking to do... Notice how the boatson the foreground layer are on a fixed scale and position. The embedded vimeo video a fixed scale but always centered. The mountains fully responsive position.
    Any ideas on how they've implemented this are welcome...but obviously understand if that is beyond the scope of the Adobe forum.

    Ok I see we can't post weblink... handy...
    The website I referred to that has achieved this is Kasra Design.
    You don't need to look at it to help, just point in the right direction to stop an individual object being responsive.

  • Urgent. Need help in the Calendar Object.

    Hello developers, best regards!
    Well my problem is that i'm facing a fact that i need a calendar object just like that one when u click the time down there in the taskbar, i need to embedd it in my java windows application.
    To be more clear, if u're familiar with the Visual Basic.NET environment, just the kind of calendars that i need is found there, in which it displays a small box which i can select the months, and accordingly i can select the specific date i need.
    Please i need your help so much.
    Thank you in advance.

    [url http://www.google.co.uk/search?hl=en&q=jcalendar&meta=]click

  • Printing Date with Calendar Object

    I am trying to print the current date of a calender object in a mm/dd/yyyy format.
    When I call system.out.println(now.getInstance()); and scroll through the console, it shows DAY_OF_MONTH=29, YEAR=2006, MONTH=6 for July 29, 2006. I realize months are zero based, so month is really 7. My question is, why does:
    System.out.println(now.MONTH + " " + now.DAY_OF_MONTH + " " + now.YEAR);
    print 2 5 1.
    Logic tells me it should print 6 29 2006.
    I have searched previous posts, and couldn't find one that answers my question. How do I take a Calendar object and print its current set date?

    My question is, why does:My question is: why do you care how a Calendar stores its data? If you want to print it, get a Date instance from it and run it through a SimpleDateFormat to get a String representation of the date.
    Don't mix up data and presentation.
    And to answer your question: you don't print the Calendar's date attributes, but the values of some static constants.

  • Convert a calendar object to a date object

    hi all...i use a calendar to select date and i want to convert the Calendar object returned to a date object ..how could i do that ?? i use the following lines of code but the Date object returned is not correct ..i mean it's not the one i chose from the calendar
    Date selectedDate=new Date();
    selectedDate=(calendar1.getTime());
    Any one could help....thank you

    Show us the code where you set the calendar to your desired value. Calendar1.getTime()
    should return the relevant date.

  • Error with calendar date being extracted : PSA - ODS - cubes correction

    Hello to all,
    I have an issue with incorrect calendar date being captured by BW.
    Here is the analysis:
    We have red request into the invoice cubes because of incorrect calendar date.  It has been found that the process is done via PSA -> ODS -> Cubes
    How will I do the correction?  Kindly specify on whether or not to change the status of the red QM status in the cubes.  Also with the data mart status of ODS and the request of ODS?  What status(has to be changed)? what request has to be deleted?  Thanks.

    Hi, 
    What I did:
    changed QM status to green of the Info Cubes
    deleted the green status of requests in the Info Cubes
    deleted the Data Mart status of ODS
    deleted the request of ODS
    edited manually the data into the PSA
    started the update immediately
    Then....
    I will wait if the request will be updated in ODS?
    Then
    I will check if the delta job from ODS to cube will proceed right?
    Please advise.  Thanks.

  • Date change problem in Calendar object

    Hi i have a calendar object for date representation.
    When i need to go ahead a day i use a add method that adds a day to the Calendar object, and if the date is out of range it adjustes proerly to the next montha and proper date in that next month.
    But it fails for 2 instances in a year.
    From May to June and From October to November.
    This i think is happenning because of the Daylight saving times in the May and October.
    I try to go from May to June but it never does on it's own !! Do i have to explicitly check that for those 2 months or there is any other method ?
    How can i get over it ??
    Please help me with this.
    Shailendra

    Hi
    There may be some other reason for it, coz Day light savings starts in last week of March, not in May.
    And day light savings ends in Last week of October
    Suresh

  • How to I get rid of the message your contacts or calendar is being synced with Mobile Me over the air

    I keep getting the message that my Contacts or my Calendar is being synced with Mobile Me over the air even though Mobile Me no longer exists on my computer or as an Apple product
    I have downloaded and am running the latest version of iTtunes running on a Win8 Pro 64bit machine
    Ernie

    It's because you must have sent some information like you must have clicked on Submit button to perform any query. So when you will click on back button it will again send this info.
    So, right click on Back button and choose 2nd site from the top of the list to go to previous page.
    ''<hr>Note: If anyone's reply has solved your problem, then please mark that reply as "Solved It" to the right of that reply after logging in your account. It will help us to concentrate on new questions.''

  • How to check whether calendar is being used in any of the sap application

    Hello All,
    We are having few factory calendars. However some of the calendar having valid to year 2010. Can you please let me know how to check whether these calendar is being used in any of the sap application/module
    kindly reply as early as possible
    Regards
    Girish

    Hi RKS
    yesterday BLOG by our SUMMER WANG helps every SRM consultant to debug the PO error
    /people/summer.wang/blog/2010/05/11/trouble-shooting-of-srm-po-transfer-error
    Good work Summer wang !!
    Muthu

  • Calendar object for the next week day

    I have a calendar object,
    How can I get the calendar object the comes on the next Tuesday or for that matter any week day.
    For example I have the calendar object for today ie. Monday, 7th Aug 2006. How can I get the calendar object for the first saturday after today.

    What you do is calculate how many days ahead it is.
    This would be the target day, less today's day
    reduced to modulo 7. Add 7 to guarantee the %
    operator works correctly e.g.
    cal.add(Calendar.DAY_OF_YEAR,(7 + Calendar.SATURDAY -
    cal.get(DAY_OF_WEEK)) % 7);
    Requires that the numeric values associated with the days of the week are ordered and one apart. Even thought this is the case, the API does not say this is the case so it should not be assumed.

  • SAP System is not the original system of the object being edited

    Hi All,
    We recently built a new system which is Copy of another system.We decomission our old system.Now when ABAPer is trying to edit Z/Y programs in se38 they are getting message "SAP System is not the original system of the object being edited".We already completed all post process after system copy.Client is open and status in Golbal modifiable in se06.
    How we can resolve this situation.

    Hello Guys,
    Thks for your input.
    @Vinod,
    We rebuild new development system from the exsisting devlopment system and scrap old one.And yes thats true that new system is not the original system where object was created,its  a replica of old dev to nev dev with SID change.
    @Arjun,
    I already check TADIR table and and its pointing to new SID.
    @Ashok,
    As i said earlier,post intallation is already completed.
    Its a problem for developer who's getting some different format of screen when they editing development object ,like, when we perform manaul corretion in abap code through snote.They can see buttons to insert code line,delete line.
    Thx,Nilesh

  • Converting String to Calendar Object

    Hi,
    I nedd to Change the below String to Calendar Object.i want to show the records which are older than half an hour.any other idea which will solve my problems?
    Fri Aug 17 01:56:40 GMT+05:30 1906
    Thanks in advance.
    Regards:
    Akash.

    Look at
    java.text.SimpleDataFormat

Maybe you are looking for