Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Calendar class question.

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Calendar class question.

    I am not sure if I am posting this under the right thread or not but here it goes. I ran into a little problem while using the Calendar class. At work our work weeks are from Monday to Sunday and I developed a report to show data for each day in the work week. So I created the method below to get the Sunday date of the week that the selected date the user selects falls in. So a user selects a Wednesday date, that date (Calendar instance) is passed into the method and the following Sunday (Calendar instance) is returned, works fine. The issue was coming that I was getting the month, for a monthly total, after this method was called and the date of the passed date was also changed to that Sunday's date and I am not sure why and/or how. Simply work around for me was to get the month before this method was called but I want to try and figure out what I am doing to cause this so I can prevent myself from doing this in the future. The code below produces the following output. Any insight or helpful links would be appreciated. Thanks

    The month before getting sundays date 8
    The month after getting sundays date 9
    public class DateClass
    {
     
        public DateClass()
        {
        }
     
    public Calendar getSundaysDate(Calendar passedDate)
        {
            Calendar newDate = passedDate;
            int day = newDate.get(Calendar.DAY_OF_WEEK);
     
            if (day != 1)
            {
                int dayIterator = day;
                while (dayIterator < 8)
                {
                    newDate.add(Calendar.DAY_OF_MONTH, 1);
                    dayIterator++;
                }
            }
            return newDate;
        }
     
        public static void main(String[] args)
        {
            SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
            Date date = new Date();
            try
            {
                date = df.parse("2012/08/28");
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
     
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
     
            DateClass dateClass = new DateClass();
            System.out.println("The month before getting sundays date    " + (cal.get(Calendar.MONTH) + 1));
            Calendar newCal = dateClass.getSundaysDate(cal);
            System.out.println("The month after getting sundays date    " + (cal.get(Calendar.MONTH) + 1));
        }
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,616
    Thanks
    25
    Thanked 321 Times in 299 Posts

    Default Re: Calendar class question.

    Wait, are you confused about why the cal variable changes? You pass it to the getSundaysDate() method and do direct changes to it with the newDate.add(Calendar.DAY_OF_MONTH,1) call.
    Simply saying Calendar newDate = passedDate; does not create a copy of passedDate, it creates a second reference to the passedDate variable. So when you make changes to newDate, those changes also occur to passedDate. I believe that cloning the object will create a new copy (with the .clone() method).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    banny7 (September 11th, 2012)

  4. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Calendar class question.

    I didn't realize that it was just creating a second reference. The clone was what I was missing thanks. Changed my code to the following.
    Calendar newDate = (Calendar) passedDate.clone();

Similar Threads

  1. Class Loader question
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: January 17th, 2012, 10:24 PM
  2. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  3. getTimeInMillis() from the Calendar class returns wrong values
    By 16mydream in forum Java Theory & Questions
    Replies: 2
    Last Post: February 16th, 2011, 12:29 PM
  4. I'm having trouble with date and calendar class
    By kiph in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 02:56 AM
  5. .class to .exe question
    By james137 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 3rd, 2009, 08:18 PM