Results 1 to 3 of 3
  1. #1
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

    Question Coparing dates in java but keep getting non-static referenced error??

    I want to read in dates from a file and then compare these dates to see if they are between two dates which will be asked from the end user. However, when I run my code, I keep getting this error message - "error: non-static method dateChecker(String,String) cannot be referenced from a static context
    System.out.print(dateChecker(select1,select2));

    what is wrong with my code??
    Java Code:
    import java.text.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.JOptionPane;
    
     public class Test
     {
    	 public static void main(String[]args)
    	 {
    		 String select1 = "11/01/2013", select2 = "11/02/2013";
    		System.out.print(dateChecker(select1,select2));
    	 }
     
     
     public String dateChecker(String select1, String select2) throws IOException
        {
    	    String aLine, result="";
                  GregorianCalendar aCalendar = new GregorianCalendar();
                  String dateParts = aCalendar.get(Calendar.DATE) + "/" +
                                                 ((aCalendar.get(Calendar.MONTH)) + 1) +  "/" + 
                                                   aCalendar.get(Calendar.YEAR);
                  Date actualStartDate, actualEndDate, dateToCheck;
                  DateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
    
                  
                  
                 FileReader getDates = new FileReader("PaidToDate.txt");
                 Scanner input = new Scanner(getDates);
                 while(input.hasNext()){
    	             aLine = input.nextLine();
    	             String[] elements = aLine.split(",");
    	             
                 
                 
                 
                 
    	  dateToCheck       =  (Date) dateFormatter.parse(elements[2]);
    	  actualStartDate = (Date) dateFormatter.parse(select1);
    	  actualEndDate   = (Date) dateFormatter.parse(select2);
    	  if ((dateToCheck.compareTo(actualStartDate) >= 0) && 
                           (dateToCheck.compareTo(actualEndDate) <= 0))
    	     result += "data should be considered";  
    	   else
    	   	result += "no";
         }
    	}
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Coparing dates in java but keep getting non-static referenced error??

    Like the error says: the dateChecker method is non-static so you must create an instance of the Test class and call the method on that instance. Just like you would a method of the String class.

  3. #3
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    101
    Rep Power
    0

    Default Re: Coparing dates in java but keep getting non-static referenced error??

    try this for your static method error
    Java Code:
    import java.text.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.JOptionPane;
     
     public class Test
     {
             String select1 = "11/01/2013", select2 = "11/02/2013";
             //Test t;
         public static void main(String[]args)
         {
             new Test();
             //dc();
         }
      
      public Test(){
          try{
            System.out.print(dateChecker(select1,select2));
          }
          catch(Exception e){
              
          }
      }
     public String dateChecker(String select1, String select2) throws IOException
        {
            String aLine, result="";
                  GregorianCalendar aCalendar = new GregorianCalendar();
                  String dateParts = aCalendar.get(Calendar.DATE) + "/" +
                                                 ((aCalendar.get(Calendar.MONTH)) + 1) +  "/" + 
                                                   aCalendar.get(Calendar.YEAR);
                  Date actualStartDate, actualEndDate, dateToCheck;
                  DateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
     
                   
                   
                 FileReader getDates = new FileReader("PaidToDate.txt");
                 Scanner input = new Scanner(getDates);
                 while(input.hasNext()){
                     aLine = input.nextLine();
                     String[] elements = aLine.split(",");
                      
                  
                  
                  
                  
          dateToCheck       =  (Date) dateFormatter.parse(elements[2]);
          actualStartDate = (Date) dateFormatter.parse(select1);
          actualEndDate   = (Date) dateFormatter.parse(select2);
          if ((dateToCheck.compareTo(actualStartDate) >= 0) && 
                           (dateToCheck.compareTo(actualEndDate) <= 0))
             result += "data should be considered";  
           else
            result += "no";
         }
        }
    }

Similar Threads

  1. Replies: 2
    Last Post: 04-10-2013, 12:46 AM
  2. Replies: 9
    Last Post: 06-10-2011, 12:40 AM
  3. Replies: 3
    Last Post: 04-11-2009, 09:14 PM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •