Java know how many (77) date and Time classes

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

The date and time classes for Java are located in the Java.util package. Using the method provided by the DateTime class, you can get the current date and time, create a date and time parameter, calculate and compare time.

Date class

The date class is a DateTime class in Java and is constructed in a number of ways, with the following 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 that runs the Java program 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 that shows the date and time:

1 Importjava.util.Date;2  Public classdemo{3      Public Static voidMain (String args[]) {4Date da=NewDate ();//Create a Time object5System.out.println (DA);//Show time and date6         LongMsec=da.gettime ();7System.out.println ("From January 1, 1970 0 o'clock to now total:" + msec + "milliseconds");8     }9}

Operation Result:

Mon Feb 22:50:05 CST 2007
From January 1, 1970 0 o'clock to present total: 1170687005390 milliseconds

Some of the more commonly used Date class methods:

method function
boolean after (date date) call Date The object contains a date that is later than the date specified by date and returns True, otherwise false.
boolean before (date date) returns True if the Date object contains dates that are earlier than the date contained in the object specified by date, otherwise false. The
Object Clone () replicates the call to the Date object.
int compareTo (date date) compares the date that the call object contains and the date that the specified object contains, or returns 0 if it is earlier than the latter, or returns a positive value if the former is equal.
long getTime () returns the current time in milliseconds from January 01, 1970 00 o'clock.
int hashcode () returns the hash value of the calling object.
void settime (long time) sets the times and dates, based on the value of hours. The time value is calculated starting January 01, 1970 00 o'clock. The
string toString () converts the called Date object to a string and returns the result.
public Static string valueOf (type variable) converts variable to a string.

The default order of time for a date object is week, month, day, hour, minute, second, year. You can use the SimpleDateFormat (String pattern) method if you want to modify the format of the time display.

For example, output time in a different format:

1 Importjava.util.Date;2 ImportJava.text.SimpleDateFormat;3  Public classdemo{4      Public Static voidMain (String args[]) {5Date da=NewDate ();6 System.out.println (DA);7SimpleDateFormat ma1=NewSimpleDateFormat ("yyyy MM month dd Day E GMT");8 System.out.println (Ma1.format (DA));9SimpleDateFormat ma2=NewSimpleDateFormat ("Beijing Time: yyyy mm month dd Day HH mm min ss sec");TenSystem.out.println (Ma2.format (-1000)); One     } A}

Operation Result:

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

Calendar class

The abstract class Calendar provides a set of methods that allow you to convert time in milliseconds to some useful time component. The calendar cannot create objects directly, but you can use the static method getinstance () to get the Calendars object representing the current date, such as:
Calendar calendar=calendar.getinstance ();
The object can call the following method to turn the calendar to a specified time:

 1  void  Set ( int  year,int   date);  2  void  Set ( int  year,int  date,int  hour,int   minute);  3  void  Set ( int  year,int  date,int  hour,int  minute,< Span style= "color: #0000ff;" >int  second); 

To invoke information about the year, month, hour, week, and so on, you can do so by calling the following method:

int get (int field);
Where the value of the parameter field is determined by the static constant of the Calendar class. Among them: year represents years, month represents months, HOUR represents 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:

Method function
abstract void Add (int which,int val) Add Val to the time or date specified by which, and you can add a negative number if you need to implement the reduced function. Which must be one of the fields defined by the Calendar class, such as Calendar.hour
Boolean after (Object calendarobj) Returns False if the call to the Calendar object contains a date that is later than the date contained by the object specified by Calendarobj.
Boolean before (Object calendarobj) Returns False if the call to the Calendar object contains a date that is earlier than the date contained by the object specified by the calendarobj.
Final void Clear () Zeroing all time components contained in the calling object
final void Clear (int which) Zeroing the time component specified for the which contained by the calling object
Boolean equals (Object calendarobj) Returns true if the call to the Calendar object contains a date that is equal to the date that the specified object contains, or False if the calendarobj.
int get (int calendarfield) Returns the value of a time component that invokes a Calendar object, which is specified by CalendarField and can be returned by components such as: Calendar.year,calendar.month, etc.
Static Calendar getinstance () Returns a Calendar object that uses the default geography and time zone
Final Date GetTime () Returns a Date object with equal time to the calling object
Final Boolean isSet (int which) Returns True if the Call object contains a which specified time part is set, otherwise false is returned
final void set (int year,int month) Set the various date and time parts of the calling object
final void SetTime (Date D) Get the date and time part from date Object D
void Settimezone (TimeZone t) Sets the time zone specified by the calling object's time zone to T
GregorianCalendar class

GregorianCalendar is a class that implements the Calendar class specifically, 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 geography and time zone.

The GregorianCalendar class defines two fields: AD and BC, representing BC and A.D. respectively. Its default construction method, GregorianCalendar () initializes the object with the current date and time of the default geography and time zone, or it can specify a geography and time zone to create a GregorianCalendar object, for example:

1 GregorianCalendar (locale locale); 2 GregorianCalendar (TimeZone TimeZone); 3 GregorianCalendar (TimeZone timezone,locale Locale);

The GregorianCalendar class provides an implementation of all the abstract methods in the Calendar class, along with additional methods that are used to determine the leap year in the following way:

Boolean isleapyear (int year);
If year is a leap year, the method returns True, otherwise false is returned.

Series Articles:Java know how much (top)Java know how much (interface) interfaceJava knows how much (40) the difference between an interface and an abstract classJava know how much (41) generic explanationJava know how much (42) the range of generic wildcard characters and type parametersJava know how much (43) Exception Handling BasicsJava know how much (44) exception typeJava know how much (45) uncaught ExceptionsHow much Java knows (the) use of try and catchJava know how much (47) use of multiple catch statementsJava knows how much (in) the nesting of try statementsJava know how much (a) Throw: Exception throwsJava know how many () Java throws clausesJava knows how many (or) finallyJava know how much (52) built-in exceptionsJava know how much (53) Use Java to create your own exception subclassesJava know how much (54) assertion detailedJava know how many (55) threadsJava know how much (56) threading ModelJava know how much (57) main threadJava know how much (58) thread Runnable interface and thread class explanationJava know how much (59) Create multithreadingJava know how much (isAlive) and join () useJava know how much (61) thread PriorityJava know how much (62) thread synchronizationJava know how much (63) inter-thread communicationJava know how much (64) thread deadlockJava know how much (65) thread hangs, resumes, and terminatesJava know how much (66) Overview of input and output (IO) and streamsJava know how much (67) character-oriented input streamJava know how much (68) character-oriented output streamJava know how much (69) byte-oriented input and output streamJava know how much (70) application for byte streamJava know how much (71) file and directory managementJava know how much (72) random Read and write filesJava know how much (73) file compression processingJava know how much (74) base Class libraryJava know how much (the) object classJava know how (76) Language Pack (Java.lang) Introduction

Java know how many (77) date and Time classes

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.