import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import java.io.*;

// A test of date parsing and formatting.
// This uses some deprecated methods, but
// it still compiles (with a warning).


public class TestDate {


   public static void main(String[] args) {
   
      SimpleDateFormat dateFormatter;
          // This is for creating string representations of dates in
          // the format that should be used for times in HTTP headers.
      
      dateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'");
          // The string is a pattern that specifes how dates will be formatted.

      dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT"));
          // Time in HTTP headers should be specifed in GMT time.
          
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      
      System.out.println("\nThis program will read dates from the user and");
      System.out.println("re-print them in HTTP-appropriate format.  Also,");
      System.out.println("will say whether the time is in the past or the future.");
      System.out.println("Many input formats will be acceptable.  Some legal");
      System.out.println("inputs include:\n");
      System.out.println("            10/1/2004");
      System.out.println("            Oct 1, 2004 5:30");
      System.out.println("            24 September 1900, 12:00");
      System.out.println("            24 September 1900, 3:30:00 PM EDT\n");
      
      while (true) {
         System.out.print("\n\nEnter a date, press return to end:  ");
         String line;
         try {
            line = in.readLine();
         }
         catch (IOException e) {
            System.out.println("Can't read from standard input!");
            return;
         }
         if (line == null || line.trim().length() == 0)
            break;
         Date inputDate;
         try {
            inputDate = new Date(line);
         }
         catch (Exception e) {
            System.out.println("Input does not represent a legal date.");
            continue;
         }
         System.out.println("Standard format: " + inputDate);
         System.out.println("HTTP format:     " + dateFormatter.format(inputDate));
         Date now = new Date();  // Create a Date object that represents current time.
         if ( inputDate.compareTo(now) < 0 )
            System.out.println("This date is in the past.");
         else
            System.out.println("This date is in the future.");
      }
      
      System.out.println("\n\n\nPart II of the program prints out info about files");
      System.out.println("in the directory where the program was run.\n\n");
      
      File dir = new File(".");  // A File object representing the current directory.
      
      if ( ! dir.isDirectory() ) {
         System.out.println("Funny error -- didn't get current directory:\n");
         return;
      }
      
      String[] files = dir.list();  // Get list of files in directory.
      
      for (int i = 0; i < files.length; i++) {
          File f = new File(files[i]);  // One of the files in the list.
          if (f.isDirectory())
             System.out.println(files[i] + " is a directory.");
          else {
             long lastModTimeCode = f.lastModified();
             Date lastModTime = new Date(lastModTimeCode);
             System.out.println(files[i] + " has length " + f.length() +
                    " and was last modified at " +
                    dateFormatter.format(lastModTime));
          }
      }
   
   }

}
