Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Java Util Package Prepared by, S.Amudha AP/SWE

2 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 GregorianCalendar. 6.Calendar provides no public constructors. 7.Calendar defines several protected instance variables. 8.areFieldsSet 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.isSet 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.isTimeSet is a boolean that indicates if the current time has been set.

3 Calendar defines the following int constants, which are used when you get or set components of the calendar:

4

5

6

7 // Demonstrate Calendar import java.util.Calendar; class CalendarDemo{ public static void main(String args[]) { String months[] = {"Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug“, "Sep", "Oct", "Nov", "Dec"}; Calendar calendar = Calendar.getInstance(); 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)); } }

8 GregorianCalendar 1.GregorianCalendar is a concrete implementation of a Calendar that implements the normal Gregorian calendar with which you are familiar. 2.The getInstance( ) method of Calendar returns a GregorianCalendar initialized with the current date and time in the default locale and time zone. 3.GregorianCalendar defines two fields: AD and BC. 4.These represent the two eras defined by the Gregorian calendar. 5.There are also several constructors for GregorianCalendar objects. 6.The default, GregorianCalendar( ), 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: GregorianCalendar(int year, int month, int dayOfMonth) GregorianCalendar(int year, int month, int dayOfMonth, int hours, int minutes) GregorianCalendar(int year, int month, int dayOfMonth, 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 dayOfMonth. The first version sets the time to midnight. The second version also sets the hours and the minutes. The third version adds seconds.

9 You can also construct a GregorianCalendar 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: GregorianCalendar(Locale locale) GregorianCalendar(TimeZone timeZone) GregorianCalendar(TimeZone timeZone, Locale locale) GregorianCalendar provides an implementation of all the abstract methods in Calendar. It also provides some additional methods. Perhaps the most interesting is isLeapYear( ), which tests if the year is a leap year. Its form is boolean isLeapYear(int year) This method returns true if year is a leap year and false otherwise.

10 import java.util.*; class GregorianCalendarDemo{ 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. GregorianCalendar gcalendar = new GregorianCalendar(); // 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.isLeapYear(year)){ System.out.println("The current year is a leap year");} else { System.out.println("The current year is not a leap year"); } } }

11 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

12 Getting Current Date & Time import java.util.Date; public class DateDemo { public static void main(String args[]) { Date date = new Date(); System.out.println(date.toString()); } Output: Mon May 04 09:51:52 CDT 2009

13 Example2 import java.util.*; public class CurrentDate{ public static void main(String[] args){ Calendar cal = new GregorianCalendar(); 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); } }

14 Getting Current Time import java.util.*; public class CurrentTime{ public static void main(String[] args){ Calendar calendar = new GregorianCalendar(); 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); } }

15 Date Formatting using SimpleDateFormat SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. Example import java.util.*; import java.text.*; public class DateDemo { public static void main(String args[]) { Date dNow = new Date( ); SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz"); System.out.println("Current Date: " + ft.format(dNow)); } } Result: Current Date: Sun 2004.07.18 at 04:14:09 PM PDT

16 TimeZone Another time-related class is TimeZone. The TimeZone 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. TimeZone only supplies the default constructor.

17

18 Parsing Strings into Dates import java.util.*; import java.text.*; public class DateDemo { public static void main(String args[]) { SimpleDateFormat ft = new SimpleDateFormat ("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 (ParseException e) { System.out.println("Unparseable using " + ft); } } } Result: $ java DateDemo 1818-11-11 Parses as Wed Nov 11 00:00:00 GMT 1818 $ java DateDemo 2007-12-01 2007-12-01 Parses as Sat Dec 01 00:00:00 GMT 2007

19 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

20 Descriptions hashtable hashTable = new Hash table (): - 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. hashTable.put(key, in.readLine()): - puts the values in the hash table regarding to the unique key. Map map = new TreeMap (hashTable): - Creates an instance of the TreeMap for the hash table which name is passed through the constructor of the TreeMap class. Example Example

21 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.

22 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 roomNo; }

23 import java.io.*; public class SerializeExample { 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.roomNo = 111; try { FileOutputStream fileOut = new FileOutputStream("student.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(e); out.close(); fileOut.close(); System.out.println("Object is serialized & stored in 'student.ser'"); } catch (IOException ie) { ie.printStackTrace(); } } }

24 Deserializing an Object import java.io.*; public class DeserializeExample { public static void main(String[] args) { Student e = null; try { FileInputStream fileIn = new FileInputStream("Student.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); e = (Student) in.readObject(); in.close(); fileIn.close(); } catch (IOException i) { i.printStackTrace(); return; } catch (ClassNotFoundException c) { System.out.println("Student class not found"); c.printStackTrace(); 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.roomNo); } }

25 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


Download ppt "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."

Similar presentations


Ads by Google