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

Thread: Calendar date

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Calendar date

    Hey guys ... well, if you read my intro, you'll know where I'm coming from. I'm working on creating a very simple "date" ... here's the code.
    public class Date {
      private int day;
      private String month;
      private int year;
     
     
      /**
       * Creates a instance of a date and starts it on November 2, 2010
       */
     
      public Date() {
        day = 2;
        month = "November";
        year = 2010;
      }
     
      /**
       * Creates a date where the user specifies the date to start on.
       * The month must contain a string with a valid month name.
       * The day must contain an integer with values between 1-30 or 1-31 depending on the date.
       * Note that February and leap year are exceptions.
       * The year must be non negative.
       */
     
      public Date (String month, int day, int year) {
        if (month=="January"||month=="February"||month=="March"||month=="April"||month=="May"||month=="June"||month=="July"||month=="August"||month=="September"||month=="October"
              ||month=="November"||month=="December") {
        this.month = month;
        }
        else { //how to handle a bad input? 
        }
     
        this.day = day;
     
        if (year > 0) {
          this.year = year;
        }
        else { //how to handle a bad input?
        }
      }
     
     
     
     
      /**
       * @ensure that the month, day, and year are all valid inputs.
       * @return Date returns the current date in the form of a string
       */
     
     
      public String getDate() {
     
        return month + " " + day + "," + " "  + year + ".";
      }
      /**
       * @return day the day of the current date.
       */
     
      public int getDay() {
        return day;
      }
     
      /**
       * @return month the month of the current date.  The month must be capitalized and spelled correctly.
       */ 
     
      public String getMonth() {
        return month;
      }
     
      /**
       * @return year returns the year of the current date.  The year must be a non-negative number.
       */
     
      public int getYear() {
        return year;
      }
     
      public void nextWeek() {
     
        day = day + 7;
      }
     
      public void tomorrow() {
     
      }
     
    }
    What I'm having a hard time doing is figuring out how to switch months if the day is the last of the month. Would I work with that within the methods, or would that be held in the constructor?

    Thanks.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    495
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Calendar date

    Don't use == for comparing Strings or other reference types for equality of content, use the .equals(...) method instead.

    What I'm having a hard time doing is figuring out how to switch months if the day is the last of the month.
    Don't forget to cater for the last day of the year.

    db

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calendar date

    Alright, thanks for the reply. Here's a question I have regarding the setup of the constructor.
    public Date (String month, int day, int year) {
        if (month=="January"||month=="February"||month=="March"||month=="April"||month=="May"||month=="June"||month=="July"||month=="August"||month=="September"||month=="October"
              ||month=="November"||month=="December") {
        this.month = month;
        }
        else { //how to handle a bad input? 
        }
    What I want this to do is make sure that the date entered is properly spelled - otherwise it shouldn't accept it. The code currently compiles, however when I create a new date (below)

    Date d = new Date("January",2,2010)

    When I ask the "getDate()" method the code returns "Null" 2, 2010.
    I can't see what I might be doing wrong... any tips?

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    495
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Calendar date

    :headdesk:

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calendar date

    Quote Originally Posted by Darryl.Burke View Post
    :headdesk:
    Did you read my intro? Let me know when you do.
    I think I'll be moving to a different forum for help ... thanks for the ... uh ... help in your second post? Oh wait.
    Last edited by joshft91; October 26th, 2010 at 12:45 AM.

  6. #6
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    495
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Calendar date

    Doesn't make a difference which forum you go to if you don't take the advice offered and continue to compare Strings using == instead of equals(...).

    db

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,342
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: Calendar date

    Darryl is correct that you should use equals rather than == when comparing strings, this will solve your latter question. Your original question could be addressed in a number of ways. For example you could maintain a lookup table, where you can check the day of the month and move up or down a list depending upon whether that number is greater than or less than a certain day.

  8. #8
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calendar date

    Alright, I got that fixed. Everything up in the constructor works correctly now.

    However, the problem I'm having now it the "nextWeek()" method. My "tomorrow()" method works completely fine - I got that working. I know that "nextWeek()" is basically "tomorrow()" repeated 7 times - I also know that i can use a "for" loop to do that. However, I have literally no experience with the 'for' loop and I was wondering how i would go about setting up a "for" loop in the "nextWeek()" mmethod.

    Thanks.

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,342
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

  10. #10
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calendar date

    Alright, I read that but I'm still unsure how to incorporate a method into that.

    public void nextWeek() {
     
        for (int i = 0; i <7; i++) {
          tomorrow();
        }
      }

    Edit: Alright, after asking someone else, I came up with with. The code compiles fine and it appears to work ... is there anything else that i should take note?

    Thanks
    Last edited by joshft91; October 28th, 2010 at 09:32 AM.

  11. #11
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calendar date

    Little bump here. I posted this thread over on a different forums...

    Creating a test class - Java Forums

    Got any insight?

  12. #12
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: Calendar date

    Quote Originally Posted by Darryl.Burke View Post
    :headdesk:
    got a nice laugh on that. thanks. lolz.

Similar Threads

  1. 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
  2. Calendar help
    By moonieass13 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 31st, 2009, 12:43 AM
  3. calendar
    By subhvi in forum AWT / Java Swing
    Replies: 2
    Last Post: September 29th, 2009, 11:02 PM
  4. which calendar to use?
    By rptech in forum AWT / Java Swing
    Replies: 6
    Last Post: August 30th, 2009, 03:18 PM
  5. Replies: 1
    Last Post: February 28th, 2009, 09:05 PM