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 13 of 13

Thread: Time an object is created

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

    Default Time an object is created

    Hi, I'm creating a class and want to write a method that prints the time an object was created. Is there any way to do this?

    Thanks in advance!!

    Kumalh.


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time an object is created

    Also, how do I return the object name?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Time an object is created

    the time an object was created
    Capture and save the current time in the objects constructor.
    return the object name
    Not sure what the object's "name" is.
    Do you mean the name of the class? See the getClass() method.
    Or the name of the variable that holds the reference to the object. Pass that to the constructor as a String.

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time an object is created

    Capture and save the current time in the objects constructor.
    How do I do this? And do I save it as a String?

    Not sure what the object's "name" is.
    By name I mean the name of the instance. So say you created a new Calendar object and named it 'Cal' (e.g. cal = new Calendar(4, 8, 2011) )
    So I think that is the variable that holds the reference to the object. Correct?

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Time an object is created

    Norm has answered these questions. For the time, check out the API for System, or Calendar, or Date. Or do some googling. If something you see confuses you, create an SSCCE that tests it out and post it here.

    You can't get the variable name. You can pass a String to the constructor, as Norm already said. You also might want to look into using a Map and using the String names as the keys and the Calendars the values.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time an object is created

    Ok, I'll check out Calendar and Date. Thanks.

    Also, I'm writing my own Calendar class (I know one already exists, but I'm required to write my own. So I can't use any methods from the pre-existing calendar class).

    I've written all of my constructors, but I've come across a problem.

        // Constructor for calendar given the day, month, and year.
        public Calendar(int d, int m, int y) {
            date = d;
            month = m;
            year = y;
        }

    So here is one of the constructors of the class (they are all fairly similar, just putting in default values when they aren't given). Now, obviously the date given can't exceed the number of days in that month, nor can the given month exceed the number of months in a year. How do I return an error when somebody tries to do this?

    For example, if somebody tries to create
     Calendar cal = new Calendar(5, 13, 2011);
    how do I prevent this?

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Time an object is created

    What do you want to happen? Do you want to throw an Exception? Then throw one from the constructor. Do you want to show an error message? If so, check the input before you even call the constructor. You could even do both, I suppose.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time an object is created

    Ok, so I've sorted out all of that stuff. Thank you for your help.

    I have another problem that I can't get though. I'm not sure if I should start a new post or not (I'm new - I apologise), so I'll post it here.

    One of the required methods for a calendar object is the ability to roll the calendar date forward or backward by a given number of days. So far, I have this:

        private void addOne(boolean up) {
            if(up == true)
                date = date + 1;
            else
                date = date - 1;
     
        }
     
        public void rollDays(int d) {
            while(d != 0) {
                if(d > 0){
                    addOne(true);
                            d = d - 1;  }
                else {
                    addOne(false);
                            d = d + 1;  }      
           }

    Now my problem is getting the method to recognise when to increase the month. So for example, if rolling forward 10 days from the 25th may, how do I recognise when it reaches the 31st, then start rolling again from the 1st of june.. Similarly, how do I recognise when rolling backward. Like when it hits the 1st of the month and needs to go to the last day of the previous month.

    I also have a method that finds the number of days in each month, and whether it is a leap year or not. I imagine these will need to be used, just don't know where...

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Time an object is created

    Well, how do you do this by hand or in your head? Pretend you have a friend who has never seen a calendar before. Write out instructions that he could follow to do the task, and you'll have an algorithm that should be pretty easy to translate to code.

    You seem to mean well, but people don't like crossposting:

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/47220-rolling-forward-days-calendar.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time an object is created

    Ahh sorry about the cross posting. This is my first time using forums so I'm not too familiar with all of that stuff. Like you said, I only meant well. In future I'll be sure to be more careful. Again, I apologise

  11. #11
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time an object is created

    So I've written this:

        public void rollDaysTEST(int d) {
        int count = d;
            for(int i = 0; i == d; i = i + 1){
                date = date + 1;
                    if(date > getDaysInCurrentMonth()){
                        month = month + 1;
                        date = count; }
                    else
                        count = count - 1;
            }
        }

    However, when I call it, nothing happens. The date value doesn't get increased. What am I missing?

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Time an object is created

    I'm not sure what you expect that code to do. Perhaps an SSCCE, or at least some comments, would help us understand it?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time an object is created

    Sorry about that. If my commenting makes no sense, I apologise. It's been a very, very long night.


      // Method rolls the date of the calendar forward by a given number of days
        public void rollDaysTEST(int d) {
        int count = d;
            for(int i = 0; i == d; i = i + 1){ // Loop starts at 0, and finishes when the given number of days has been added to the calendar
                date = date + 1; // Increases the date by one day each run of the for loop
                    if(date > getDaysInCurrentMonth()){ // Checks whether the date is greater than the number of days in the month
                        month = month + 1; // If the date is greater, the month is increased
                        date = count; } // The new date becomes what is left of the count. This is to add the days left when the month increases. (e.g. if 10 days is added to the 25th of may, when the month changes to June, there are                                              4 days left.
                    else
                        count = count - 1;  // Decreases the count by one
            }
        }

Similar Threads

  1. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  2. Running methods of a class that created you...
    By joestr in forum Java Theory & Questions
    Replies: 2
    Last Post: April 3rd, 2011, 01:59 PM
  3. Autoboxing and unboxing for user created objects
    By tcstcs in forum Java Theory & Questions
    Replies: 3
    Last Post: March 22nd, 2011, 07:54 AM
  4. creating a controller to allow instances to be created from keyboard
    By ss7 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 2nd, 2009, 12:30 PM
  5. [SOLVED] Program to read from created file
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 7th, 2009, 03:51 AM