The date and time classes in Java as well as the Calendar class usage detailed _java

Source: Internet
Author: User
Tags local time locale time and date time in milliseconds

Introduction to Java Date and time classes
The Java date and time class is located in the Java.util package. The date-time class provides a way to get the current date and time, create date and time parameters, and calculate and compare times.
Date class

The date class is a DateTime class in Java with more than one construction method, and the following are two commonly used:
Date (): Initializes an object with the current date and time.
Date (Long Millisec): Calculates the time in milliseconds from January 01, 1970 00 O'Clock (GMT) and calculates millisec milliseconds. If the local time zone in which the Java program is run is the Beijing time zone (8 hours from GMT), date Dt1=new Date (1000), then the object DT1 is January 01, 1970 08:00 01 seconds.

Take a look at an example of a date 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 January 1, 1970 0 o'clock to now altogether:" + msec + "millisecond");
  }


Run Result:

Mon Feb 22:50:05 CST 2007
from January 1, 1970 0 o'clock to now total: 1170687005390 ms

Some of the more commonly used Date class methods:

The Date object represents the default order of time for weeks, months, days, hours, minutes, seconds, and years. You can use the SimpleDateFormat (String pattern) method if you want to modify the time display format.

For example, to output time in a different format:

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 ("yyyy year MM month DD Day E");
    System.out.println (Ma1.format (DA));
    SimpleDateFormat ma2=new SimpleDateFormat ("Beijing Time: YYYY year mm month DD Day HH when mm minute ss seconds");
    System.out.println (Ma2.format ( -1000));
  }

Run Result:

Sun 17:31:36 CST 2015
January 04, 2015 Sunday Beijing Time
Beijing time: January 01, 1970 07:59 59 seconds


Calendar class

Abstract class Calendar provides a set of methods that allow milliseconds of time to be converted into useful time components. Calendar cannot create objects directly, but you can use static method getinstance () to get Calendar objects that represent the current date, such as:

  Calendar calendar=calendar.getinstance ();


The 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 invoke information about the year, month, hour, week, and so on, you can do this 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. of which: year representative, MONTH representative of the month, HOUR representative hours, MINUTE representative points, such as:

  Calendar.get (Calendar.month);


If the return value of 0 represents the current calendar is January, if 1 is returned for February, and so on.

Some common methods defined by the 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 under the default region and time zone.

The GregorianCalendar class defines two fields: AD and BC, respectively, representing BC and A.D. Its default constructor GregorianCalendar () initializes the object with the current date and time of the default region and time zone, and can also specify a geography and time zone to establish 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, along with additional methods that are used to determine leap years:

Boolean isleapyear (int year);


If year is a leap year, the method returns True, otherwise it returns false.

Java Object class
The Object class is in the Java.lang package and is the ancestor of all Java classes, and each class in Java is extended by it.

When you define a Java class, the Object class is inherited by default if the specified parent class is not displayed. For example:

public class demo{
  //...
}

is actually shorthand for the following code:

public class Demo extends object{
  //...
}


In Java, only the base type is not an object, for example, values, characters, and Booleans are not objects, and all array types, whether an object array or a base type array, inherit from the object class.

The Object class defines some useful methods that, because they are root classes, exist in other classes and are typically overloaded or overwritten to achieve their specific functions.
Equals () method

The Equals () method in the object class is used to detect whether an object is equivalent to another object, and the syntax is:

  public boolean equals (Object obj)


For example:

Obj1.equals (OBJ2);


In Java, the basic meaning of data equivalence is that the values of two of data are equal. When comparing by Equals () and "= =", reference type data compares the reference, that is, the memory address, and the base data type compares the value.

Attention:
The Equals () method can only compare reference types and "= =" to compare reference types and base types.
When compared using the Equals () method, the class File, String, Date, and wrapper class are compared to the type and content without regard to whether the reference is the same instance.
When you compare with "= =", the data types on both sides of the symbol must be the same (except for the data types that can be automatically converted), otherwise the compilation is wrong, and the two data that is compared with the Equals method as long as it is a reference type.
Hashcode () method

hash code (HASHCODE) is a numerical value obtained by 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 () are basically not the same.

The Hashcode () method is mainly used to implement quick lookup in the collection, and can also be used for object comparison.

In Java, the rules for Hashcode are as follows:
Invoking Hashcode () on the same object during execution of the same application must return the same integer result-provided that the information compared by equals () has never been altered. As for the invocation results of the same application at different execution periods, there is no need to be consistent.
If two objects are considered equal by the Equals () method, calling Hashcode () on both objects must obtain the same integer result.
If two objects are considered unequal by the Equals () method, then calling Hashcode () on the two objects does not have to produce different integer results. However, programmers should be aware that different integers result in different objects, and it is possible to elevate the efficiency of Hashtable (which later learns a class in the set framework).

Simply put: If two objects are the same, their hashcode values must be the same, and if two objects have the same hashcode values, they are not necessarily the same. In the Java specification, it is generally stated that overriding the Equals () method should cover the Hashcode () method.
ToString () method

The ToString () method is another important method defined in the object class, which is the string representation of the objects, and 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 for the current object, but overrides in some subclasses (such as String, Date, and so on) or overrides the ToString () method in the user's custom type as needed to return more applicable information.

In addition to explicitly calling the object's ToString () method, the ToString () method is automatically invoked when a String is connected to other types of data.

The above several methods, in Java is often used, here only for a brief introduction, let you have an understanding of the object class and other classes, detailed instructions please refer to the Java API documentation.

Related Article

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.