/**Class TimeClass calculates the present time with a precision of upto 1 second. * Time format is 24Hours. * * @version 1.1 07/12/2001 * */ // Importing necessary packages... import java.util.*; import java.lang.*; // Class TimeClass starts here... public class TimeClass { //Defining private data members... private int intSeconds = 0; //stores value of "Seconds" private int intMinutes = 0; //stores value of "Minutes" private int intHours = 0; //stores value of "Hours" private String strPresentTime = null; //stores value of Present Time as a string // Constructor 1: No arguments, assigns the present system time public TimeClass() { Calendar cal = Calendar.getInstance(); Date dt = cal.getTime(); intSeconds = dt.getSeconds(); intMinutes = dt.getMinutes(); intHours = dt.getHours(); strPresentTime = Integer.toString(intHours) + ":" + Integer.toString(intMinutes) + ":" + Integer.toString(intSeconds); } // Constructor 2: Accept user defined Hour, Minute, Second as integers public TimeClass(int hours, int minutes, int seconds) { intSeconds = seconds; intMinutes = minutes; intHours = hours; strPresentTime = Integer.toString(intHours) + ":" + Integer.toString(intMinutes) + ":" + Integer.toString(intSeconds); } // Method 1: Allow access to private strPresentTime public String getPresentTime() { return strPresentTime; } // Method 2: Allow access to private intHours public int getPresentHours() { return intHours; } // Method 3: Allow access to private _intMinutes public int getPresentMinutes() { return intMinutes; } // Method 4: Allow access to private _intSeconds public int getPresentSeconds() { return intSeconds; } // Method 5: Check the present system time in 24 hrs format and assign the values to the private members public void checkPresentTime() { Calendar cal = Calendar.getInstance(); Date dt = cal.getTime(); intSeconds = dt.getSeconds(); intMinutes = dt.getMinutes(); intHours = dt.getHours(); strPresentTime = Integer.toString(intHours) + ":" + Integer.toString(intMinutes) + ":" + Integer.toString(intSeconds); } // Method 6: Allow access to private intHours in 12 hr format public int getPresent12Hours() { int a = intHours; if (a > 12) { a = a - 12; } return a; } } //~ End: TimeClass