Introduction to Java date and time classes
Java's date and time classes are located in the java.util package. Using the methods provided by the date and time class, you can get the current date and time, create date and time parameters, calculate and compare time.
Date class
The Date class is a date and time class in Java, and it has many construction methods. The following are two commonly used:
Date(): Initialize an object using the current date and time.
Date(long millisec): Calculate the time in milliseconds starting from 00:00 on January 1, 1970 (Greenwich time), and calculate millisec milliseconds. If the local time zone where the Java program is running is the Beijing time zone (8 hours apart from Greenwich time), Date dt1=new Date(1000); then object dt1 is 08:00:01 on January 1, 1970.
Please see an example of displaying date and time:
import java.util.Date;public class Demo{ public static void main(String args[]){ Date da=new Date(); //Create time object System.out.println(da); //Show time and date long msec=da.getTime(); System.out.println("From 0:00 on January 1, 1970 to now: " + msec + "msec"); }}
Running results:
Mon Feb 05 22:50:05 CST 2007 From 0:00 on January 1, 1970 to now: 1170687005390 milliseconds
Some commonly used Date class methods:
The default order of the Date object to represent time is week, month, day, hour, minute, second, year. If you need to modify the format of the time display, you can use the "SimpleDateFormat(String pattern)" method.
For example, output time in different formats:
import java.util.Date;import java.text.SimpleDateFormat;public class Demo{ public static void main(String args[]){ Date da=new Date(); System.ou t.println(da); SimpleDateFormat ma1=new SimpleDateFormat("yyyyy year MM month dd day E Beijing time"); System.out.println(ma1.format(da)); SimpleDateFormat ma2=new SimpleDateFormat("Beijing time: yyyy year MM month dd day HH time mm minute ss seconds"); System.out.println(ma2.format(-1000)); }} Running results:
Sun Jan 04 17:31:36 CST 2015 January 04, 2015 Beijing time Beijing time: January 01, 1970 07:59:59:59:59:59:59:
Calendar class
The abstract class Calendar provides a set of methods that allow time in milliseconds to be converted into useful time components. Calendar cannot create objects directly, but you can use the static method getInstance() to obtain a calendar object representing the current date, such as:
Calendar calendar=Calendar.getInstance();
This object can call the following method to turn the calendar to a specified time:
void set(int year,int month,int date);void set(int year,int month,int date,int hour,int minute);void set(int year,int month,int date,int hour,in t minute, int second);
To call information about year, month, hour, week, etc., you can do it by calling the following method:
int get(int field);
Where, the value of the parameter field is determined by the static constants of the Calendar class. Among them: YEAR represents year, MONTH represents month, HOUR represents hour, and MINUTE represents minute, such as:
calendar.get(Calendar.MONTH);
If the return value is 0, it means the current calendar is January, if the return 1 represents February, and so on.
Some common methods defined by Calendar are shown in the following table:
GregorianCalendar Class
GregorianCalendar is a class that specifically implements the Calendar class, which implements the Gregorian calendar. The getInstance() method of the Calendar class returns a GregorianCalendar, which is initialized to the current date and time under the default region and time zone.
The GregorianCalendar class defines two fields: AD and BC, representing BC and post-AD, respectively. Its default constructor GregorianCalendar() initializes the object with the current date and time of the default region and time zone. In addition, you can also specify the region and time zone to create a GregorianCalendar object, for example:
GregorianCalendar(Locale locale);GregorianCalendar(TimeZone timeZone);GregorianCalendar(TimeZone timeZone,Locale locale);
The GregorianCalendar class provides the implementation of all abstract methods in the Calendar class, and also provides some additional methods, among which the method used to judge leap years is:
Boolean isLeapYear(int year);
If year is a leap year, the method returns true, otherwise false.
Java Object Class
The Object class is located in the java.lang package and is the ancestor of all Java classes, and each class in Java is extended from it.
If the parent class is not displayed when defining a Java class, the Object class is inherited by default. For example:
public class Demo{ // ...} It is actually the abbreviation of the following code:
public class Demo extends Object{ // ...}
In Java, only the basic types are not objects, such as numerical values, characters, and boolean values are not objects. All array types, whether they are object arrays or basic types arrays, are inherited from the Object class.
The Object class defines some useful methods. Since it is a root class, these methods exist in other classes. They are generally overloaded or overwritten, and implement their specific functions.
equals() method
The equals() method in the Object class is used to detect whether an object is equivalent to another object. The syntax is:
public boolean equals(Object obj)
For example:
obj1.equals(obj2);
In Java, the basic meaning of data equivalent means that the values of two data are equal. When comparing equals() and "==", the reference type data compares the reference type data, that is, the memory address, and the basic data type compares the value.
Notice:
The equals() method can only compare reference types, and "==" can compare reference types and basic types.
When comparing with the equals() method, for the class File, String, Date and wrapper classes, the type and content are compared without considering whether the reference is the same instance.
When comparing with "==", the data types on both sides of the symbol must be the same (except for data types that can be automatically converted), otherwise there will be an error in compilation, and the two data compared with the equals method can be as long as they are reference types.
hashCode() method
HashCode is a numerical value obtained from an object according to a certain algorithm, and the hash code is not regular. If x and y are different objects, x.hashCode() and y.hashCode() will basically not be the same.
The hashCode() method is mainly used to implement quick search and other operations in a collection, and can also be used for object comparison.
In Java, the provisions on hashCode are as follows:
During the execution of the same application, when hashCode() is called on the same object, the same integer result must be returned - provided that the information compared by equals() has not been changed. As for the call results obtained by the same application during different execution periods, there is no need to be consistent.
If two objects are treated as equals by the equals() method, calling hashCode() on both objects must obtain the same integer result.
If two objects are treated as unequal by the equals() method, calling hashCode() on both objects does not have to produce different integer results. However, programmers should realize that producing different integer results for different objects may improve the efficiency of hashTable (a class in the collection framework that will be learned later).
Simply put: if two objects are the same, their hashCode values must be the same; if the hashCode values of the two objects are the same, they are not necessarily the same. In the Java specification, it is generally stipulated that the equals() method should be overridden with the hashCode() method.
toString() method
The toString() method is another important method defined in the Object class. It is the string expression of the object, and the syntax is:
public String toString()
The return value is type String, which describes the information about the current object. The toString() method implemented in the Object class is to return the type and memory address information of the current object, but it is rewritten in some subclasses (such as String, Date, etc.), and can also be rewritten in user-defined types as needed. toString() method to return more applicable information.
In addition to explicitly calling the object's toString() method, the toString() method will be automatically called when connecting String and other types of data.
The above methods are often used in Java. Here are a brief introduction to let you understand Object classes and other classes. For detailed instructions, please refer to the Java API documentation.