Introduction to java date and time classes

The date and time classes for java are in the java.util package. Use the methods provided by the datetime class,Can get the current date and time,Create date and time parameters,Calculate and compare time.

date class

The date class is a datetime class in java.There are many construction methods.Here are two commonly used:

date ():Initializes an object with the current date and time.

date (long millisec):Calculate time in milliseconds from 00:00 (Greenwich Mean Time) on January 01, 1970,Calculate millisec milliseconds. If the local time zone in which the java program is running is the Beijing time zone (a difference of 8 hours from Greenwich Mean Time) and date dt1=new date (1000) ;, then the object dt1 is January 01, 1970 08:00:01

Consider an example showing the 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);//Display time and date
    long msec=da.gettime ();
    system.out.println ("From 00:00 on January 1, 1970 to the present:" + msec+ "milliseconds");

operation result:

mon feb 05 22:50:05 cst 2007
From 00:00 on January 1, 1970 to the present:1170687005390 milliseconds

Some more commonly used date class methods:

The default order in which date objects 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, to 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.out.println (da);
    simpledateformat ma1=new simpledateformat ("mm month dd yyyy year e Beijing time");
    system.out.println (ma1.format (da));
    simpledateformat ma2=new simpledateformat ("Beijing time:yyyy year mm month dd day hh hour mm minute ss second");
    system.out.println (ma2.format (-1000));

operation result:

sun jan 04 17:31:36 cst 2015
Sunday January 04, 2015 Beijing Time
Beijing Time:January 01, 1970 07:59:59

calendar class

The abstract class calendar provides a set of methods,Allows time in milliseconds to be converted into useful time components.calendar cannot directly create objects,But you can use the static method getinstance () to get 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, int minute, int second);

To recall information about the year, month, hour, week, etc.,This can be achieved by calling:

 int get (int field);

The value of the parameter field is determined by the static constant of the calendar class.Where:year is the year, month is the month, and hour is the hour.minute represents minutes, such as:

 calendar.get (calendar.month);

If the return value is 0, the current calendar is January,If 1 is returned for February,So on and so forth.

Some common methods defined by calendar are shown in the following table:

gregoriancalendar class

gregoriancalendar is a class that implements the calendar class, which implements the Gregorian calendar.The getinstance () method of the calendar class returns a gregoriancalendar that is initialized to the current date and time in the default region and time zone.

The gregoriancalendar class defines two fields:ad and bc, which represent pre-BC and post-AD, respectively.Its default constructor, gregoriancalendar (), initializes the object with the current date and time in the default region and time zone.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 implementations of all the abstract methods in the calendar class,It also provides some additional methods,The method used to judge leap years is:

boolean isleapyear (int year);

This method returns true if year is a leap year, otherwise it returns false.

java object class

The object class is in the java.lang package and is the ancestor of all java classes.Every class in java extends from it.

If the java class is not explicitly specified when defining the java class,Then it inherits the object class by default. E.g:

public class demo {

It is actually a short form of the following code:

public class demo extends object {

In java, only primitive types are not objects,For example, numeric, character, and Boolean values ​​are not objects.All array types,Both object arrays and primitive types 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,It is usually overloaded or overwritten,Realized 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)


obj1.equals (obj2);

In Java, the basic meaning of data equivalent means that the values ​​of two data are equal.When comparing by equals () and "==",Reference type data compares references,Memory address,Basic data types compare values.


The equals () method can only compare reference types,"==" can compare reference types and basic types.

When comparing using the equals () method,For classes file, string, date, and wrapper classes,It compares type and content regardless of whether the reference is the same instance.

When using "==" for comparison,The data type on both sides of the symbol must be the same (except for data types that can be automatically converted), otherwise the compilation errorThe two data to be compared by the equals method need only be reference types.

hashcode () method

A hash code is a value obtained from an object according to a certain algorithm.The hash code is not regular.If x and y are different objects,x.hashcode () and y.hashcode () are basically not the same.

The hashcode () method is mainly used to implement fast find and other operations in the collection.Can also be used for comparison of objects.

The rules for hashcode in java are as follows:

During the same application execution,Calling hashcode () on the same object must return the same integer result-provided that the information compared by equals () has not been altered.As for the results of calls made by the same application in different execution periods,No need to be consistent.

If two objects are considered equal by the equals () method,Then calling hashcode () on both objects must get the same integer result.

If two objects are considered unequal by the equals () method,Then calling hashcode () on these two objects doesn't have to produce different integer results.However programmers should be aware thatProduces different integer results for different objects,It is possible to improve hashtable (we will learn later,(A class in the collections framework).

Simply put:if two objects are the same,Then their hashcode values ​​must be the same;If two objects have the same hashcode value, they are not necessarily the same.In the java specification,Generally, the equals () method should be overridden with the hashcode () method.

tostring () method

The tostring () method is another important method defined in the object class.Is the string representation of the object,The syntax is:

 public string tostring ()

The return value is a string type that describes information about the current object.The tostring () method implemented in the object class returns the type and memory address information of the current object.However, it has been rewritten in some subclasses (such as string, date, etc.). You can also override the tostring () method in user-defined types as needed to return more applicable information.

In addition to explicitly calling the object's tostring () method, when performing a string and other type of data connection operation,The tostring () method is called automatically.

  • Previous Android regular expression
  • Next Realize the progress bar display effect based on jQuery Bar Indicator plugin