Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out

White Paper - Java Input Output Package

VIEWS: 7 PAGES: 5

									Spring 2003

California State Polytechnic University, Pomona Computer Information Systems Department CIS 424 - Advanced Java Programming

Dr. Stumpf

White Paper – Advanced Java Dates
The additions are added to this white paper. They are all in blue font. At first glance, the date class seems simple and complete. This is particularly true when one uses the text package to format the date output. However, when one tries to get the components of a date such as the month or hour, do date arithmentice, or to instantiate it with other than today’s date, life becomes more complicated. This is because all this behavior was placed in another class called GregorianCalendar. The Date Class The Date class is in the package “java.util.” Using the Date class is very simple as long as the user needs are simple. To create a new date with today’s date one just uses the ordinary default constructor: Date todaysDate = new Date(); The only other constructor that is really available 1 : Date date1 = new Date(long fastTime); The class definition for the date contains only one instance variable. It is fastTime. For example, January 1, 2003 has the value of “1041408000000”. It represents the number of milliseconds since January 1, 1970. Representing dates prior to 1970 are possible as the instance variable is then represented as a negative number. The accessors for fastTime are: void setTime(long fastTime) long getTime() Thus if one wishes to calculate using milliseconds, they may. Such calculations as the number of days between two dates are possible by subtracting the fast times of the two dates. All one needs to do is divide by 86,400,000 (24 x 60 x 60 x 1000) to get the number of elapsed days. Warning! If one is going to be doing serious date computations, it is recommended they convert the date to a date of type GregorianCalendar. This is done later in this paper. DateFormat Class

1

Other constructors show, but they are deprecated. Deprecated means that the designers realize they should never have introduce them in the first place as they contained errors. They will be removed in a future version of Java, thus it is inadvisable to ever use them.
308ad20d-abaf-46f4-b00c-015cb8d2e8b2.doc CIS 424 program 11/28/2009 by Professor Robert Stumpf

1

Spring 2003

California State Polytechnic University, Pomona Computer Information Systems Department CIS 424 - Advanced Java Programming

Dr. Stumpf

Date Formatting The class DateFormat in package “java text” contains all the required behavior to simply print the date and / or time. An instance is created using the factory pattern. The factory pattern allows one to choose different instances depending on the request. For this reason, no default constructor is used. Instead a class method is invoked for each different format required. Some of the class behaviors are: DateFormat dateFormat = DateFormat.getDateInstance() DateFormat timeFormat = DateFormat.getTimeInstance() DateFormat dateTimeFormat = DateFormat.getDateTimeInstance() Since most business transactions might require the time at some later point, the most common class method is getDateTimeInstance. When one wishes to display the date, use something like: java.util.Date date = new java.util.Date(); java.text.DateFormat dateTimeFormat = java.text.DateFormat.getDateTimeInstance(); System.out.println(dateTimeFormat.format(date)); When one runs this an answer will appear as a string with the value similar to: “Jan 1, 2003 4:48:07 PM.” Date Parsing Another useful method besides format is “parse.” It simply converts a string date to a date. For example: java.util.Date date = null; try { String stringDate = "Jan 1, 2002 9:52:30 PM"; java.text.DateFormat dateTimeFormat = java.text.DateFormat.getDateTimeInstance(); date = dateTimeFormat.parse(stringDate); } catch (java.text.ParseException exception) { System.out.println(“Date Entered is invalid”); }

308ad20d-abaf-46f4-b00c-015cb8d2e8b2.doc CIS 424 program 11/28/2009 by Professor Robert Stumpf

2

Spring 2003

California State Polytechnic University, Pomona Computer Information Systems Department CIS 424 - Advanced Java Programming

Dr. Stumpf

System.out.println(date); It will throw a ParseException if the string cannot be converted. This solves the problem of how to convert data input from the keyboard as all one needs to do is enter a string value. If one wishes to obtain a date other than today’s date, the parse technique is the best way to do it. This avoids having to learn the GregorianCalendar class. The real benefit from using the date format class in the text package is that it is also country and locale sensitive. That is, if one is in France, the date will be shown in French. To test this feature, set the locale in your control panel before opening Eclipse and run this code. The Calendar Class This is an abstract class that mainly stores many constants for use in the GregorianCalendar methods. Calendar.YEAR Calendar.MONTH Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY Calendar.MINUTE Calendar.SECOND Calendar.MILLISECOND Calendar.JANUARY Calendar.FEBRUARY etc. The Gregorian Calendar Class Gregorian Calendar Constructors The GregorianCalendar class is also in the package java.util. The simplest constructor is: GregorianCalendar date = new GregorianCalendar(); This constructor returns today’s date. Note there is no constructor that accepts a date. Other constructors are: GregorianCalendar date = new GregorianCalendar(2001,1, 15); This is Feb. 15, 2001 as months start at zero. Another example is: GregorianCalendar date = new GregorianCalendar(2001, 1, 15, 13, 30);
308ad20d-abaf-46f4-b00c-015cb8d2e8b2.doc CIS 424 program 11/28/2009 by Professor Robert Stumpf

3

Spring 2003

California State Polytechnic University, Pomona Computer Information Systems Department CIS 424 - Advanced Java Programming

Dr. Stumpf

This is 1:30 PM on Feb. 15, 2001 as the hour is a 24 hour clock. Still another example: GregorianCalendar date = new GregorianCalendar(2001, 1, 15, 13, 30, 45); This adds 45 seconds to above date. Gregorian Calendar Accessors If one is comparing only times and not dates, be sure that the year, month, and day of month are the same. It is also possible to set each data member of the GregorianCalendar separately. For example: gregorianDate.set(Calendar.YEAR, 2001); gregorianDate.set (Calendar.MONTH, Calendar.JANUARY); gregorianDate.set(Calendar.DAY_OF_MONTH, 01); gregorianDate.set(Calendar.HOUR_OF_DAY, 18); gregorianDate.set(Calendar.MINUTE, 30); gregorianDate.set(Calendar.SECOND, 00); gregorianDate.set(Calendar.MILLISECOND, 000); Note that 18:30:00 is really 6:30 PM. There are many constants in the Calendar class besides the ones illustrated above. Just type Calendar. And Eclipse will show the other available constants. To get the integer values from a GregorianCalendar object use the following: gregorianDate.get gregorianDate.get gregorianDate.get gregorianDate.get gregorianDate.get gregorianDate.get (Calendar.YEAR); (Calendar.MONTH); (Calendar.DAY_OF_MONTH); (Calendar.HOUR_OF_DAY); (Calendar.MINUTE); (Calendar.SECOND);

Date Conversions The GregorianCalendar class is seldom used in a class definition as it cannot be formatted. Thus one must first learn how to go from a Date to a GregorianCalendar and back. To convert from a Date to a GregorianCalendar use: Date date = new Date(); GregorianCalendar gregorianDate = new GregorianCalendar(); gregorianDate.setTime(date);
308ad20d-abaf-46f4-b00c-015cb8d2e8b2.doc CIS 424 program 11/28/2009 by Professor Robert Stumpf

4

Spring 2003

California State Polytechnic University, Pomona Computer Information Systems Department CIS 424 - Advanced Java Programming

Dr. Stumpf

To convert from a GregorianCalendar to a Date use: GregorianCalendar gregorianDate = new GregorianCalendar (); Date date = gregorianDate.getTime (); It may seem strange but both get and set time use a Date object as a return and argument. Behaviors The most important ones are the before (GregorianCalendar) and after (GregorianCalendar) methods. For example: if(gregorianDate1.before(gregorianDate2){} if(gregorianDate1.after(gregorianDate2){} if(gregorianDate1.equals(gregorianDate2){} Also there is an add and subtract, for example: gregorianDate1.add(Calendar.DAY_OF_MONTH, 3);// add 3 days gregorianDate1.add(Calendar.DAY_OF_MONTH, -3);// subtract 3 days gregorianDate1.add(Calendar. MONTH, 3); add 3 months End

308ad20d-abaf-46f4-b00c-015cb8d2e8b2.doc CIS 424 program 11/28/2009 by Professor Robert Stumpf

5


								
To top