in JSP, we often have to deal with the date and time information, when you can use the date class in Java.util, when writing Java source program, it is obvious that you must pass the "import java.util.*" Introduction of Java.util software packages; When writing JSP source programs, because date is not the default object of JSP, you still have to introduce this package through the "<%@ page import=" java.util.* "". In addition, because there is a date class with the same name in java.sql, in order to correctly differentiate between the two, you might want to write it java.util.Date when you use the date class in Java.util.
in fact, the date class does not just represent dates, but is accurate to milliseconds. From this point of view, perhaps it is more appropriate to call it a datetime, in this article, we sometimes call date, hope not to arouse the reader misunderstanding.
when it comes to the date classes in Java.util, we often refer to UTC and GMT, which are almost the same concept. They all refer to Greenwich Mean time, except that the term UTC is more formal. The difference is that the former is an astronomical concept, and the latter is based on an atomic clock. In UTC, there is a "leap second" every year or two, and we generally ignore this "leap second", but in Java, this causes us to sometimes appear in 60 or 61 seconds.
The date class has a method that breaks the date into years, months, days, times, minutes, seconds, and converts it to a string, and it can perform a reverse operation. But starting with JDK1.1, the Calendar class and the date format class can also perform both types of functionality, and the corresponding method in the date class is no longer recommended, according to the official Java file. But in principle, there is no big difference between the two. In this article, we will continue to introduce these methods, but will make a corresponding note.
in all cases, the parameters accepted by these methods do not need to satisfy the valid range; For example, if a date is defined as 32 days, it will be interpreted as February 1.
A, date constructor
1.1 Constructs a date instance that reflects the time of the day
Date
public Date ()
constructs a Date object and initializes it to reflect the current time.
1.2 Constructs a date instance from a long-integer data
Date
public Date (long date)
Constructs a Date object and initializes it based on the number of milliseconds relative to GMT January 1, 1970 00:00:00.
parameter:
Date-The number of milliseconds relative to GMT January 1, 1970 00:00:00.
1.3 Constructs a date instance from the time of the month and the day
Date
public Date (int,
int month,
int date)
public Date (int,
Int month,
int date,
int hrs,
int min)
public Date (int,
int month,
int date,
int hrs,
int min,
int sec)
these three constructors are deprecated, in JDK version 1.1, respectively, by Calendar.set (year + 1900, month, date) or GregorianCalendar (year + 1900, month, date ), Calendar.set (year + 1900, month, date, hrs, min) or GregorianCalendar (year + 1900, month, date, hrs, min), Calendar.set (yea R + 1900, month, date, hrs, min, sec) or GregorianCalendar (year + 1900, month, date, hrs, min, sec) instead.
Constructs a Date object and initializes it to the start time, year, month, date, Hrs, and the start time of the corresponding date for year, month, and date (that is, midnight), years, month, date, hrs, and Min. Min and SEC start time for seconds.
parameter:
year-years minus 1900.
Month-The month is between 0-11.
Date-dates between 1-31.
hrs-The hour between 0-23.
min-minutes between 0-59.
sec-sec between 0-59.
1.4 Constructs a date instance from a string
Date
public Date (String s)
is not recommended for use. In JDK version 1.1, replaced by Dateformat.parse (String s).
constructs a Date object and initializes it to the date and time of the object of the string s. Parsing the string will be the same as the parse (java.lang.String) method.
parameter:
S-A string that reflects the date.
II, other methods
2.1 Converts a string into a Date object corresponding to the long shaping representation
Parse
public static long parse (String s)
is not recommended for use. In JDK version 1.1, replaced by Dateformat.parse (String s).
An attempt was made to parse the string s as a representation of a date and time. If the resolution is successful, the number of milliseconds relative to GMT January 1, 1970 00:00:00 is returned. Conversely, the illegalargumentexception exception will be thrown.
It can accept many statement formats; Specifically, it can understand this format: "Sat, Aug 1995 13:30:00 GMT". It can also understand U.S. Such time zone abbreviations, but usually we represent the time zone: "Sat, Aug 1995 13:30:00 gmt+0430". If the time zone is not specified, the default current time zone is represented. Here, GMT and UTC represent the same concept.
The
string s is processed from left to right. The characters between "(" and ")" will be ignored. Parentheses can be nested. In addition, the string can contain only the following characters:
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
0123456789,+-:/
and spaces.
numbers that appear in the string are resolved according to the following rules:
If a number starts with a + or-and the year has been validated, the number is considered a time zone setting. If this number is less than 24, this represents the time zone offset of the hour. Otherwise, the time zone offset is the minute. Symbol-Represents the western time zone. The time zone setting is always relative to UTC. In this way, for example,-5 means "Greenwich West 5 Hours", while +0430 means "Greenwich East 4 hours 30 minutes". You can also use redundant characters: GMT, UT, and UTC. For example, GMT-5 or utc+0430.
This number will represent the year if one of the following conditions is true:
1, the number is equal to or greater than 70 and followed by a space, comma, or slash, or at the end of the string.
2, if the number is less than 70, and the month or date has been defined.
if the number of years is less than 100, this is an abbreviation that represents one year before the current 80 and 19 years later. If current is 1999, then 19 to 99 means 1919 to 1999, and 0 to 18 indicates 2000 to 2018. There is a need to pay special attention to the differences in SimpleDateFormat.
If the number follows a colon, this means an hour, unless the hour is already defined. In the case where the hour has been defined, this represents the minute.
If the number follows a slash, this represents the month (0 to 11 for January-December) unless the month is already defined. This represents a date when the month has already been defined.
If the number follows a space, a comma or hyphen, or at the end of a string, that represents a minute when an hour has been defined but no minutes have been defined, which represents a second if the hour and minute have been defined but no seconds have been defined, otherwise this represents a date.
the letters that appear in the string are resolved according to the following rules:
AM (ignoring case) will be ignored (but parsing will fail if the hours are not defined, less than 1, or greater than 12).
PM (ignores case) and will add 12 after the hour (but parsing will fail if the hours are not defined, less than 1, or greater than 12).
SUNDAY, MONDAY, Tuesday, Wednesday, Thursday, Friday, and Saturday, and their prefixes (ignoring case), are ignored. For example, Sat, Friday, Tue and Thurs will all be ignored.
In addition, January, February, March, APRIL, May, JUNE, July, August, September, October, November, and December, and their prefixes (ignoring case), is considered to represent the month in this order and converts it to a number (0 to 11). For example, Aug, Sept, April, and Nov all represent the month. And Ma will be understood as March rather than May.
GMT, UT, and UTC (Ignoring case), will be treated as UTC.
EST, CST, MST, and PST (ignore case), will be considered as the time zone for North America, Greenwich West 5 hours, 6 hours, 7 hours, 8 hours respectively. and EDT, CDT, MDT and PDT represent the same concept.
when all of the strings are scanned, if the time zone is defined, the times are considered to be UTC, and then the time zone is applied. Otherwise, the time refers to the time in the current time zone.
parameter:
S-a date that is resolved as a date.
return:
the number of milliseconds relative to GMT January 1, 1970 00:00:00.
2.2 Gets the year, month, day, hour, minute, second element, and week
in the Date object
getyear
public int getyear ()
getmonth
public int getmonth ()
getDate
public int getDate ()
getday
public int getday ()
getHours
public int getHours ()
getminutes
public int getminutes ()
getseconds
public int getseconds ()
This set of methods is not recommended for use. JDK version 1.1, respectively, is Calendar.get (calendar.year)-1900, Calendar.get (Calendar.month), Calendar.get (Calendar.day_of_month), Calendar.get (Calendar.day_of_week), Calendar.get (Calendar.hour_of_day), Calendar.get (Calendar.MINUTE), Calendar.get (Calendar.second) instead.
its function is:
returns the year corresponding to date minus 1900.
returns the month corresponding to the date object. The return value is between 0 and 11, and 0 represents January.
the date of this Date object. The return value is between 1 and 31.
returns the week of this date. (0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday)
the hour when the date object was returned. The return value is a number from 0 to 23.
returns the minute of this date, and the return value is between 0 and 59.
returns the second of the date. The return value is between 0 and 61. 60 and 61 appear only when the Java Virtual machine is processing leap seconds.
2.3 Sets the year, month, day, time, minute, and second elements in the Date object
setyear
public void Setyear (int year)
setmonth
public void setmonth (int month)
setdate
public void setdate (int date)
sethours
public void sethours (int hours)
setminutes
public void setminutes (int minutes)
setseconds
public void setseconds (int seconds)
This set of methods is not recommended for use. In JDK version 1.1, they were Calendar.set (calendar.year, Year + 1900), Calendar.set (calendar.month, int MONTH), Calendar.set ( calendar.day_of_month, int date), Calendar.set (calendar.hour_of_day, int hours), calendar.set (calendar.minute, int minutes), Calendar.set (calendar.second, int seconds) instead.
its function is:
sets the year for the date object to the specified value plus 1900. The other properties of the Date object are unchanged (if the date is February 29 and the year set is not a leap years, the new date will be March 1.) )
the month of the set date to the specified value. The other elements of the date are unchanged. If the date is October 31 and the current month is June, the new date will be July 1, since June is only 30 days.
The Date object is set to the specified value. The other elements of the date are unchanged. If the date is April 31 and the current date is 31st, the new date will be May 1, since April is only 30 days.
sets the hour for this date object, and the other elements of the date are unchanged.
sets the minute for this date object to the specified value, and the other elements of the date are unchanged.
sets the second of this date object to the specified value. The other properties of the object are unchanged.
its parameters in order:
year-years.
Month-The month is between 0-11.
Date-dates between 1-31.
hours-hour value.
minutes-min.
seconds-Sec.
2.4 Two Date objects compare
before
public boolean before (Date)
after
public boolean after (Date)
equals
public boolean equals (Object obj)
CompareTo
public int CompareTo (Date anotherdate)
CompareTo
public int compareTo (Object o)
its function is:
test whether this date is before the specified date.
test whether this date is after the specified date.
compares two dates for equality. Returns true if and only if the argument is not empty and if the parameter is exactly equal to the Date object (to the millisecond level). In fact, two date objects are equal, meaning that the GetTime method returns the same long integer data.
compares the order of two dates.
compares this date to another object. If this object is a date, its functionality will be exactly the same as CompareTo (date). Otherwise, a ClassCastException exception is thrown (because the date can only be compared to another date).
Its parameters are:
when-a date.
when-a date.
obj-the object used to compare.
anotherdate-Date used to compare.
O-the object used to compare.
returns the following sequence:
returns True if and only if the date object is earlier than the When, otherwise returns false.
returns True if and only if the date object is later than when;
Returns True when two objects are equal, or false
.
returns 0 if the two date equals, or if the date returns a value less than 0 before the parameter value, or if the date returns a value greater than 0 before the parameter value.
returns 0 if the two date equals, or if the date returns a value less than 0 before the parameter value, or if the date returns a value greater than 0 before the parameter value.
2.5 date objects and their corresponding long integer representations
GetTime
public Long getTime ()
returns the number of milliseconds for this date object relative to GMT January 1, 1970 00:00:00.
return:
the number of milliseconds relative to GMT January 1, 1970 00:00:00.
settime
public void settime (long)
sets this date object according to the number of milliseconds relative to GMT January 1, 1970 00:00:00.
parameter:
time-the number of milliseconds.
2.6 Other
Clone
public Object Clone ()
returns a copy of this object.
return:
a copy of this instance.
Thrown:
Clonenotsupportedexception-If the class of this object does not support the Cloneable interface. This exception will be thrown.
OutOfMemoryError-Not enough memory.
UTC
public static long UTC (int year,
int month,
int date,
int hrs,
int min,
int sec)
is not recommended for use. In JDK version 1.1, it was Calendar.set (year + 1900, month, date, hrs, min, sec) or GregorianCalendar (year + 1900, month, date, hrs, Min, se c), (using UTC TimeZone), followed by Calendar.gettime (). GetTime () instead.
determines the date and time based on the parameters. This parameter is resolved to the year, month, day, time, minute, and second. Just as in the date constructor, it is only used UTC instead of the current time zone. This time will be represented as the number of milliseconds relative to GMT January 1, 1970 00:00:00.
parameter:
year-years minus 1900.
Month-The month is between 0-11.
Date-dates are between 1-31.
hrs-The hour between 0-23.
min-minutes between 0-59.
sec-sec between 0-59.
return:
The number of milliseconds that the
parameter corresponds to the date relative to GMT January 1, 1970 00:00:00.
hashcode
public int hashcode ()
returns the hash code value for this object, which means that the long integer value returned by the GetTime () method is as follows:
Converts this date object to a string of the following format:
Dow mon dd hh:mm:ss zzz yyyy
is here:
Dow is the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).
Mon is the month (the Feb, the Mar, the APR, May, June, April, Aug, Sep, Oct, Nov, Dec).
The
DD is the date (01 to 31) and is represented by a two-digit number.
hh is an hour (00 to 23), represented by a two-digit number.
mm is minutes (00 to 59), expressed in two digits.
The
SS is seconds (00 to 61, expressed in two digits).
zzz is the time zone. The abbreviation for the standard Time zone is the same as in the Parse method. If the time zone information is invalid zzz is empty-that is, no characters are included.
yyyy is the year, expressed in four digits.
return:
A string representing the date.
tolocalestring
public String tolocalestring ()
Not recommended for use. JDK version 1.1, replaced by Dateformat.format (date date).
generates a string that reflects this date object in the current format. This format string should be quite familiar to Java application developers. However, it is not exactly the same as the "%c" format of the strftime () function in ISO C.
return:
A string that reflects this date, using the current convention.
togmtstring
public String togmtstring ()
is not recommended for use. In JDK version 1.1, the Dateformat.format (date date) is replaced with the GMT time zone.
creates a string that reflects the date object in the following format: D Mon yyyy hh:mm:ss GMT
of which:
d Date (1 to 31), expressed in one to two digits.
Mon month (with a few, Feb, Mar, APR, May, June, April, Aug, Sep, Oct, Nov, Dec).
yyyy year, expressed in four digits.
hh hours (00 to 23), expressed in two-digit digits.
MM minutes (00 to 59), expressed in two-digit digits.
SS seconds (00 to 61), expressed in two-digit digits.
GMT is specified as Greenwich standard.
The
return value is independent of the current time zone.
return:
A string that reflects the current date (using Internet GMT).
getTimezoneOffset
public int getTimezoneOffset ()
is not recommended for use. In JDK version 1.1, it was replaced by Calendar.get (Calendar.zone_offset) + calendar.get (calendar.dst_offset).
return time zone, accurate to minutes.
For example, in Massachusetts, it should be Greenwich West 5 hours:
new Date (1). getTimezoneOffset () returns 300 because on January 14, 1996, the standard Time (Eastern Standard) was used; however, new date (5, 1). getTimezoneOffset () returns 240 because on May 1, 1996, Daylight saving time (Eastern Daylight Saving time) is used.
The return value of this method is the same as the result of the following calculation:
(This.gettime ()-UTC (This.getyear (),
this.getmonth (),
this.getdate (),
this.gethours (),
this.getminutes (),
This.getseconds ())/(60 * 1000)
return Value:
the current time zone's settings, accurate to minutes.
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.