Java Util Package Prepared by S Amudha APSWE
Java Util Package Prepared by, S. Amudha AP/SWE
Calendar 1. The abstract Calendar class provides a set of methods that allows you to convert a time in milliseconds to a number of useful components. 2. Some examples of the type of information that can be provided are: year, month, day, hour, minute, and second. 3. It is intended that subclasses of Calendar will provide the specific functionality to interpret time information according to their own rules. 4. This is one aspect of the Java class library that enables you to write programs that can operate in several international environments. 5. An example of such a subclass is Gregorian. Calendar. 6. Calendar provides no public constructors. 7. Calendar defines several protected instance variables. 8. are. Fields. Set is a boolean that indicates if the time components have been set. 9. fields is an array of ints that holds the components of the time. 10. is. Set is a boolean array that indicates if a specific time component has been set. 11. time is a long that holds the current time for this object. 12. is. Time. Set is a boolean that indicates if the current time has been set.
Calendar defines the following int constants, which are used when you get or set components of the calendar:
// Demonstrate Calendar import java. util. Calendar; class Calendar. Demo{ public static void main(String args[]) { String months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug“, "Sep", "Oct", "Nov", "Dec"}; Calendar calendar = Calendar. get. Instance(); System. out. print("Date: "); System. out. print(months[calendar. get(Calendar. MONTH)]); System. out. print(" " + calendar. get(Calendar. DATE) + " "); System. out. println(calendar. get(Calendar. YEAR)); System. out. print("Time: "); System. out. print(calendar. get(Calendar. HOUR) + ": "); System. out. print(calendar. get(Calendar. MINUTE) + ": "); System. out. println(calendar. get(Calendar. SECOND)); calendar. set(Calendar. HOUR, 10); calendar. set(Calendar. MINUTE, 29); calendar. set(Calendar. SECOND, 22); System. out. print("Updated time: "); System. out. print(calendar. get(Calendar. HOUR) + ": "); System. out. print(calendar. get(Calendar. MINUTE) + ": "); System. out. println(calendar. get(Calendar. SECOND)); } }
Gregorian. Calendar 1. Gregorian. Calendar is a concrete implementation of a Calendar that implements the normal Gregorian calendar with which you are familiar. 2. The get. Instance( ) method of Calendar returns a Gregorian. Calendar initialized with the current date and time in the default locale and time zone. 3. Gregorian. Calendar defines two fields: AD and BC. 4. These represent the two eras defined by the Gregorian calendar. 5. There also several constructors for Gregorian. Calendar objects. 6. The default, Gregorian. Calendar( ), initializes the object with the current date and time in the default locale and time zone. 7. Three more constructors offer increasing levels of specificity: Gregorian. Calendar(int year, int month, int day. Of. Month) Gregorian. Calendar(int year, int month, int day. Of. Month, int hours, int minutes, int seconds) All three versions set the day, month, and year. Here, year specifies the number of years that have elapsed since 1900. The month is specified by month, with zero indicating January. The day of the month is specified by day. Of. Month. The first version sets the time to midnight. The second version also sets the hours and the minutes. The third version adds seconds.
You can also construct a Gregorian. Calendar object by specifying either the locale and/or time zone. The following constructors create objects initialized with the current date and time using the specified time zone and/or locale: Gregorian. Calendar(Locale locale) Gregorian. Calendar(Time. Zone time. Zone, Locale locale) Gregorian. Calendar provides an implementation of all the abstract methods in Calendar. It also provides some additional methods. Perhaps the most interesting is is. Leap. Year( ), which tests if the year is a leap year. Its form is boolean is. Leap. Year(int year) This method returns true if year is a leap year and false otherwise.
import java. util. *; class Gregorian. Calendar. Demo{ public static void main(String args[]) { String months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; int year; // Create a Gregorian calendar initialized // with the current date and time in the // default locale and timezone. Gregorian. Calendar gcalendar = new Gregorian. Calendar(); // Display current time and date information. System. out. print("Date: "); System. out. print(months[gcalendar. get(Calendar. MONTH)]); System. out. print(" " + gcalendar. get(Calendar. DATE) + " "); System. out. println(year = gcalendar. get(Calendar. YEAR)); System. out. print("Time: "); System. out. print(gcalendar. get(Calendar. HOUR) + ": "); System. out. print(gcalendar. get(Calendar. MINUTE) + ": "); System. out. println(gcalendar. get(Calendar. SECOND)); // Test if the current year is a leap year if(gcalendar. is. Leap. Year(year)) { System. out. println("The current year is a leap year"); } else { System. out. println("The current year is not a leap year"); } } }
Java Date Class • Java provides the Date class available in java. util package, this class encapsulates the current date and time. Constructors • Date( ) – Default • Date(long millisec) - accepts one argument that equals the number of milliseconds
Getting Current Date & Time import java. util. Date; public class Date. Demo { public static void main(String args[]) { Date date = new Date(); System. out. println(date. to. String()); } } Output: Mon May 04 09: 51: 52 CDT 2009
Example 2 import java. util. *; public class Current. Date{ public static void main(String[] args){ Calendar cal = new Gregorian. Calendar(); int month = cal. get(Calendar. MONTH); int year = cal. get(Calendar. YEAR); int day = cal. get(Calendar. DAY_OF_MONTH); System. out. println("Current date : " + day + "/" + (month + 1) + "/" + year); } }
Getting Current Time import java. util. *; public class Current. Time{ public static void main(String[] args){ Calendar calendar = new Gregorian. Calendar(); String am_pm; int hour = calendar. get(Calendar. HOUR); int minute = calendar. get(Calendar. MINUTE); int second = calendar. get(Calendar. SECOND); if(calendar. get(Calendar. AM_PM) == 0) am_pm = "AM"; else am_pm = "PM"; System. out. println("Current Time : " + hour + ": " + minute + ": " + second + " " + am_pm); } }
Date Formatting using Simple. Date. Format • Simple. Date. Format is a concrete class formatting and parsing dates in a locale-sensitive manner. • Example import java. util. *; import java. text. *; public class Date. Demo { public static void main(String args[]) { Date d. Now = new Date( ); Simple. Date. Format ft = new Simple. Date. Format ("E yyyy. MM. dd 'at' hh: mm: ss a zzz"); System. out. println("Current Date: " + ft. format(d. Now)); } } Result: • Current Date: Sun 2004. 07. 18 at 04: 14: 09 PM PDT
Time. Zone Another time-related class is Time. Zone. The Time. Zone class allows you to work with time zone offsets from Greenwich mean time (GMT), also referred to as Coordinated Universal Time (UTC). It also computes daylight saving time. Time. Zone only supplies the default constructor.
Parsing Strings into Dates import java. util. *; import java. text. *; public class Date. Demo { public static void main(String args[]) { Simple. Date. Format ft = new Simple. Date. Format ("yyyy-MM-dd"); String input = args. length == 0 ? "1818 -11 -11" : args[0]; System. out. print(input + " Parses as "); Date t; try { t = ft. parse(input); System. out. println(t); } catch (Parse. Exception e) { System. out. println("Unparseable using " + ft); } } } Result: $ java Date. Demo 1818 -11 -11 Parses as Wed Nov 11 00: 00 GMT 1818 $ java Date. Demo 2007 -12 -01 Parses as Sat Dec 01 00: 00 GMT 2007
Creating a Hash Table : Java Util • Hash Table holds the records according to the unique key value. • It stores the non-contiguous key for several values. • Hash Table is created using an algorithm (hashing function) to store the key and value regarding to the key in the hash bucket
Descriptions • hashtable<Integer, String> hash. Table = new Hash table<Integer, String>(): - Creates the instance of the Hashtable class. This code is using the type checking of the elements which will be held by the hash table. • hash. Table. put(key, in. read. Line()): - puts the values in the hash table regarding to the unique key. • Map<Integer, String> map = new Tree. Map<Integ er, String>(hash. Table): - Creates an instance of the Tree. Map for the hash table which name is passed through the constructor of the Tree. Map class. Example
Java Serialization • Object can be represented as sequence of byte which have data as well as information about states of object. • Information about states of objects includes type of object and the data types stored in the object. • Representing object into this form is known as Object Serialization.
EXAMPLE 1: Serializing an object & storing it into a file public class Student implements java. io. Serializable{ public String name; public String address; public transient int rollno; public int room. No; }
import java. io. *; public class Serialize. Example { public static void main(String[] args) { Student e = new Student(); e. name = "Kapil k Singh"; e. address = "E-247, Beta-1, Noida"; e. rollno = 513210153; e. room. No = 111; try { File. Output. Stream file. Out = new File. Output. Stream("student. ser"); Object. Output. Stream out = new Object. Output. Stream(file. Out); out. write. Object(e); out. close(); file. Out. close(); System. out. println("Object is serialized & stored in 'student. ser'"); } catch (IOException ie) { ie. print. Stack. Trace(); } } }
Deserializing an Object import java. io. *; public class Deserialize. Example { public static void main(String[] args) { Student e = null; try { File. Input. Stream file. In = new File. Input. Stream("Student. ser"); Object. Input. Stream in = new Object. Input. Stream(file. In); e = (Student) in. read. Object(); in. close(); file. In. close(); } catch (IOException i) { i. print. Stack. Trace(); return; } catch (Class. Not. Found. Exception c) { System. out. println("Student class not found"); c. print. Stack. Trace(); return; } System. out. println("Deserialized Student. . . "); System. out. println("Name: " + e. name); System. out. println("Address: " + e. address); System. out. println("Roll no: " + e. rollno); System. out. println("Room No: " + e. room. No); } }
Assignment Questions 1) Determining the actual age from date of birth in Java 2) Determining If a Year is a Leap Year in Java 3) Determining the Day-of-Week for a Particular Date
- Slides: 25