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?
Java

Avatar of undefined
Last Comment
rob22888

8/22/2022 - Mon
ksivananth

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 ) ;
ASKER
rob22888

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??
ksivananth

can u post the code?

also make sure cal is an instance of Calendar!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER
rob22888

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();
    }
}
ASKER
rob22888

sorry, ignore

int calculation = (theday + themonth + theyear);
String result = Integer.toString(calculation);
ASKER
rob22888

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ksivananth

>>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
ksivananth

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
rob22888

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???
ksivananth

>>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!
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER
rob22888

Well no because I am trying to say that the date is 1st January 2000, which should be day number 1 of the year?
ksivananth

then you have to give the month as 0!
ASKER
rob22888

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.