Advertisement
jdalbey

Time class without javadoc comments

Apr 20th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. import java.util.Date;
  2. import java.util.Calendar;
  3. import java.text.SimpleDateFormat;
  4.  
  5. /**
  6.  * Time is the current local clock time at an airport
  7.  * at which a flight arrives or departs.
  8.  * @author Some Student
  9.  */
  10. public class Time
  11. {
  12.     private Date aTime;
  13.     private SimpleDateFormat df = new SimpleDateFormat("HHmm");
  14.    
  15.     public Time()
  16.     {
  17.         try {
  18.         aTime = df.parse("0000");
  19.         } catch (java.text.ParseException ex)
  20.         { ex.printStackTrace();
  21.         }
  22.        
  23.     }
  24.  
  25.     public Time(String time)
  26.     {
  27.         try {
  28.         aTime = df.parse(time);
  29.         } catch (java.text.ParseException ex)
  30.         { ex.printStackTrace();
  31.         }
  32.     }
  33.  
  34.     public int subtract(Time other)
  35.     {
  36.         Calendar cal = Calendar.getInstance();
  37.         cal.setTime(aTime);
  38.         long ms = cal.getTimeInMillis();
  39.         cal.setTime(other.aTime);
  40.         ms -= cal.getTimeInMillis();
  41.         final long msPerMin = 1000L * 60L;
  42.         return (int) (ms / msPerMin);        
  43.     }
  44.  
  45.     public void addOne()
  46.     {
  47.         Calendar cal = Calendar.getInstance();
  48.         cal.setTime(aTime);
  49.         cal.add(Calendar.MINUTE,1);
  50.         aTime = cal.getTime();      
  51.     }
  52.  
  53.     public String toString()
  54.     {
  55.         return df.format(aTime);
  56.     }
  57.    
  58.     public int compareTo(Time other)
  59.     {
  60.         return aTime.compareTo(other.aTime);
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement