Non static method reference?

A little side project I've been doing for fun involves subtracting the current date from a future date given by the user to return the days between them.

public int getDaysBetween(int date2)
{
    //Subtract current date from future date (date2), leaving the number of days between them
    int getDaysBetween = 0;
    Calendar myCalendar = Calendar.getInstance();
    myCalendar.get(Calendar.DAY_OF_YEAR); 
    getDaysBetween = date2-Calendar.DAY_OF_YEAR;
    return getDaysBetween;
}

The method for doing this is non-static, as the date2 int changes. However, when I try to reference it in my main class...

//Figure out a non-static reference
    int date2 = Integer.parseInt(JOptionPane.showInputDialog("Enter a day in the year ahead of today"));
    message = "Days bewteen: " + Date.getDaysBetween(date2-Calendar.DAY_OF_YEAR);
    JOptionPane.showMessageDialog(null, message);

I get the error that a non-static method cannot be referenced from a static context.

I am fairly new to Java, so it might seem easy to most of you guys, but I could use the help.

Thanks in advance!

Jon Skeet
people
quotationmark

The method for doing this is non-static, as the date2 int changes.

I think you've misunderstood the meaning of the static modifier.

Your method doesn't use any instance fields, and there's no reason to override it in subclasses, so it should be a static method.

date2 is a parameter, so each call to it can pass a different value. That doesn't depend on the instance you call the method on.

(As an aside, it's not really clear what your method is meant to achieve - are you really interested in the day of year? It's also likely that java.time or Joda Time would provide a better API for this. However, it's most important that you understand what static means... you might want to read the Java tutorial on class members.)

people

See more on this question at Stackoverflow