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

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

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: 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

Java Util Package

Prepared by,S.Amudha AP/SWE

Page 2: 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

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.

Page 3: 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

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

Page 4: 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
Page 5: 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
Page 6: 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
Page 7: 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

// Demonstrate Calendarimport 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)); } }

Page 8: 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

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

Page 9: 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

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.

Page 10: 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

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 yearif(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");} } }

Page 11: 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

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

Page 12: 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

Getting Current Date & Timeimport 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

Page 13: 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

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);  }}

Page 14: 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

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);   } }

Page 15: 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

Date Formatting using SimpleDateFormat• SimpleDateFormat is a concrete class for formatting and

parsing dates in a locale-sensitive manner.• Exampleimport 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

Page 16: 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

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

Page 17: 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
Page 18: 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

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

Page 19: 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

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

Page 20: 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

Descriptions• hashtable<Integer, String> hashTable = new Hasht

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

• hashTable.put(key, in.readLine()): - puts the values in the hash table regarding to the unique key.

• Map<Integer, String> map = new TreeMap<Integer, String>(hashTable): - Creates an instance of the TreeMap for the hash table which name is passed through the constructor of the TreeMap class. Example

Page 21: 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

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.

Page 22: 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

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

Page 23: 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

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(); } } }

Page 24: 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

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); } }

Page 25: 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

Assignment Questions

1) Determining the actual age from date of birth in Java

2) Determining If a Year is a Leap Year in Java3) Determining the Day-of-Week for a Particular

Date