Java Time related content learning (3) Date

Source: Internet
Author: User
Tags constructor contains local time

This chapter focuses on the date class and learns how to use it through an example. Finally, explain the relationship between UTC, GMT, and time zone.

Date Introduction

Date definition

public class Date implements Java.io.Serializable, Cloneable, comparable<date> {}

Date is the class that represents the time.
A Date object represents a specific moment that can be accurate to milliseconds. We can get to date "year, month, day, hour, minute, second" through this particular moment. Conversely, we can change the specific moment specified by date by setting the date's "year, month, day, time, minutes, seconds," and so on.
Data also allows you to format and parse date strings in addition to information such as "month and minute". That is, we can define a string that contains time information and then parse the string through date to get the corresponding Date object.

Before JDK 1.1, it was usually through data that was "minutes and seconds". However, because date's associated APIs are not easy to internationalize. Starting with JDK 1.1, you should use the Calendar class to manipulate the time of day and minutes, and you can format and parse the date string through the DateFormat class. The corresponding method in Date is deprecated.

Date and calendar are converted to each other

(a) Date converted to Calendar

New Date with date/time of 2013-09-19 14:22:30
Date date = new Date (113, 8, 19, 14, 22, 30);
Creates a new calendar object and sets the date
Calendar cal = Calendar.getinstance ();
Cal.settime (date);

Calendar changed to date

Create a new Calendar object
Calendar cal = Calendar.getinstance ();
Get date for Calendar
Date date = Cal.gettime ();

List of Date functions

The Date altogether contains the following 5 constructors

Date constructor One: Incoming year, month, Day
//(01) year-minus 1900 year. To set to 1988, the year should be 88.
//(02) Month--0-11 month. 0 is January, 1 is February, and so on.
//(03) Day-1-31 between the day.
date (int years, int month, int day)
     
/Date constructor Two: Incoming year, month, day, time, minute,/
/(01) year-minus 1900 year. To set to 1988, the year should be 88.
//(02) Month--0-11 month. 0 is January, 1 is February, and so on.
//(03) Day-1-31 between the day.
//(04)--The number of hours between 0-23.
//(05)--minutes between 0-59.
date (int years, int month, int day, int hour, int minute)
     
//Date constructor Three: Incoming "year, month, day, hour, minute, second"
//(01) year-minus 1900 The year. To set to 1988, the year should be 88.
//(02) Month--0-11 month. 0 is January, 1 is February, and so on.
//(03) Day-1-31 between the day.
//(04)--The number of hours between 0-23.
//(05)--minutes between 0-59.
//(06) seconds--the number of seconds between 0-59.
Date (int year, int month, int day, int hour, int minute, int second)
     
//Date constructor four: Pass in "millisecond". milliseconds = "Target Time"-"1970-01-01 00:00:00 GMT"
Date (long milliseconds)
     
//Date constructor five: Pass in "string".
Date (String string)

The Action API for date

Determines the date and time based on the parameters.
These parameters are interpreted as years, months, days of the month, one hour of the day, one minute in the hour, and one second in minutes.
static long UTC (int month, int day, int hour, int minute, int second)//This date is after the specified date.
Boolean after (date date)//This date is a Boolean before (date date)//Returns a copy of this object before the specified date.
Object Clone ()//compares the order of two dates.
int CompareTo (date date)//Compares two dates for equality. Boolean equals (object)//Returns the day of the month represented by this date object.
The returned value is between 1 and 31, representing the day of the month that contains or begins the time represented by this date object (interpreted in the local time zone). int getDate ()//Returns the day of the week represented by this date. The return value (0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday) represents the day of the week int getday ( )//Returns the hour represented by this date object. The return value is a number (0 to) int getHours ()//Returns the number of minutes that the hour represented by this date has elapsed (interpreted in the local time zone).
The return value is between 0 and 59. int getminutes ()//Returns the number representing the month that contains or begins the instant represented by this date object. The value returned is between 0 and 11.
0 is January, 1 is February, and so on. int getmonth ()//returns the number of seconds that the minutes represented by this date have elapsed. The value returned is between 0 and 61.
Values 60 and 61 can only occur on a Java virtual machine that takes leap seconds into account.
int getseconds ()//Returns the number of milliseconds this Date object represents since January 1, 1970 00:00:00 GMT. Long GetTime ()//return relative to UTCThe offset (in minutes) of the local time zone (corresponding to the time represented by this Date object).
int getTimezoneOffset ()//Returns a value that is the result of subtracting 1900 from the year that contains or begins the moment represented by this Date object (interpreted in the local time zone).
int getyear ()//hash value int hashcode ()///to interpret the string s as a representation of the date and time.
The static long parse (string string)//sets a day in the month of this date object to the specified value.
void setdate (int day)//sets the hour of this date object to the specified value.
void sethours (int hour)//sets the number of minutes for this date object to the specified value. void setminutes (int minute)//Set the month of this date to the specified value.
0 is January, 1 is February, and so on.
void setmonth (int month)//sets the number of seconds for this Date to the specified value.
void setseconds (int second)//sets this Date object to represent the point in time milliseconds after January 1, 1970 00:00:00 GMT.
void SetTime (long milliseconds)//sets the year of this date object to the specified value plus 1900.     void setyear (int year)//return GMT String String togmtstring ()//back to local string string tolocalestring ()//Return strings string ToString ()

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.