Link to home
Start Free TrialLog in
Avatar of rob22888
rob22888

asked on

Problem with java.util.calendar import

Hi,

I have used the import:

import java.util.Calendar;

in my java applet, but there appears to be some methods missing? When I try and use methods such as set() or get() i get an error saying 'cannot find symbol'.

Does anyone have any ideas?
Avatar of ksivananth
ksivananth
Flag of United States of America image

what is the method signature u used?

for e.g., it can be

cal.get( Calendar.YEAR ) ; //or any int constant defined
cal.set( Calendar.MONTH, 2 ) ;
Avatar of rob22888
rob22888

ASKER

well this is what i am trying to make work...

cal.set(Calendar.YEAR, 1990);
and
cal.get(Calendar.DAY_OF_YEAR):

but the set() and get() methods aren't being recognised??
can u post the code?

also make sure cal is an instance of Calendar!
import java.util.Calendar;
...
...
...    
private void submitbtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
        int theday = Integer.parseInt(dayfield.getText());
        int themonth = Integer.parseInt(monthfield.getText());
        int theyear = Integer.parseInt(yearfield.getText());
        cal.set(Calendar.YEAR, theyear);
        int calculation = (theday + themonth + theyear);
        String result = Integer.toString(calculation);
        answerfield.setText(result);
       
    }

and there is a cal class with:

import java.util.Calendar;
class cal {

    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
    }
}
sorry, ignore

int calculation = (theday + themonth + theyear);
String result = Integer.toString(calculation);
>>and there is a cal class with:

import java.util.Calendar;
class cal {

    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance(); //if this is what u refer in the method, u have to move it as instance variable rather inside main method!
    }
}
>>

I think the above is the problem!

and where have initialized the cal var?

>>cal.set(Calendar.YEAR, theyear);

I think, the above cal refers to ur cal class rather an instance of Calendar!

why do you have a class with cal?
ASKER CERTIFIED SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, that has sorted the problem.

I am trying to make an applet that will figure out the day number of the year, for eg. the 2nd February would be day 33 and so on, but taking into account leap years and that.... can you see any issues with the code now which isn't right?

    private void submitbtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
        Calendar cal = Calendar.getInstance();                                      
        int theday = Integer.parseInt(dayfield.getText());
        int themonth = Integer.parseInt(monthfield.getText());
        int theyear = Integer.parseInt(yearfield.getText());
        cal.set(Calendar.YEAR, theyear);
        cal.set(Calendar.MONTH, themonth);
        cal.set(Calendar.DAY_OF_MONTH, theday);
        int calculation = cal.get(Calendar.DAY_OF_YEAR);
        String result = Integer.toString(calculation);
        answerfield.setText(result);
       
    }          

When I input 1 for the day, 1 for the month and 2000 for the year I am getting the day of year as 32???
>>When I input 1 for the day, 1 for the month and 2000 for the year I am getting the day of year as 32???

I think, this is right!

jan - 31days
1 day - 1 day

so totally 32 days!
Well no because I am trying to say that the date is 1st January 2000, which should be day number 1 of the year?
then you have to give the month as 0!
ah right i see.

i have just done this to the code:

        int month = Integer.parseInt(monthfield.getText());
        int themonth = month - 1;

as a work around for this little technicality, so now when the user inputs 1st Jan, they get day number 1 as it should be :-)

Thank you for your help.