Saturday 3 August 2013

37:Date and Properties classes

Learning Objectives
After completing this session, you will be able to:
‰  Identify java.util.Date and java.util.Properties classes
The Date Class
‰  Date class represents a precise momentin time, down to the millisecond.
‰  Dates are represented as a long type that counts the number of milliseconds since
midnight, January 1, 1970, Greenwich Mean Time.
The Date Class: Example
// Return the number of milliseconds in the Date
// as a long, using the getTime() method
Date d1 = new Date();
// timed code goes here
for (int i=0; i<1000000; i++) { int j = i;}
Date d2 = new Date();
long elapsed_time = d2.getTime() - d1.getTime();
System.out.println("That took " + elapsed_time
+ " milliseconds");
The Properties Class
‰  The Properties class represents a persistent set of properties.
‰  The properties can be saved to a stream or loaded from a stream:
‰  Typically a file
‰  Each key and its corresponding value in the property list is a string.
‰  A property list can contain another property list as its "defaults“. This second property
list is searched if the property key is not found in the original property list.
The Properties Class: Example
// set up new properties object
// from file "myProperties.txt"
FileInputStream propFile
= new FileInputStream("myProperties.txt");
Properties p
= new Properties(System.getProperties());
p.load(propFile);
// set the system properties
System.setProperties(p);
// display new properties
System.getProperties().list(System.out);
Try It Out
Problem Statement:
Write a program that illustrates the usage of java.util.Date class along with
java.util.GregorianCalendar and java.util.TimeZone classes in order to access date and time
information.
Code:
import java.util.*;
public class DateApp {
public static void main(String args[]) {
Date today = new Date();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(today);
System.out.println("Today: ");
displayDateInfo(cal);
cal.clear();
cal.set(2000, 0, 1);
System.out.println("\nNew Years Day 2000: ");
displayDateInfo(cal);
}
How It Works:
‰  The program creates a Date object and a GregorianCalendar object using the default
Date() and GregorianCalendar() constructors.
‰  The Date object is assigned to the today variable, and the GregorianCalendar object is
assigned to the cal variable.
‰  The cal variable is updated with the current date by invoking its setTime() method with
the Date object stored in today.
‰  The displayDateInfo() method is then invoked to display date and time information
about the cal variable.
‰  The clear() method of the Calendar class is invoked to reset the date of the
GregorianCalendar object stored in cal.
‰  The set() method is used to set its date to New Year's 2000. There are several
versions of the set() method, each of which takes a different set of parameters.
‰  The version used in DateApp takes the year, month, and date as parameters.
‰  Note that the month value ranges from zero to 12, where the year and date values
begin at one.
‰  The displayDateInfo() method is invoked again to display information about the new
calendar date.
‰  The displayDateInfo() method creates the days, months, and am_pm arrays to define
string values corresponding to the days of the week, months of the year, and a.m./p.m.
‰  It then prints a line corresponding to date and time values.
‰  These values are retrieved using the get() method of the Calendar class and the
Calendar constants corresponding to date and time values.
‰  The getTimeZone() method of Calendar is invoked to retrieve the local TimeZone
object.
‰  The getID() method of the TimeZone class is used to retrieve the local time zone ID
string.
Tips and Tricks:
Instead of using Date class, what is the alternate way to get the current time?
Solution:
‰  To get the current time use System.currentTimeMillis().
‰  However, the catch is that the time is provided in milliseconds.
‰  The value corresponds to the difference between the current time and midnight,
January 1, 1970 UTC.
‰  This method is useful for program timing studies.
Summary
‰  The classes you need to understand are java.util.Date, java.util.Calendar,
java.text.DateFormat, java.text.NumberFormat, and java.util.Locale.
‰  Most of the Date class’s methods have been deprecated.
‰  A Date is stored as a long, the number of milliseconds since January 1, 1970.
‰  Date objects are go-betweens the Calendar and Locale classes.
‰  The Calendar provides a powerful set ofmethods to manipulate dates, performing
tasks such as getting days of the week, or adding some number of months or years to
a date.
‰  The DateFormat.format() method is used to create Strings containing properly
formatted dates.
‰  The Locale class is used in conjunction with DateFormat and NumberFormat.
Test Your Understanding
1.Properties class is a subclass of the Hashtable class. Do you agree with this statement?
2.Which methods do you use to add and retrieve data in a Properties object?

No comments:

Post a Comment