0

I currently have a method called tomorrow() which modifies an objects attributes which in this case are day, month, year (This ins't java.util.Date, I've made my own Date constructor.)

So if for example I had an object called date1(30,12,2013) and I ran date1.tomorrow() it would make date1 = (1,1,2014) NOTE: We are currently assuming all months have 30 days.

Now this works fine if I keep it as public void tomorrow() and simply write

date1.showDate(); // show original value
date1.tomorrow(); // add 1 to day, do some if statements if day > 30, etc, etc
date1.showDate(); // show modified value

BUT I want tomorrow() to also have the function to print the modified date as a String, obviously I need to make my method public String tomorrow() and have a return section but I'm not entirely sure what I'm returning, I've tried returning showDate but it isn't working correctly.

Any help would be greatly appreciated.

NOTE: This is what I currently have for my tomorrow() method

public void tomorrow()
{
        this.day++;
     if(this.day > 30)
        {
                this.day = 1;
                this.month++;
        }
        if(this.month > 12)
        {
                this.month = 1;
                this.year++;
        }
}
  • 1
    So you want a tomorrow method which does not modify the `Date` object? – Jack Mar 04 '13 at 17:01
  • What does `showDate` look like? – matts Mar 04 '13 at 17:02
  • 2
    Why not return a modified Date and not modify the instance (no side effects) ? You could rename your method to `getTomorrow` – Christophe Roussy Mar 04 '13 at 17:04
  • public void showDate() { System.out.print("\n\n THIS OBJECT IS STORING "); System.out.print(getDate()); System.out.print("\n\n"); } – Mario Stanicic Mar 04 '13 at 17:10
  • Christophe Roussy is absolutely right - you should return a new instance of your `Date` instead of modifying `this`. As far as `print`ing, I think you want `System.out.println(""+this.month+"/"+this.day+"/"+this.year);` NOTE: I'm assuming you want it just printed on your console. – mazaneicha Mar 04 '13 at 17:10

3 Answers3

0

Your ShowDate() must be printing only, not returning anything. So if you try to return ShowDate and your return type in tomorrow() is String, it will give you compile time error. So, what you can do is :

  1. go in your showDate() method, find what it is printing, and return that same thing that showDate() prints in your tomorrow().

  2. Print the exact same thing that showDate() is printing , from your tomorrow(). In this case you don't have to modify your tomorrow() method to return string.

  3. If you don't like any of the two options above and you want the values to be returned as String no matter what, follow the answer by PremGenError above.

  4. last but not the least, I would suggest you to return the Date object itself from tomorrow, and call the showDate() on that object.

Jimmy
  • 2,589
  • 21
  • 31
0

I am not sure whether you want to print the date or return a String. If you want to print it you can do this like this:

System.out.println(day + ......);

or, assuming showDate() prints

public void tomorrow()
{
     ....
     showDate();
 }

but if you want to return it, you can return a string, which you would get from showDate() (assuming its public/private String showDate() )

public String tomorrow()
{
     ....
     return showDate();
 }

if you don't have a method to get the date as a string, because showDate() yust prints it, I would recommend to implement that and call it here and in showDate, because if you want to change the formatting, add sth. or there is a bug in it you don't have to write/correcct it multiple times.

Leander
  • 1,322
  • 4
  • 16
  • 31
0

If your requirement is to add a day to the current date & return it as a string form a method you can modify your method as shown below:

public String getTomorrow(String today) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(simpleDateFormat.parse(today));
        calendar.add(Calendar.DATE, 1);  // Adding one day to input date
        return simpleDateFormat.format(calendar.getTime());
    }

Please find below the sample test results for better understanding:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateHelper {

    public String getTomorrow(String today) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(simpleDateFormat.parse(today));
        calendar.add(Calendar.DATE, 1);  // Adding one day to input date
        return simpleDateFormat.format(calendar.getTime());
    }

    public static void main(String[] args) throws ParseException {
        DateHelper dateHelper = new DateHelper();
        System.out.println(dateHelper.getTomorrow("28-02-2013"));
        System.out.println(dateHelper.getTomorrow("04-03-2013"));
        System.out.println(dateHelper.getTomorrow("31-03-2013"));
    }

}

It will produce the output:

01-03-2013
05-03-2013
01-04-2013
1218985
  • 7,531
  • 2
  • 25
  • 31