I am trying to include this piece of code in my Java Class. But the compiler tells me that it can't find Class Calendar. I can't create a class within a class though, can I? Any advice on how I can link this in to the rest of my code? Any advice gratefully received. I feel like I'm banging my head on a brick wall with this at the moment.

{

    int tempDifference = 0;
    int difference = 0;
    Calendar earlier = Calendar.getInstance();
    Calendar later = Calendar.getInstance();
 
    if (y1.compareTo(y2) < 0)
    {
        earlier.setTime(y1);
        later.setTime(y2);
    }
    else
    {
        earlier.setTime(y2);
        later.setTime(y1);
    }
 
    while (earlier.get(Calendar.YEAR) != later.get(Calendar.YEAR))
    {
        tempDifference = 365 * (later.get(Calendar.YEAR) - earlier.get(Calendar.YEAR));
        difference += tempDifference;
 
        earlier.add(Calendar.DAY_OF_YEAR, tempDifference);
    }
 
    if (earlier.get(Calendar.DAY_OF_YEAR) != later.get(Calendar.DAY_OF_YEAR))
    {
        tempDifference = later.get(Calendar.DAY_OF_YEAR) - earlier.get(Calendar.DAY_OF_YEAR);
        difference += tempDifference;
    }
 
    return difference;

Full code here:

public class EmmaDate
{
    
       
    private final int MAXYEAR=2099;
    
    private final int MINYEAR=0;
    
    private String nameOfDay[]=
    {
        "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"                  //data
        
    };
    
     private String shortNameOfDay[]=
     {
        "Sun","Mon","Tue","Wed","Thu","Fri","Sat"
        
    };
    
     private String nameOfMonth[]=
     {
        "January","February","March","April","May","June",
        "July","August","September","October","November","December"
        
    };
    
    private String shortNameOfMonth[]=
    {
        "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
        
    };
    
    private int daysInMonth[]={31,28,31,30,31,30,31,31,30,31,30,31};
    
    private String ordinal[]=
    
    {
        "st","nd","rd","th","th","th","th","th","th","th",        
        "th","th","th","th","th","th","th","th","th","th",
        "st","nd","rd","th","th","th","th","th","th","th",
        "st"
        
    };
        private int cumDays[]= { 0, 31, 59, 90, 120, 151
                                ,181, 212, 243, 273, 304, 334};         //cumulative days
        
   /**
    * Constuctor taking 3 parameters, d (day), m (month)  and y (year in format yyyy)
    */           
   private int year,month,day;
   private int year2,month2,day2;
   
   EmmaDate(int d,int m,int y)  
    
    {
        if(this.valYear(y))
        {
            year=y;
        }
        else
        {
            year=new java.util.Date().getYear()+1900;
        }
        if(this.valMonth(m))
        {
            month=m;
        }
        else
        {
            month=new java.util.Date().getMonth()+1;
        }
        if(this.valDay(d,this.getMonth(),this.getYear()))                             
        {                                               
            day=d;
        }
        else
        {
            day=new java.util.Date().getDay();                                     
        }
    }  
   /**
    * 
    */ 
   
     EmmaDate (int d,int m)
   //overloading  i.e. assumes that if the year is not given we mean this year                                   
    {
        this(d,m,new java.util.Date().getYear()+1900);
    }
    EmmaDate(int d)
    {
        this(d,new java.util.Date().getMonth()+1,
               new java.util.Date().getYear()+1900);
    }
    EmmaDate()
    {
        this(new java.util.Date().getDay(),
             new java.util.Date().getMonth()+1,
             new java.util.Date().getYear()+1900);
    }
    
    EmmaDate (EmmaDate ed)
    {
        this(ed.day,ed.month,ed.year);
    }
    private void setYear(int y)
    {
        int y2=this.getYear();
        if(this.valYear(y))
        {
            year=y;
        }
        else
        {
            year=y2;
        }
    }
    
    public void setMonth(int m )
    {
        int m2=this.getMonth();
        if(this.valMonth(m))
        {
            month=m;
        }
        else
        {
            month=m2;
        }
    }
   
    public void setDay(int d)                                                      
    {
        int d2=this.getDay();
        if(this.valDay(d))
        {
            day=d;
        }
        
        else
        {
            day=d2;
        }
    }
    
   public int getDay()   
   {
       return day;
    }
    public int getMonth()
    {
        return month;
    }
    public int getYear()
    {
        return year;
    }

    public String toString()
    {
        String s;                                                               
        
        if(day<10)
        {
            s="0"+day;
        }
        else
        {
            s=""+day;
        }
       if(month<10)
        {
            s=s+" "+"0"+month;
        }
        else
        {
            s=s+" "+month;
        }
      
        
        if (year<10)
        
        {
            s=s+"0"+" "+year;                                                             
                                                                                    
        }
        
        else
        
        {
            s=s+" "+year;
            
        }
        
       
        return s;
    }
      
    public boolean valDay(int d) 
                                                
    {
        if(d>=1&&d<31)
            return true;
        else
            return false;
    }
    
    public boolean valDay (int d, int m, int y)
    
    {
        if(!valMonth(m))
            return false;
                    
        int dim=daysInMonth[m-1]; 
        if (leapYear(y)&&m==2)
            dim=29;
                 
        if (d>=1&&d<=dim)
        
            return true;
        
        else
        
            return false;
        
    }

    public boolean valMonth(int m)
    {
        if(m>=1&&m<12)
            return true;
        else
            return false;
    }

    public boolean leapYear(int y)
    {
        
        if(y%400==0)
        
            return true;
            
        else
            
            if(y%100==0)
        
                return false;
        
            else
            
                if(y%4==0)
        
                    return true;
            
                    
                else
        
                    return false;
    
}
    public boolean valYear(int y)
    {
        if (y>9999) 
            return false;
        else
            return true;
    }
    public boolean leapYear()
    
    {
    
    return this.leapYear(this.getYear());
    
    }    
 
  
   //public int daysDiff (int d1, int m1, int y1, int d2, int m2, int y2)
   
   {

    int tempDifference = 0;
    int difference = 0;
    Calendar earlier = Calendar.getInstance();
    Calendar later = Calendar.getInstance();
 
    if (y1.compareTo(y2) < 0)
    {
        earlier.setTime(y1);
        later.setTime(y2);
    }
    else
    {
        earlier.setTime(y2);
        later.setTime(y1);
    }
 
    while (earlier.get(Calendar.YEAR) != later.get(Calendar.YEAR))
    {
        tempDifference = 365 * (later.get(Calendar.YEAR) - earlier.get(Calendar.YEAR));
        difference += tempDifference;
 
        earlier.add(Calendar.DAY_OF_YEAR, tempDifference);
    }
 
    if (earlier.get(Calendar.DAY_OF_YEAR) != later.get(Calendar.DAY_OF_YEAR))
    {
        tempDifference = later.get(Calendar.DAY_OF_YEAR) - earlier.get(Calendar.DAY_OF_YEAR);
        difference += tempDifference;
    }
 
    return difference;
    
    //public String dayName (int day, int month, int year)
   
}
}

Did you import java.util.Calendar; ?

import statements go before anything else in the .java file (except a package statement). If you want to use API classes like Calendar in your code you must either import them first, or use their fully qualified name (ie java.util.Calendar ) every time.

learn a bit about Java and you'd know the answer to that.
I suggest you reread your entire courseware and the Java tutorial from Sun's website, and actually study what's there.

In the class or the applet?

Looking on your code "EmmaDate" need it. Not sure about applet as there is no code

EDIT: LOL 3 replies in same time

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.