Java Util package Usage Details

Source: Internet
Author: User

This chapter introduces Java's utility library java. util package. In this package, Java provides some practical methods and data structures. For example, Java provides the date (Data) class and Calendar class to generate and obtain the date and time, and provides the Random number (Random) class to generate various types of Random numbers, stack, Vector, Bitset, and Hashtable classes are also provided to represent the corresponding data structure.
Figure 1.1 shows the basic hierarchy of the java. util package. The following describes several important classes.
Using java. util. BitSet
│ Java. util. Calendar
│ Upgrade java. util. GregorianCalendar
│ Java. util. Date
│ Java. util. Dictionary
│ Define java. util. Hashtable
│ Define java. util. Properties
│ Java. util. EventObject
│ Java. util. ResourceBundle
Java. util. ListResourceBundle
│ Java. util. PropertyResourceBundle
│ Java. util. Local
│ Java. util. Observable
│ Java. util. Random
│ Java. util. StringTokenizer
│ Java. util. Vector
│ Java. util. Stack
Java. util zookeeper java. util. TimeZone
│ Define java. util. SimpleTimeZone
│ Define java. util. Enumeration
Listener interface implements java. util. EventListener
│ Define java. util. Observer
│ Define java. util. EmptyStackException
Handler exception class java. util. MissingResourceException
│ Java. util. NoSuchElementException
Using java. util. TooManyListenersException
Figure 1.1 Basic hierarchy of java. util package


1.2 Date type

Java encapsulates date and time information in the date class. You can call the corresponding method to obtain the system time or set the date and time. Many methods in the Date class are outdated after JDK1.0 is published. In section 8.3, we will introduce other classes newly added in JDK to replace the Date function.
Six constructor types are defined in the date class.
(1) public Date ()
The date and time of the created date class object are set to the date and time corresponding to the creation time.
For example, Date today = new Date (); // today is set to the Date time corresponding to the creation time.
(2) public Date (long date)
The long parameter date can be obtained by calling the static method parse (String s) in the Date class.
Example long l = Date. parse ("Mon 6 Jan 1997 ");
Date day = new Date (l );
// The time in day is Monday, January 1, January 6, 1997, and.
(3) public Date (String s)
Generates a date object by string s. The format of s is the same as that of the string parameter in method parse.
For example, Date day = new Date ("Mon 6 Jan 1997 ");
// The time in day is Monday, January 1, January 6, 1997.
(4) public Date (int year, int month, int date)
(5) public Date (int year, int month, int date, int hrs, int min)
(6) public Date (int year, int month, int date, int hrs, int min, int sec)
Create a date object based on the given parameters.
Parameter description:
The value of year is: the year to be set-1900. For example, if the year to be set is 1997, the value of year should be 97, that is, the result of 1997-1900. Therefore, the minimum year in Date can be set to 1900;
The value range of month is 0 ~ Represents, and 11 represents;
The value range of date is 1 ~ Between 31;
The value range of hrs is 0 ~ Between 23. From midnight to one o'clock P.M. the next day, hrs = 0, and from noon to hrs = 12;
The value range of min and sec is 0 ~ Between 59.
For example, Date day = new Date (, 4 );
// The time in day is: 12:00:00 AM
You can also give incorrect parameters.
The example is set to January 1, February 30, 1910. It will be interpreted as January 1, March 2.
Date day = new Date (10, 1, 30, 10, 12, 34 );
System. out. println ("Days date is:" + day );
// The printed result is: Days date is: Web Mar 02 10:13:34 GMT + 08:00 1910
Below are some common methods for Date classes.
(1) public static long UTC (int year, int month, int date, int hrs. int min, int sec)
This method uses the given parameter to calculate the UTC value. UTC is a timing system, which is slightly different from GMT (Greenwich Mean Time. The UTC timing system is based on the atomic clock, while the GTMT timing system is based on astronomical observation. The calculation usually uses the GMT timing system.
(2) public static long parse (String s)
This method converts string s to a long date. This method was used when we introduced the construction method Date (long date.
String s has a certain format, generally:
(GMT + time zone)
If the time zone is not specified, the local time zone is used.
(3) public void setMonth (int month)
(4) public int getMonth ()
These two methods are respectively set and get the month value.
The value range of the obtained month is 0 ~ 11, 0 represents January, and 11 represents December.
(5) public String toString ()
(6) public String toLocalString ()
(7) public String toGMTString ()
Converts a given date object to a string of different formats. For specific formats, see Example 8.1.
(8) public int getTimezoneOffset ()
This method is used to obtain the time zone offset of a date object.
In Example 8.1, the basic methods in the Date class described above are applied and the corresponding results are printed. Some outdated methods are used, so warnings are generated during compilation. In addition, because the time representation in this example is related to the platform, different JDK versions do not have the same processing. Therefore, the results of executing this example in different JDK versions may be slightly different.
Example 1.1 DateApp. java
Import java. lang. System;
Import java. util. Date;
Public class DateApp {
Public static void main (String args []) {
Date today = new Date ();
// The date in today is set to the date and time of the Creation Time. Assume that the creation time is January 1, March 1997.
// 17: 51: 54 seconds on the 23rd.
System. out. println ("Todays date is" + today );
// Returns a general time representation. In this example, the result is
// Todays date is Fri May 23 17:51:54 1997
System. out. println ("Todays date (Internet GMT) is :"
+ Today. toGMTString ());
// The returned result is in GMT notation. In this example, the result is
// Todays date (Internet GMT) is: 23 May 1997 09: 51: 54: GMT
System. out. println ("Todays date (Locale) is :"
+ Today. toLocaleString ());
// The returned result is a time representation of local habits. The result is
// Todays date (Locale) is: 05/23/97 17:51:54
System. out. println ("Todays year is:" + today. getYear ());
System. out. println ("Todays month is:" + (today. getMonth () + 1 ));
System. out. println ("Todays date is:" + today. getDate ());
// Call the method in the Date class to obtain the value of year, month, and day.
// The following calls different constructor methods to create Date class objects.
Date day1 = new Date );
System. out. println ("Day1s date is:" + day1 );
Date day2 = new Date ("Sat 12 Aug 1996 ");
System. out. println ("Day2s date is:" + day2 );
Long l = Date. parse ("Sat 5 Aug 1996 GMT + 0800 ");
Date day3 = new Date (l );
System. out. println ("Day3s date (GMT) is:" + day3.toGMTString ());
System. out. println ("Day3s date (Locale) is :"
+ Day3.toLocaleString ());
System. out. println ("Day3s time zone offset is :"
+ Day3.getTimezoneOffset ());
}
}

Running result (JDK1.3 is different from the original version, and the original version is JDK1.0 ):
E: javautorialj

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.